我已经为 Android 制作/下载了一个运行良好的 Java 媒体播放器类,除了在屏幕超时或手动关闭它时,在恢复时,我在尝试mediaPlayer.isPlaying()
.
谷歌搜索我发现这可能是媒体播放器处于错误状态。然后我尝试try {...} catch(IllegalStateException...
了事情,但我添加了太多,事情变得一团糟,失控(并且没有成功)。
然后我尝试了类似的东西:
mediaPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
mp.release();
return false;
}
但它不起作用,仍然收到 IllegalStateException 错误。(编辑:顺便说一句,这里说“IllegalStateExceptions 不会触发 OnErrorListener 调用”。这是正确的吗??)
所以,我的问题:
1) 屏幕超时导致媒体播放器进入错误状态时发生了什么?2)我怎样才能防止这种情况发生?3) 我需要在 mediaPlayer.setOnErrorListener 中放入什么?4)我是否错过了错误侦听器的某些内容,需要以某种方式处理它?
抱歉……这里是新手。
编辑:我被要求添加调用,所以这里是整个类:
package com.floritfoto.apps.ave;
import java.io.FileDescriptor;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
public class Music implements OnCompletionListener{
MediaPlayer mediaPlayer;
boolean isPrepared = false;
public Music(FileDescriptor fileDescriptor){
mediaPlayer = new MediaPlayer();
try{
mediaPlayer.setDataSource(fileDescriptor);
mediaPlayer.prepare();
isPrepared = true;
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
mp.release();
return false;
}
});
} catch(Exception ex){
throw new RuntimeException("Couldn't load music, uh oh!");
}
}
public void onCompletion(MediaPlayer mediaPlayer) {
synchronized(this){
isPrepared = false;
}
}
public void play() {
if(mediaPlayer.isPlaying()){
return;
}
try{
synchronized(this){
if(!isPrepared){
mediaPlayer.prepare();
}
mediaPlayer.seekTo(0);
mediaPlayer.start();
}
} catch(IllegalStateException ex){
ex.printStackTrace();
} catch(IOException ex){
ex.printStackTrace();
}
}
public void stop() {
mediaPlayer.stop();
synchronized(this){
isPrepared = false;
}
}
public void switchTracks(){
mediaPlayer.seekTo(0);
mediaPlayer.pause();
}
public void pause() {
mediaPlayer.pause();
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public boolean isLooping() {
return mediaPlayer.isLooping();
}
public void setLooping(boolean isLooping) {
mediaPlayer.setLooping(isLooping);
}
public void setVolume(float volumeLeft, float volumeRight) {
mediaPlayer.setVolume(volumeLeft, volumeRight);
}
public String getDuration() {
return String.valueOf((int)(mediaPlayer.getDuration()/1000));
}
public void dispose() {
if(mediaPlayer.isPlaying()){
stop();
}
mediaPlayer.release();
}
}
这是错误:
E/AndroidRuntime(21110): FATAL EXCEPTION: main
E/AndroidRuntime(21110): java.lang.RuntimeException: Unable to pause activity {com.floritfoto.apps.ave/com.floritfoto.apps.ave.OpenBird}: java.lang.IllegalStateException
E/AndroidRuntime(21110): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2718)
E/AndroidRuntime(21110): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2674)
E/AndroidRuntime(21110): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2652)
E/AndroidRuntime(21110): at android.app.ActivityThread.access$800(ActivityThread.java:127)
E/AndroidRuntime(21110): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1170)
E/AndroidRuntime(21110): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(21110): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(21110): at android.app.ActivityThread.main(ActivityThread.java:4507)
E/AndroidRuntime(21110): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(21110): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(21110): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
E/AndroidRuntime(21110): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
E/AndroidRuntime(21110): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(21110): Caused by: java.lang.IllegalStateException
E/AndroidRuntime(21110): at android.media.MediaPlayer.isPlaying(Native Method)
E/AndroidRuntime(21110): at com.floritfoto.apps.ave.Music.dispose(Music.java:101)
E/AndroidRuntime(21110): at com.floritfoto.apps.ave.OpenBird.onPause(OpenBird.java:211)
E/AndroidRuntime(21110): at android.app.Activity.performPause(Activity.java:4563)
E/AndroidRuntime(21110): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1198)
E/AndroidRuntime(21110): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2705)
E/AndroidRuntime(21110): ... 12 more
W/ActivityManager( 1864): Force finishing activity r.intent.getComponent().flattenToShortString()