Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load a MediaPlayer from a Uri in a context assets

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());
        }
    }
}
like image 495
madmik3 Avatar asked May 03 '11 21:05

madmik3


People also ask

What does MediaPlayer release do?

MediaPlayer class can be used to control playback of audio/video files and streams.

What is the use of MediaPlayer class?

MediaPlayer Class in Android is used to play media files. Those are Audio and Video files. It can also be used to play audio or video streams over the network.

How do I play audio files on Android?

Prepare media file: To play a media file, you need to first prepare it i.e. you need to load the file for playback. Methods used for doing this are prepare(), prepareAsync(), and setDataSource(). Start/Pause the playback: After loading the media file, you can start playing the media file by using the start() method.

How does Android Media Player work?

You can play audio or video from media files stored in your application's resources (raw resources), from standalone files in the filesystem, or from a data stream arriving over a network connection, all using MediaPlayer APIs. Note: You can play back the audio data only to the standard output device.


1 Answers

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.

like image 125
Michael Avatar answered Oct 29 '22 21:10

Michael