完成播放声音后如何发布MediaPlayer?

时间:2013-03-03 18:13:21

标签: android media-player android-mediaplayer

我有一个类似app的音板。即使用户点击另一个声音,我也不希望声音停止。但是,过了一会儿,就像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";

    }

}

3 个答案:

答案 0 :(得分:9)

试试这个

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

        }
    });

答案 1 :(得分:0)

您可以使用release()medeiaplayer

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

答案 2 :(得分:0)

尝试一下:

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;
            }
        });