开发者

Android Assets-Folder: it takes always the first file by alphabetical order

开发者 https://www.devze.com 2023-01-20 03:33 出处:网络
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 as

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?


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();


Not sure why, but the URI syntax doesn't seem to work for the assets. Try getting the AssetFileDescriptor instead, as answered in the related question:

Play audio file from the assets directory

0

精彩评论

暂无评论...
验证码 换一张
取 消