I have made/downloaded a Java mediaplayer class for Android that is working well, except that at screen timeout or when turning it off by hand, when resuming, I get an IllegalStateException when trying a mediaPlayer.isPlaying()
.
Googling I found that this is probably the mediaplayer being in error state. I then tried try {...} catch(IllegalStateException...
thing, but I am adding too many, and the thing is being a big mess out of control (and without success).
Then I tried something like:
mediaPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
mp.release();
return false;
}
but it is not working, still getting the IllegalStateException error. (EDIT: BTW, here it says that " IllegalStateExceptions don't trigger the OnErrorListener call". Is this correct??)
So, my question(s):
1) What is happening at screen timeout that puts mediaplayer into error state? 2) How can I prevent this from happening? 3) What do I need to put inside the mediaPlayer.setOnErrorListener? 4) Did I miss something for the error listener, need to handle it somehow?
Sorry... newbie here.
EDIT: I was required to add the call, so here is the whole class:
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();
}
}
And this is the error:
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()