android 播放完声音后,如何释放MediaPlayer?

xiozqbni  于 2023-03-16  发布在  Android
关注(0)|答案(4)|浏览(181)

我有一个类似soundboard的应用程序。我不希望声音停止,即使用户点击另一个声音。但是,过了一段时间,像30点击(声音播放器),它停止。我认为它耗尽内存或Android不允许它创建超过30个示例。
如何更好地实现这一点,以便在声音结束时销毁媒体播放器示例?

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";

    }

}
2wnc66cl

2wnc66cl1#

试试这个

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

        }
    });
8ulbf1ek

8ulbf1ek2#

试试这个:

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

或者:

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

smtd7mpg3#

您可以使用medeiaplayer的release(

if(!mediaPlayerObject.isPlaying()) 
   mediaPlayerObject.release();
lnlaulya

lnlaulya4#

我在Kotlin的所作所为:

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

            start()
        }
    }
}

相关问题