I have this piece of code:
try
{
mMusicPlayer.reset();
FileInputStream fis = new FileInputStream(pathToTitleFile);
mMusicPlayer.setDataSource(fis.getFD());
mMusicPlayer.prepareAsync();
}
catch (Exception e){}
When I skip a song, sometimes I get two Error/error(-38,0).
I've debugged it and the errors were thrown at the following code-section:
The error(-38,0) after the reset()-call. The Error(-38,0) after the prepareAsync()-call.
After this the player calls the onPrepare()-callback and jumps back and performs the try-block again. This repeats a few times before the song is played.
Strange thing is, that I don't get any other information about the error and error (-38,0) seems to be a "generic-error".
Based here MediaPlayer reset
Resets the MediaPlayer to its uninitialized state. After calling this method, you will have to initialize it again by setting the data source and calling prepare().
You need to initialize MediaPlayer again.
Related
I have searched too much here on stack overflow but I am unable to find any solution of my problem.
I am getting a Media Player Error named 'MEDIA_ERROR_IO' code -1004.
I am stream an audio from server everything works good. song prepared and then starts in onPrepared() method.
Now the problem comes when there is an incoming call and phone starts ringing, and I pauses the song by calling mediaPlayer.pause() from my BroadcastReceiver class.
when the Phone call ends start the audio again by calling mediaPlayer.start()
the audio starts but after 2-3 seconds it stops with an Error that is MediaPlayer error (1, -1004).
Now What should I do?
Any Help is appreciable.
Thank you.
the audio starts but after 2-3 seconds it stops with an Error that is
MediaPlayer error (1, -1004).
First, Lets understand what error (1, -1004) means. -1004 is the error code for IO error. Below reference from MediaPlayer.java source code.
/** File or network related operation errors. */
public static final int MEDIA_ERROR_IO = -1004;
This type of error comes if for some reason, the media player is not able to connect to the server due to network issues. It could be bad internet connectivity at that instance or some network related reason due which the media player was unable to connect to the server. There are some other similar error codes that the media player can throw like time-outs or server died:
/** Some operation takes too long to complete, usually more than 3-5 seconds. */
public static final int MEDIA_ERROR_TIMED_OUT = -110;
/** Media server died.*/
public static final int MEDIA_ERROR_SERVER_DIED = 100;
Now What should I do?
To handle errors generated by media player at run time, you should implement Error Listener. You can handle the error in which ever way as you like for example restart the player.
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
switch(extra){
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
// Do Something
// eg. reset the media player and restart
break;
case MediaPlayer.MEDIA_ERROR_IO:
// Do Something
// eg. Show dialog to user indicating bad connectivity
// or attempt to restart the player
break;
case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
//Do Something
//eg. Show dialog that there was error in connecting to the server
// or attempt some retries
break;
}
//You must always return true if you want the error listener to work
return true;
}
});
I have a ListView with videos as items. In some occasions I get an IllegalStateException while I try to set the surface.
Source code:
if(surfaceTexture != null){
mSurface = new Surface(surfaceTexture);
mMediaPlayer.setSurface(mSurface);
}
Exception:
Fatal Exception: java.lang.IllegalStateException
android.media.MediaPlayer._setVideoSurface (MediaPlayer.java)
android.media.MediaPlayer.setSurface (MediaPlayer.java:829)
However, the Android MediaPlayer docs state that setSurface can be called in any state.
setSurface
any
{}
This method can be called in any state and calling it does not change the object state.
(http://developer.android.com/reference/android/media/MediaPlayer.html)
I cannot reproduce this exception and I don't know in which state the MediaPlayer is during the exception.
Any Ideas?
This would occur if the player was not yet initialized or already released without your realizing it. The documentation for MediaPlayer doesn't seem to reflect the behavior I've experienced. I've encountered it throwing IllegalStateException on very early calls to setSurface where the mediaPlayer object is not null but likely not initialized and separately after release is called. To handle this I simply catch the exception:
try {
mediaPlayer.setSurface(surface);
} catch (Exception e) {
Log.i("TAG", "MediaPlayer setSurface failed.");
}
Since our callback that calls setSurface gets hit multiple times throughout the setup of playback, by the time start is called, the surface has been set again without throwing the exception.
I work with a MediaPlayer and set the state of the player often programmatically like for example:
if(mp.isPlaying()) {
mp.pause();
animationPausedMusic();
}
private void animationPausedMusic() {
// Changing button image to play button
btn_play.setBackgroundResource(R.drawable.play);
... // more code
}
But sometimes the logcat gives me the message:
"internal/external state mismatch corrected"
And then the play and pause function is not working anymore.
What does this message mean? And how can I solve it?
After going through the android's native framework for media player I found that in source file mediaplayer.cpp inside function bool MediaPlayer::isPlaying() The developer is checking if the currentState of media player is in STARTED state and yet the media player is not playing any media, so it tries to change the state to PAUSED state so that the state consistency should be maintained for API users.(and here is where he is printing the message "ALOGE("internal/external state mismatch corrected");")
Now If you go through the media player state diagram below:
You would notice that this may happen when the MediaPlayer moved to 'STARTED' state after a call to start() and at this time for some obscure reason has not yet started the playback and you fire a MediaPlayer.isPlaying() method call , The Framework treat this as state inconsistency and moves to 'PAUSED' state and that's why you cannot see anything playing further.
However, if someone has some better understanding please share your thoughts!
I ran into this recently, and like some other questions say, it's this bug (marked obsolete alas)
https://code.google.com/p/android/issues/detail?id=9732
I found this error occurs when playing a MIDI file, but only sometimes. It happens when mp.isPlaying() is called quickly after mp.start()
If you can manage to not call mp.isPlaying() for a little bit, the error doesn't occur. In my case, a 10th of a second or so made the difference between getting the error or not. It's awkward, but it works.
e.g.
//setting a new track
mp.setDataSource(path);
mp.prepare();
mp.start();
//calling mp.isPlaying() here or shortly after starts the problem
//since we know it's playing, we can store that state, or call
updateUiPlaying(); //eg instead of updateUi();
//or just call some code here that takes more time first
updateScaledImages(); //something that might take time
Log.v(TAG, "mp.isPlaying = " + mp.isPlaying()); //now isPlaying() shouldn't cause that error
Also, I put a check in when I pause later.
mp.pause()
if(mp.isPlaying()){
//shouldn't be playing, must be in error
mp.stop();
mp.release();
mp = new MediaPlayer();
//any other initialization here
}
Though the problem doesn't occur if there is a wait before calling isPlaying()
Apparently there is more than one cause of this message. The following solution worked for me. It may or may not work for you. I called the method MediaPlayer.reset() immediately after instantiating the MediaPlayer object:
MediaPlayer mp = new MediaPlayer();
mp.reset();
Im hoping you can provide some guidance. I created a 'final' instance of a mediaplayer (mp1) and on a button click it plays a mp3 file. I then need to stop the file when I click a second button. This works fine until I try to play the file again - nothing happens. I think that because the mp1 instance is 'final', when I stop it, it stops for good until I relaunch the app. I dont want to pause the file, I want to stop it and then restart it afresh. Any ideas welcome. I tried putting the mp1 creation within the button. This worked until the app crashed - probably because multiple mediaplayer creations used all the device memory?
Thanks!!!
// const mediaplayer
mp1 = MediaPlayer.create(getApplicationContext(), R.raw.mysound);
...
// in button 1
if (radSound1.isChecked()) {
radSound2.setChecked(false); // ...set radiobutton 2 to false
mp1.start(); // ...play the mp3 file
}
...
// in button 2
if (mp1 != null){
mp1.reset();
//mp1.setDataSource();
// mp1.prepare();
}
Follow the state diagram here. stop() stops playing and puts you in stopped state. Before starting to play for the next time, you should call prepare() and then start() again.
So in button 1, you should call start() and in button 2, you should call stop() and prepare(). The initialization is fine, do it once and keep it outside the buttons.
Also accept answers to your questions including this so that people like me will be more motivated to reply to them in the future.
I'm working on writing a small app that will stream mp3 files. I'm using the NPR code, but having a strange problem with mediaPlayer.prepareAsync().
I'm using a trimmed down version of the PlaybackService from the NPR app, which is getting started correctly. I am getting a reference to the service inside an OnClick handler inside an Activity, and calling listen() with the URL to the MP3 stream. The following (simplified) code is from my PlaybackService:
public void listen(String url) throws IllegalArgumentException, IllegalStateException, IOException {
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
}
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnInfoListener(this);
mediaPlayer.setOnPreparedListener(this);
synchronized (this) {
mediaPlayer.setDataSource(url);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
}
}
// ... lots of other code
#Override
public void onPrepared(MediaPlayer mp) {
Log.d(LOG_TAG, "Prepared");
play();
}
I have the other callbacks defined as well. I can see from LogCat that the MediaPlayer is loading the stream and buffering, as I see the following messages:
AwesomePlayer I calling prefetcher->prepare()
AwesomePlayer I prefetcher is done preparing
But my onPrepared method never gets called. If I add a timer and try to call play() on the MediaPlayer at some point after I see the above log messages, the media players plays, so it is indeed entering into the Prepared state.
If I replace the call to prepareAsync() with prepare(), the player just works. This is all on 2.2, which I have been reading seems to have some issues, but the problem I'm having doesn't seem related, as the stream works fine when prepare() is used.
I did notice that the Content-Length on the the stream is quite large (450MB), but since I can call play on the Media Player without getting an exception, it appears to be handling this OK.
The only other change is that in the NPR app, the service is being bound to and the playback started from inside a View object (while in my app, this happens inside an Activity).
Any thoughts on what I could be doing wrong?
Make sure you have created the mediaplayer in a thread that has a looper, which is required for the callbacks to work properly.