Question

I have a button that starts playing a recorded audio when pressed. Also it's icon changes. I want to change the icon again when the playing ends. Can somebody give me an example or something? Thank you :)

Here is my audio class:

public class Audio {
    private MediaPlayer mPlayer;
    private MediaRecorder mRecorder;

    public String path;

    public void setPath(String path) {
        this.path = path;
    }

    public void stopPlayRecorded() {
        if (mPlayer != null) {
            mPlayer.stop();
        }
    }

    public void playRecorded() throws IllegalArgumentException,
            SecurityException, IllegalStateException, IOException {

        ditchMediaPlayer();

        File f = new File(path);

        if(!recordingExists(f)) {
            return;
        }

        mPlayer = new MediaPlayer();
        mPlayer.setDataSource(path);
        mPlayer.prepare();
        mPlayer.start();
    }

    private void ditchMediaPlayer() {
        if (mPlayer != null) {
            try {
                mPlayer.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public void stopRecording() {
        if (mRecorder != null) {
            mRecorder.stop();
            ditchMediaRecorder();
        }

    }

    public void beginRecording() throws IllegalStateException, IOException {
        File f = new File(path.substring(0, path.lastIndexOf("/")));

        if (!recordingExists(f)) {
            f.mkdirs();
        }

        ditchMediaRecorder();
        File outFile = new File(path);

        Log.d("R_PATH", path);

        if (outFile.exists()) {
            outFile.delete();
        }

        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile(path);

        mRecorder.prepare();
        mRecorder.start();
    }

    public boolean recordingExists(File f) {
        if (f.exists())
            return true;
        else
            return false;
    }

    private void ditchMediaRecorder() {
        if (mRecorder != null) {
            mRecorder.release();
        }
    }
}
Was it helpful?

Solution

Yes you can do that with OnCompletionListener:

    mPlayer.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            // do something when the end of a media source 
            // has been reached during playback.

        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top