seikida
seikida

Reputation: 407

mediaPlayer setDataSource doesn't work

I try to add a video on my Android application, like the page : MediaPlayer

But There are errors :

enter image description here

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

Answers (4)

Yor PG
Yor PG

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

zulu_papa
zulu_papa

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

Blackbelt
Blackbelt

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

Alex Kucherenko
Alex Kucherenko

Reputation: 20327

Be more attentive and read error message: "Unhandled exception: java.io.IOException". So, all you need is try-catch block

Upvotes: 1

Related Questions