'Play recorded audio with chronometer counter
I'm creating a recorder app with chronometer counter, the chronometer is working fine with start record, stop record and stop play.
My problem is only with start play, when pressing start play button, the chronometer start from 0 but I couldn't stop the chronometer when the audio file stops.
This is the start play code, please help. Any help I greatly appreciated
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();
}
});
Sir, this the code of stop audio
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;
}
}
});
this is the code of stop record
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 method
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 Listener
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();
}
}
});
Solution 1:[1]
You can use OnCompletionListener, for example:
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// your code to stop record
}
});
for more information : https://developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener
Solution 2:[2]
The line
myChronometer.stop();
is never called.
In your playAudio's onClick() method, you should set
running = true
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sofiane Majdoub |
Solution 2 | matdev |