While I use almost the same code to play two videos, the first works perfectly, but the second does not. When I press the next video button, the program crashed with:
WARN/System.err(15726): java.lang.IllegalStateException
WARN/System.err(15726): at android.media.MediaPlayer.setDataSource(Native Method)
Source code:
the first play code:
mediaPlayer = new MediaPlayer();
playURI = receiveIntent.getStringExtra("playURI");
showDebugInfo("play uri "+playURI);
Log.e("Gplayer on create", "play uri "+playURI);
try {
mediaPlayer.setDataSource(playURI);
} catch (IllegalArgumentException e) {
Log.v(LOGTAG, e.getMessage());
finish();
} catch (IllegalStateException e) {
Log.v(LOGTAG, e.getMessage());
finish();
} catch (IOException e) {
Log.v(LOGTAG, e.getMessage());
finish();
}
when press next button it runs the following code:
String uri = listAdapter.getItem(position).getItem().getFirstResource().getValue();
showDebugInfo(" Uri "+uri);
if(mediaPlayer != null){
mediaPlayer.stop();
}
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(uri);
mediaPlayer.prepareAsync();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
finish();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
finish();
}
Why does this happen? and I check the two parts only the different that, the first one is new a mediaplayer and the second is let the mediaplayer stop and reset then the same.
I had the same problem.
You need to call mediaPlayer.reset(); before you are calling in the second video mediaPlayer.setDataSource(uri); the reason is, because the function mediaPlayer.setDataSource(uri); can be called only on idle state, and mediaPlayer.reset(); is the function that take you to idle state.
I understand it from this answer, even though the question is a bit different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With