How do I release MediaPlayer after it finishes playing a sound?
Asked Answered
K

4

8

I have a soundboard like app. I don't want the sound to stop even if the user clicks another sound. But, after a while, like 30 clicks (sound players), it stops. I think it runs out of memory or Android is not letting it make more than 30 instances.

How can I implement this better so that when a sound has finished, the media player instance is destroyed?

import java.io.IOException;

public class AudioRecordTest {
    private static final String LOG_TAG = "AudioRecordTest";
    private static String mFileName = null;
    private MediaRecorder mRecorder = null;
    private MediaPlayer mPlayer = null;


    public void startPlaying(int fileName) {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName + fileName + ".3gp");
            mPlayer.prepare();
            mPlayer.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

    }

    public MediaPlayer getPlayer() { 
        return mPlayer;
    }

    public void startRecording(int fileName) {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName + fileName + ".3gp");
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

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

        try {
            mRecorder.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stopRecording() {
        try {
            if (mRecorder != null) {
                mRecorder.stop();
                mRecorder.release();
                mRecorder = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public AudioRecordTest() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/aaa";

    }

}
Knavish answered 3/3, 2013 at 18:13 Comment(0)
P
11

Try this

 voicePlayer.getMediaPlayer().setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            mediaPlayer.stop();
            if (mediaPlayer != null) {
                mediaPlayer.killMediaPlayer();
            }

        }
    });
Patsy answered 3/3, 2013 at 18:26 Comment(1)
What is .killMediaPlayer()? Do you mean .release?. and why do you .stop() before the check for null?Myo
I
2

Try this:

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });

Or:

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mediaPlayer.release();
                mediaPlayer = null;
            }
        });
Infanticide answered 11/12, 2019 at 11:25 Comment(0)
M
0

You can use release() of medeiaplayer

if(!mediaPlayerObject.isPlaying()) 
   mediaPlayerObject.release();
Mcminn answered 3/3, 2013 at 18:14 Comment(2)
but that will be skipped over while the sound is playingKnavish
Set an onCompletionListener and do it when its called. Then you can release it. Of course, reusing the same player and not creating more than 1 would make more sense.Almonte
S
0

What I did in Kotlin:

class SoundServiceImpl(private val context: Context) : SoundService {
    override fun playSound(soundRes: Int) {
        MediaPlayer.create(context, soundRes).apply {
            setOnCompletionListener {
                release()
            }

            start()
        }
    }
}
Slaughterhouse answered 15/3, 2023 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.