11

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.

Fran Verona
  • 5,438
  • 6
  • 46
  • 85
CalvinS
  • 615
  • 1
  • 7
  • 14

3 Answers3

21

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) { } 
Redax
  • 9,231
  • 6
  • 31
  • 39
  • 1
    This did it for me. Explicitly adding the offset and length. Strange that just giving the fd didn't work. – grebulon Feb 26 '14 at 08:56
6

Try this and see if any exceptions are caught:

try {
    MediaPlayer mp = new MediaPlayer();
    mp.setDataSource(this, uri);
}
catch (NullReferenceArgument e) {
    Log.d(TAG, "NullReferenceException: " + e.getMessage());
}
catch (IllegalStateException e) {
    Log.d(TAG, "IllegalStateException: " + e.getMessage());
}
catch (IOException e) {
    Log.d(TAG, "IOException: " + e.getMessage());
}
catch (IllegalArgumentException e) {
    Log.d(TAG, "IllegalArgumentException: " + e.getMessage());
}
catch (SecurityException e) {
    Log.d(TAG, "SecurityException: " + e.getMessage());
}

The exception caught will explain what is going wrong in your create. According the the docs, the static create method is just shorthand for what is in the try block above. The major difference that I can see is that the static method create doesn't throw while setDataSource does.

Jere.Jones
  • 9,915
  • 5
  • 35
  • 38
  • I added exception handling as you suggested and an mp.prepare() statement and I am getting this error on the prepare() 06-22 14:45:20.806: ERROR/PlayerDriver(554): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported 06-22 14:45:20.806: ERROR/MediaPlayer(857): error (1, -4) 06-22 14:45:20.987: DEBUG/dalvikvm(857): JDWP invocation returning with exceptObj=0x43744d68 06-22 14:45:33.056: DEBUG/ASSETTEST(857): IOException: Prepare failed.: status=0x1 0 6-22 14:45:43.068: WARN/System.err(857): java.io.IOException: Prepare failed.: status=0x1 Next? – CalvinS Jun 22 '10 at 14:52
  • 7
    I solved this by fixing the code to access the mp3 file in the assets. mp = new MediaPlayer(); AssetFileDescriptor afd = getAssets().openFd("song.mp3"); mp.setDataSource(afd.getFileDescriptor()); Thanks Jere! – CalvinS Jun 22 '10 at 16:11
0

You'd better try this on the devices running on Android N or the latest:

try {
    AssetFileDescriptor afd = getAssets().openFd("*.mp3 / *.mp4");
    player = new MediaPlayer();
    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });
    player.setDataSource(afd);
    player.prepareAsync();
    player.start();
} catch (...) {
}

else, do like the best answer below.

Rico
  • 71
  • 1
  • 8