6

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

    }

}
4

3 回答 3

11

尝试这个

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

        }
    });
于 2013-03-03T18:26:36.040 回答
0

您可以使用release() 的媒体播放器

if(!mediaPlayerObject.isPlaying()) 
   mediaPlayerObject.release();
于 2013-03-03T18:14:51.857 回答
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;
            }
        });
于 2019-12-11T11:25:59.417 回答