按下后退按钮停止媒体播放器

时间:2015-01-10 06:12:14

标签: java android media-player

我有一个播放来自URL的mp3的媒体播放器。当我播放歌曲并按后退按钮时,应用程序会出错。我使用了prepareAsync();

我是新人,很抱歉,如果你不能理解我。

这是我的play函数和ondestroy函数。感谢。

public void  playSong(final int naatindex){
    // Play song
    tv = (TextView) this.findViewById(R.id.mywidget);  
    try {
        mp.reset();
        mp.setDataSource(naatpaths[naatindex]);
        Log.w("Play Song", "CS"+naatindex);
        tv.setText("Buffering Please Wait....!");
        songCurrentDurationLabel.setVisibility(View.INVISIBLE);
        tv.setSelected(true);
        songTotalDurationLabel.setVisibility(View.INVISIBLE);
        mp.prepareAsync();
        mp.setOnPreparedListener(new OnPreparedListener(){
            @Override
            public void onPrepared(MediaPlayer mp){
                songCurrentDurationLabel.setVisibility(View.VISIBLE);
                songTotalDurationLabel.setVisibility(View.VISIBLE);

                    mp.start();

                    tv.setText(naattitles[naatindex]);
                    // Changing Button Image to pause image
                    btnPlay.setImageResource(R.drawable.btn_pause);
                    // set Progress bar values
                    songProgressBar.setProgress(0);
                    songProgressBar.setMax(100);
                    // Updating progress bar
                    updateProgressBar();
            }
        });



        mp.setOnErrorListener(new OnErrorListener() {
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.w("Unable to Play", "CS"+naatindex);
                playSong(currentSongIndex+1);
                return false;
            }
        });




         // Set focus to the textview
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

  @Override
  public void onDestroy(){
  mp.release();
  mp = null;
  super.onDestroy();
  }

这是我的日志错误

01-10 16:16:40.339: W/dalvikvm(26539): threadid=1: thread exiting with uncaught exception (group=0x416282a0)
01-10 16:16:40.344: E/AndroidRuntime(26539): FATAL EXCEPTION: main
01-10 16:16:40.344: E/AndroidRuntime(26539): java.lang.NullPointerException
01-10 16:16:40.344: E/AndroidRuntime(26539):    at com.maulantariqjameel.bayyan.media_player$1.run(media_player.java:5032)
01-10 16:16:40.344: E/AndroidRuntime(26539):    at android.os.Handler.handleCallback(Handler.java:615)
01-10 16:16:40.344: E/AndroidRuntime(26539):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-10 16:16:40.344: E/AndroidRuntime(26539):    at android.os.Looper.loop(Looper.java:137)
01-10 16:16:40.344: E/AndroidRuntime(26539):    at android.app.ActivityThread.main(ActivityThread.java:4921)
01-10 16:16:40.344: E/AndroidRuntime(26539):    at java.lang.reflect.Method.invokeNative(Native Method)
01-10 16:16:40.344: E/AndroidRuntime(26539):    at java.lang.reflect.Method.invoke(Method.java:511)
01-10 16:16:40.344: E/AndroidRuntime(26539):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-10 16:16:40.344: E/AndroidRuntime(26539):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-10 16:16:40.344: E/AndroidRuntime(26539):    at dalvik.system.NativeStart.main(Native Method)

5 个答案:

答案 0 :(得分:2)

@Override 
protected void onDestroy() { 
if (runnableObject != null) 
myHandler.removeCallbacks(runnableObject ); 
super.onDestroy(); 
}
private void backPressed() {
if (mpAudioSdCard != null) {
            mpAudioSdCard.stop();
            mpAudioSdCard.release();
            mpAudioSdCard = null;
        }

}

试试这个会对我有用我认为你的错误是你的调用super.onDestroy();第一次

答案 1 :(得分:1)

private void releaseMediaPlayer() {
    if (mp!= null) {
        if(mp.isPlaying()) {
            mp.stop();
        }
        mp.release();
        mp= null;
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    releaseMediaPlayer();
}

private void releaseMediaPlayer() {
    if (mp!= null) {
        if(mp.isPlaying()) {
            mp.stop();
        }
        mp.release();
        mp= null;
    }
}

@Override
public void onBackPressed() {
    releaseMediaPlayer();
    finish();
}

答案 2 :(得分:0)

在onDestroy()中,由于活动已经完成,因此无需调用this.finish()。删除该行,然后重试。

答案 3 :(得分:0)

你应该在release..try这段代码之前调用super.onbackpressed()

@Override
public void onBackPressed() {
 super.onBackPressed();
System.out.println("onBackPressed() called");
releaseMediaPlayer();

}

答案 4 :(得分:0)

查看日志时,您发布到Runnable的{​​{1}}内部似乎发生了错误。我猜测你是使用postDelayed()发布的,以定期更新基于Handler状态的UI或其他内容。我对吗?您在onDestroy()中将mp设置为null,但MediaPlayer继续运行并尝试访问它,从而导致Runnable

要解决此问题,您需要在onDestroy()中清除处理程序的待处理Runnables队列,如下所示:

NullPointerException

更好的方法是在onPause()中执行此操作,然后在onResume()中重新启动它