How do I know an audio playback arrived at the end?

Asked

Viewed 34 times

0

So I would like to know when my audio has reached the end of the playback, follow my code to startar the audio and stop, I thank you in advance!

private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

try {
    mRecorder.prepare();
} catch (IOException e) {
    Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();}

private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;

uploadAudio();

}

  • Why don’t you use the Mediaplayer class to run the audio? So you can have a greater control over it! There is an event called onCompletion in this class!

  • Thanks Milton, I’m using the media player , I have onCompletion here, I didn’t know it was for this , for me it was called as soon as I started reading the file and not when it was finished, anyway . I’ll check here.

  • 1

    See if this snippet of code helps you! mplayer.setOnCompletionListener(new Mediaplayer.Oncompletionlistener() { public void onCompletion(Mediaplayer mp) { //TODO } });

1 answer

0

Man, the class FileObserver meets you in this question, follows the Documentation, is very simple to use, and when the file is closed(ends recording), the callback onEvent is called with the parameter CLOSE_WRITE.

Follow the example:

MyFileObserver fb = new MyFileObserver(mediaFile_path, FileObserver.CLOSE_WRITE);
fb.startWatching();

class MyFileObserver extends FileObserver {

    public MyFileObserver (String path, int mask) {
        super(path, mask);
    }

    public void onEvent(int event, String path) {
        //uploadAudio();
    }
}

Don’t forget to call the method stopWatching()... Original response (English)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.