views:

105

answers:

1

I have the following code:

    AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
    player = new MediaPlayer();
    player.setDataSource(afd.getFileDescriptor());
    player.prepare();
    player.start();

The problem is that, when I run this code, it starts playing all the audio files in the assets directory, in alphabetical order instead of just playing the audio file I requested. What am I doing wrong? Is there a better way to play audio files from the assets directory?

Follow-up question: Is there a difference between keeping audio files in the assets directory and keeping them in the res/raw directory? Besides the fact that they don't get ids if they are in the assets directory. If I move the audio files to the res/raw folder then I have a problem with reusing MediaPlayers because there is no id parameter for setDataSource(). I can't find a good guideline for handling this kind of problem.

A: 
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
Sarwar Erfan
It worked. Thanks. But I think my version SHOULD have also worked.
kaciula
Your version would work if you had only one file in the assets directory.The asset directory contents are not actually 'real files' on disk. All of them are put together one after another. So, if you do not specify where to start and how many bytes to read, the player will read up to the end (that is, will keep playing all the files in assets directory)
Sarwar Erfan