媒体播放器未经发布而最终确定(JNI)

时间:2014-01-15 05:13:31

标签: android-mediaplayer

我在logcat中收到一条警告信息

  

W / MediaPlayer-JNI(16795):MediaPlayer未经发布而最终确定

请告诉我哪里错了以及如何解决问题。 我希望当第二次调用playAudio时它会停止之前的mMediaPlayer音频播放。

public class WordAudio  {

MediaPlayer mMediaPlayer;
String url;
private static WordAudio wordAudio;
private WordAudio() {
}

public static WordAudio getInstance() {
    if (wordAudio == null) {
        wordAudio = new WordAudio();
    }
    return wordAudio;

}

public void playAudio(final Context context, String id) {

    try {

        if (mMediaPlayer != null) {

            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.stop();
            }           
        }

        mMediaPlayer = new MediaPlayer();
        url = AUDIO_STREEM_URL+"id=" + id+ "&x=mp3";
        mMediaPlayer = MediaPlayer.create(context, Uri.parse(url)); 
        mMediaPlayer.setOnPreparedListener(new OnPreparedListener() { 
            @Override
            public void onPrepared(MediaPlayer mp) {
                mMediaPlayer.start();
            }
        });
        mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer mMediaPlayer) {    
                //mMediaPlayer.release();
            }
        });


        mMediaPlayer.setOnInfoListener(new OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mMediaPlayer, int what, int extra) {
                if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {                       
                } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
                   mMediaPlayer.start();
                }
                return false;
            }
        });
    }
    catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}}

谢谢

1 个答案:

答案 0 :(得分:2)

以这种方式完成

mMediaPlayer = new MediaPlayer();
url = AUDIO_STREEM_URL;
mMediaPlayer = MediaPlayer.create(context, Uri.parse(url)); 
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() { 
    @Override
    public void onPrepared(MediaPlayer mp) {
        mMediaPlayer.start();
    }
});
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
    public void onCompletion(MediaPlayer mMediaPlayer) {    
        mMediaPlayer.release();
    }
});