Android - MediaPlayer.release 销毁无效
Android - MediaPlayer.release on Detroy not working
我在 Stack-overflow 上搜索了很多答案,但 none 对我有用,必须使用这个,我用过但也不起作用...
private void releaseMediaPlayer() {
if (mediaPlayer != null) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
mediaPlayer = null;
}
}
声明为:MediaPlayer mediaPlayer=new MediaPlayer();
准备媒体播放器方法是这样的:
public void playMp3(String _link){
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(_link);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
btnDegjo.setVisibility(View.VISIBLE);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
当用户点击按钮时,mp3 正在播放或暂停或停止..
我的需求:
当用户点击后退按钮或onDestroy mediaPlayer被释放时,方法playMp3(link);
被调用onCreate..我不知道用户是立即返回还是播放音乐然后返回..
@Override
public void onBackPressed() {
super.onBackPressed();
releaseMediaPlayer();
}
@Override
protected void onDestroy() {
super.onDestroy();
releaseMediaPlayer();
}
当用户点击后退按钮或 onDestroy 应用程序停止工作时使用此代码..
为什么?
编辑:
10-13 15:45:09.886: E/AndroidRuntime(10589): FATAL EXCEPTION: main
10-13 15:45:09.886: E/AndroidRuntime(10589): Process: com.FS.myapp,
PID: 10589 10-13 15:45:09.886: E/AndroidRuntime(10589):
java.lang.RuntimeException: Unable to pause activity
{com.FS.myapp/com.FS.myapp.SS}: java.lang.NullPointerException 10-13
15:45:09.886: E/AndroidRuntime(10589): at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:3242)
10-13 15:45:09.886: E/AndroidRuntime(10589): at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:3193)
10-13 15:45:09.886: E/AndroidRuntime(10589): at
android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3171)
10-13 15:45:09.886: E/AndroidRuntime(10589): at
android.app.ActivityThread.access00(ActivityThread.java:151) 10-13
15:45:09.886: E/AndroidRuntime(10589): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1338)
10-13 15:45:09.886: E/AndroidRuntime(10589): at
android.os.Handler.dispatchMessage(Handler.java:110) 10-13
15:45:09.886: E/AndroidRuntime(10589): at
android.os.Looper.loop(Looper.java:193) 10-13 15:45:09.886:
E/AndroidRuntime(10589): at
android.app.ActivityThread.main(ActivityThread.java:5292) 10-13
15:45:09.886: E/AndroidRuntime(10589): at
java.lang.reflect.Method.invokeNative(Native Method) 10-13
15:45:09.886: E/AndroidRuntime(10589): at
java.lang.reflect.Method.invoke(Method.java:515) 10-13 15:45:09.886:
E/AndroidRuntime(10589): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
10-13 15:45:09.886: E/AndroidRuntime(10589): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 10-13
15:45:09.886: E/AndroidRuntime(10589): at
dalvik.system.NativeStart.main(Native Method) 10-13 15:45:09.886:
E/AndroidRuntime(10589): Caused by: java.lang.NullPointerException
10-13 15:45:09.886: E/AndroidRuntime(10589): at
com.FS.myapp.SS.onPause(SS.java:62) 10-13 15:45:09.886:
E/AndroidRuntime(10589): at
android.app.Activity.performPause(Activity.java:5368) 10-13
15:45:09.886: E/AndroidRuntime(10589): at
android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1239)
10-13 15:45:09.886: E/AndroidRuntime(10589): at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:3224)
10-13 15:45:09.886: E/AndroidRuntime(10589): ... 12 more
您的堆栈跟踪和 onPause 方法表明您的变量之一为空。
快速修复可以是:
@Override
protected void onPause() {
if (mediaPlayer != null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
if (btnPlay != null)
btnPlay.setText("Play");
super.onPause();
}
我在 Stack-overflow 上搜索了很多答案,但 none 对我有用,必须使用这个,我用过但也不起作用...
private void releaseMediaPlayer() {
if (mediaPlayer != null) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
mediaPlayer = null;
}
}
声明为:MediaPlayer mediaPlayer=new MediaPlayer();
准备媒体播放器方法是这样的:
public void playMp3(String _link){
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(_link);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
btnDegjo.setVisibility(View.VISIBLE);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
当用户点击按钮时,mp3 正在播放或暂停或停止..
我的需求:
当用户点击后退按钮或onDestroy mediaPlayer被释放时,方法playMp3(link);
被调用onCreate..我不知道用户是立即返回还是播放音乐然后返回..
@Override
public void onBackPressed() {
super.onBackPressed();
releaseMediaPlayer();
}
@Override
protected void onDestroy() {
super.onDestroy();
releaseMediaPlayer();
}
当用户点击后退按钮或 onDestroy 应用程序停止工作时使用此代码.. 为什么?
编辑:
10-13 15:45:09.886: E/AndroidRuntime(10589): FATAL EXCEPTION: main 10-13 15:45:09.886: E/AndroidRuntime(10589): Process: com.FS.myapp, PID: 10589 10-13 15:45:09.886: E/AndroidRuntime(10589): java.lang.RuntimeException: Unable to pause activity {com.FS.myapp/com.FS.myapp.SS}: java.lang.NullPointerException 10-13 15:45:09.886: E/AndroidRuntime(10589): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3242) 10-13 15:45:09.886: E/AndroidRuntime(10589): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3193) 10-13 15:45:09.886: E/AndroidRuntime(10589): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3171) 10-13 15:45:09.886: E/AndroidRuntime(10589): at android.app.ActivityThread.access00(ActivityThread.java:151) 10-13 15:45:09.886: E/AndroidRuntime(10589): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1338) 10-13 15:45:09.886: E/AndroidRuntime(10589): at android.os.Handler.dispatchMessage(Handler.java:110) 10-13 15:45:09.886: E/AndroidRuntime(10589): at android.os.Looper.loop(Looper.java:193) 10-13 15:45:09.886: E/AndroidRuntime(10589): at android.app.ActivityThread.main(ActivityThread.java:5292) 10-13 15:45:09.886: E/AndroidRuntime(10589): at java.lang.reflect.Method.invokeNative(Native Method) 10-13 15:45:09.886: E/AndroidRuntime(10589): at java.lang.reflect.Method.invoke(Method.java:515) 10-13 15:45:09.886: E/AndroidRuntime(10589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 10-13 15:45:09.886: E/AndroidRuntime(10589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 10-13 15:45:09.886: E/AndroidRuntime(10589): at dalvik.system.NativeStart.main(Native Method) 10-13 15:45:09.886: E/AndroidRuntime(10589): Caused by: java.lang.NullPointerException 10-13 15:45:09.886: E/AndroidRuntime(10589): at com.FS.myapp.SS.onPause(SS.java:62) 10-13 15:45:09.886: E/AndroidRuntime(10589): at android.app.Activity.performPause(Activity.java:5368) 10-13 15:45:09.886: E/AndroidRuntime(10589): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1239) 10-13 15:45:09.886: E/AndroidRuntime(10589): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3224) 10-13 15:45:09.886: E/AndroidRuntime(10589): ... 12 more
您的堆栈跟踪和 onPause 方法表明您的变量之一为空。
快速修复可以是:
@Override
protected void onPause() {
if (mediaPlayer != null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
if (btnPlay != null)
btnPlay.setText("Play");
super.onPause();
}