tags:

views:

85

answers:

3

Hello

I'm new in Java/Android programming, so please have patience with me.

I try to play a mp3 which is locate und the assets folder. I know there is another way with the /res/raw/ folder, but use the assets-folder because later I'll try to access the file by String.

This code works to play a mp3-file:

        try
    {
        MediaPlayer mp = new MediaPlayer(); 
        FileDescriptor sfd = getAssets().openFd("song.mp3").getFileDescriptor(); 


        mp.setDataSource(sfd); 
        mp.prepare(); 
        mp.start();
    }
    catch(Exception e) {}

Now the problem: In the same assets-folder is another mp3 file stored. Though I specify the name of the mp3 to use it take the one which comes first in alphabet. E.g. the other file is named "music.mp3" it plays this one. Renaming it to "worldmusic.mp3" it will play "song.mp3". Rerename "worldmusic.mp3" back to "music.mp3" it will take this mp3 again. Another test: Renaming "song.mp3" to something other so the application can find whats specify by the code above will result that no song is played. So this means the songname have to exist, although it take arbitrary the song first in alphabet.

I'm testing with the AVD emulator of eclipse. But I think the behaviour would be the same on a real device.

Have someone an idea about this problem?

A: 

I don't believe using FileDescriptor is the right way to do this. Try using .create() and a Uri instead:

MediaPlayer mp = MediaPlayer.create(getBaseContext(), songUri);
mp.start();
kcoppock
A: 

I tested this:

    String songstring = "song.mp3";
    Uri songuri = Uri.parse(songstring);
    MediaPlayer mp = MediaPlayer.create(getBaseContext(), songuri);
    mp.start();

And shorter:

    MediaPlayer mp = MediaPlayer.create(getBaseContext(), Uri.parse("song.mp3"));
    mp.start();

In both cases the app crashes with error: "The application has stopped unexpectedly"

Some lines of the LogCat:

10-18 16:21:30.862: ERROR/AndroidRuntime(841): ERROR: thread attach failed

10-18 16:21:32.652: ERROR/PlayerDriver(31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported

10-18 16:21:32.652: ERROR/MediaPlayer(848): error (1, -4)

10-18 16:21:32.692: ERROR/AndroidRuntime(848): Uncaught handler: thread main exiting due to uncaught exception

AndroDevBeginner
A: 

I tried all variants of uri-parsing for the assets-folder I found on internet:

    MediaPlayer mp = MediaPlayer.create(getBaseContext(), Uri.parse("android.resource://[packagename]/assets/song.mp3"));
    MediaPlayer mp = MediaPlayer.create(getBaseContext(), Uri.parse("file:///assets/song.mp3"));
    MediaPlayer mp = MediaPlayer.create(getBaseContext(), Uri.parse("file:///android_asset/song.mp3"));

Nothing works for me

AndroDevBeginner