Skip to content Skip to sidebar Skip to footer

Read Audio File From Assets On Android

I am trying to play audio files stored in the assets directory, but before I even read a file I get the error 'error occured java.io.FileNotFoundException: track.mp3'. Here's how I

Solution 1:

I think this code should work for you:

AssetFileDescriptorafd= getAssets().openFd("track.mp3");

MediaPlayerplayer=newMediaPlayer();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
player.prepare();
player.start();

Otherwise, check if the file is in the right folder and the name is "track.mp3".

Solution 2:

Right-click your "track.mp3" file, with the "Refactor" > "Move" command, move your file to another directory (e.g. "res"). With this same command, move it back to your "assets" directory. For me, it works.

Solution 3:

Android guidelines suggest for Audio use res/raw folder. So make sure that all audio are there and when you read you can use this method:

voidplayAudio(Context mContext, String sound){
    newAudioPlayer().play(mContext, mContext.getResources().getIdentifier(
            sound,"raw", mContext.getPackageName()));
}

And if you need something line audio player then take a look at MediaPlayer

Solution 4:

Use this code

MediaPlayermediaPlayer=newMediaPlayer();
            AssetFileDescriptor afd;
            try {
                afd = getAssets().openFd("1.mp3");
                mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (IOException e) {
                          Toast.makeText(LearnActivity.this, "Error Playing audio from Asset directory",Toast.LENGTH_SHORT).show();
            }

Otherwise, check if the file is in the right folder and the name is "track.mp3".

Post a Comment for "Read Audio File From Assets On Android"