Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error creating MediaPlayer with Uri or file in assets

I copied song.mp3 to my project's assets directory and wrote this code:

private MediaPlayer mp;

Uri uri = Uri.parse("file:///android_asset/song.mp3");

mp=MediaPlayer.create(this, uri);

After running the create statement, the variable mp is null. What is wrong?

Thanks.

like image 575
CalvinS Avatar asked Jun 22 '10 13:06

CalvinS


1 Answers

Try this:

try {
    AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
    player = new MediaPlayer();
    player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
    player.prepare();
    player.start();
    } 
catch (IllegalArgumentException e) {    } 
catch (IllegalStateException e) { } 
catch (IOException e) { } 
like image 51
Redax Avatar answered Sep 20 '22 16:09

Redax