Reputation: 407
I try to add a video on my Android application, like the page : MediaPlayer
But There are errors :
For example, why this line doesn7t work :
mediaPlayer.setDataSource(url);
And this line too :
mediaPlayer.prepare(); // might take long! (for buffering, etc)
I don't understand because it's the developer.android.com example. Can you help me to make understand.
Thank you (and if my question is stupid, I am sorry).
Upvotes: 2
Views: 972
Reputation: 1
Try this:
if(mediaPlayer != null ){
if (mediaPlayer.isPlaying()){mediaPlayer.stop();}
mediaPlayer.reset(); //this line is important!
String path = File.separator + "sdcard" + File.separator + utilsFields.repoDirRoot + File.separator + media.mp4;
try {
mediaPlayer.setDataSource(path);
}catch (Exception ignored){}
try {
mediaPlayer.prepare();
}catch (Exception ignored){}
mediaPlayer.start();
}
Upvotes: 0
Reputation: 415
As other two answers suggest, your code needs to be surrounded with try/catch block. Also, if it is fetched from the web it would be in best practice to use
mediaPlayer.prepareAsync();
Hope it helped, hope that your program works. However, a few minutes later i found out that prepareAsync() method is not recommended for playing mp3 files
Upvotes: 0
Reputation: 157487
setDataSource(String) throws IOException
and IllegalArgumentException
that you are not catching. You should wrap the two calls around a try-catch
block
Upvotes: 3
Reputation: 20327
Be more attentive and read error message: "Unhandled exception: java.io.IOException". So, all you need is try-catch block
Upvotes: 1