gpt4 book ai didi

java - 如何使用Java在Android中创建30秒自动音频剪辑

转载 作者:行者123 更新时间:2023-12-03 01:52:15 24 4
gpt4 key购买 nike

我正在做一个有趣的休闲项目

在android studio中,我尝试实现按钮单击,以启动自动30秒的音频记录,用户可以在该时间段内执行或说出任何操作。

剪辑完成后,它会自动播放并提供直接保存或下载到设备的功能

我还没有开始,我对应该研究API中的哪些特定方法感到好奇???

任何建议将是惊人的谢谢。

最佳答案

尝试这样的事情:

private MediaRecorder mMediaRecorder;
private static final long UPDATE_INTERVAL = 100;
private Sync mSync;
private boolean mIsRecording = false;
private long mStartTime = 0;
private long mStopTime = 0;
private String mFileName;

private void startRecord() {
startRecording();
mSync = new Sync(new Runnable() {

@Override
public void run() {
if (!mIsRecording||!isAdded())
return;
long currentTime = System.currentTimeMillis();
if (currentTime-mStartTime>AppConstants.MAX_RECORD_DURATION_S*1000){
stopRecord();
updateLayoutVisibility();
return;
}
String str = getString(R.string.recording_template,(currentTime-mStartTime)/1000,AppConstants.MAX_RECORD_DURATION_S);
mTextSeconds.setText(str);
mSync.next();
}
},UPDATE_INTERVAL);
mSync.now();
mLayoutButtonsStartRecord.setVisibility(View.GONE);
mLayoutButtonsRecord.setVisibility(View.VISIBLE);
}


private void stopRecord() {
mSync.stop();
mIsRecording = false;
mStopTime = System.currentTimeMillis();
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;
}

private void startRecording() {
if (mMediaRecorder != null) {
mMediaRecorder.release();
}
mFileName = getTempRecordFileName();
if (mFileName==null){
dismiss();
return;
}
mIsRecording = true;
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setAudioSource(AudioSource.MIC);
mMediaRecorder.setOutputFormat(OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(AudioEncoder.AAC);
mMediaRecorder.setAudioChannels(1);

mMediaRecorder.setOutputFile(mFileName);
try {
mMediaRecorder.prepare();
mMediaRecorder.start();
mStartTime = System.currentTimeMillis();
} catch (IOException e) {
Toast.makeText(getActivity(),
R.string.error_failed_start_recording, Toast.LENGTH_LONG)
.show();
Log.d(TAG, "Failed start recorder", e);
mMediaRecorder.release();
mMediaRecorder = null;
dismiss();
return;
}
}

private class Sync{
private Handler handler = new Handler();
private Runnable task;
private long period;

public Sync(Runnable task,long period){
this.task = task;
this.period = period;
handler.removeCallbacks(task);
}
public void now(){
task.run();
}

public void next(){
handler.postDelayed(task, period);
}
public void stop(){
handler.removeCallbacks(task);
}
}

@Override
public void onPause() {
super.onPause();
if (mMediaRecorder != null) {
mMediaRecorder.release();
mMediaRecorder = null;
}
if (mSync!=null){
mSync.stop();
}

}

关于java - 如何使用Java在Android中创建30秒自动音频剪辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38633603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com