我正在创建一个带有计时器计数器的记录器应用程序,计时器在开始记录、停止记录和停止播放时工作正常。
我的问题只是开始播放,当按下开始播放按钮时,计时器从 0 开始,但是当音频文件停止时我无法停止计时器。
这是开始播放代码,请帮助。我非常感谢任何帮助
playAudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) throws IllegalArgumentException,
SecurityException, IllegalStateException {
stopRecord.setEnabled(false);
startRecord.setEnabled(false);
stopAudio.setEnabled(true);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(AudioSavePathInDevice);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
if (!running) {
myChronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
myChronometer.start();
running = true;
// myChronometer.stop();
}
mediaPlayer.start();
Toast.makeText(MainActivity.this, "Recording Playing",
Toast.LENGTH_LONG).show();
}
});
先生,这是停止音频的代码
stopAudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (running) {
myChronometer.stop();
myChronometer.setBase(SystemClock.elapsedRealtime());
pauseOffset = 0;
pauseOffset = SystemClock.elapsedRealtime() - myChronometer.getBase();
running = false;
}
stopRecord.setEnabled(false);
startRecord.setEnabled(true);
stopAudio.setEnabled(false);
playAudio.setEnabled(true);
if(mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
// MediaRecorderReady();
//resetmymyChronometer
myChronometer.setBase(SystemClock.elapsedRealtime());
pauseOffset = 0;
}
}
});
这是停止记录的代码
stopRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (running) {
myChronometer.stop();
myChronometer.setBase(SystemClock.elapsedRealtime());
pauseOffset = 0;
pauseOffset = SystemClock.elapsedRealtime() - myChronometer.getBase();
running = false;
}
mediaRecorder.stop();
stopRecord.setEnabled(false);
playAudio.setEnabled(true);
startRecord.setEnabled(true);
stopAudio.setEnabled(false);
Toast.makeText(MainActivity.this, "Recording Completed",
Toast.LENGTH_LONG).show();
}
});
MediaRecorderReady 方法
public void MediaRecorderReady(){
//myChronometer start
if (!running) {
myChronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
myChronometer.start();
running = true;
//***********************
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(AudioSavePathInDevice);
}
}
开始记录监听器
startRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(checkPermission()) {
AudioSavePathInDevice =
Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
CreateRandomAudioFileName(5) + "AudioRecording.3gp";
MediaRecorderReady();
try {
startRecord.setEnabled(true);
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startRecord.setEnabled(false);
stopRecord.setEnabled(true);
Toast.makeText(MainActivity.this, "Recording started",
Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
});