5

I have an audio file in my assets directory. assets/audio/dance.mp3.

If I run context.getAssets().list("audio"); it shows up.

But when I try to use MediaPlayer.create(context,uri) it always fails and returns null.

none of this seems to work

private void tryLoad(String path,Context context)
{
    Uri uri = Uri.parse(path);
    this.audioPlayer =  MediaPlayer.create(context,uri);
    if (this.audioPlayer == null)
    {
        Log.d(TAG, "loadAudio: audioPlayer is null. current assets"+ uri.toString()) ;
    }
    else
    {
        Log.d(TAG, "loadAudio: WORKED"+ uri.toString()) ;
    }

}
public void loadAudio(Context context)
{
    if (this.audioPlayer != null)
        return;
    if (this.audioFile != null && this.audioFile.length() >0)
    {
        try 
        {
            tryLoad("/dance.mp3",context);
            tryLoad("dance.mp3",context);
            tryLoad("audio/dance.mp3",context);
            tryLoad("/audio/dance.mp3",context);
            tryLoad("assets/audio/dance.mp3",context);
            tryLoad("/assets/audio/dance.mp3",context);
            tryLoad("\\dance.mp3",context);
            tryLoad("dance.mp3",context);
            tryLoad("audio\\dance.mp3",context);
            tryLoad("\\audio\\dance.mp3",context);
            tryLoad("assets\\audio\\dance.mp3",context);
            tryLoad("\\assets\\audio\\dance.mp3",context);
        }   
        catch (Exception e)
        {
            Log.d(TAG, "loadAudio exception: " + e.getMessage());
        }
    }
}
madmik3
  • 6,975
  • 3
  • 38
  • 60

4 Answers4

3

I think you can do it because the MediaPlayer expects an URI and it's impossible to create an URI for an assets file. Try creating a MediaPlayer object and setting a data source to it using the MediaPlayer.setDataSource(FileDescriptor) method. You can get the FileDescriptor object using the AssetManager.openFd() method and then calling the AssetFileDescriptor.getFileDescriptor() method for the returned object.

I haven't tried this solution, so it's just an idea. But I hope it'll work.

Michael
  • 53,859
  • 22
  • 133
  • 139
  • oops.. didn't see your answer (just edited an old one) anyway - +1 for being faster :) – MByD May 17 '11 at 20:32
  • @madmik3 Hi, you can award a bounty to this answer if you think it's right. Otherwise your bounty may be just lost because it's not awarded automatically when you accept an answer. – Michael May 24 '11 at 05:01
1

you cannot call mp.setDataSource(afd.getFileDescriptor()); on an asset file as it will play all the assets. Instead follow the solution in this thread: Play audio file from the assets directory

Community
  • 1
  • 1
PhiKo
  • 11
  • 1
1

I have asked something like this question, look at this post MediaPlayer issue between raw folder and sdcard on android. I hope this help you

Community
  • 1
  • 1
George
  • 1,327
  • 11
  • 25
0

You need to put the file in the res/raw folder and then loading it with:

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);

From: Android documentation

Trasplazio Garzuglio
  • 3,535
  • 2
  • 25
  • 25
  • that requires that they files are hard coded and thats what I am trying to avoid. – madmik3 May 22 '11 at 14:47
  • sorry, this wasn't clear from your original question. Have you tried this instead: `mp = new MediaPlayer(); AssetFileDescriptor afd = getAssets().openFd("dance.mp3"); mp.setDataSource(afd.getFileDescriptor());` – Trasplazio Garzuglio May 22 '11 at 15:11