2014-01-15 47 views
3

我M在logcat中得到一個警告消息媒體播放器完成而不釋放(JNI)

W/MediaPlayer的-JNI(16795):MediaPlayer的沒有被釋放

請告訴我在哪裏IM敲定錯誤以及如何解決問題。 我想第二次調用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(); 
    } 
}} 

感謝

+2

只是猜測'//mMediaPlayer.release();' –

+0

有U要我刪除mMediaPlayer.release()的意見; – Gattsu

+1

是的,這就是我的建議。 –

回答

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(); 
    } 
}); 
+0

對於我仍然劑量不工作! – Criss

+0

問它作爲一個新的questoin – Gattsu