Play Audio File from the Assets Directory

Play audio file from the assets directory


player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());

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)

Can you play a mp3 file from the assets folder?

You can do it in another way too. Put the .mp3 files under res/Raw folder and use the following code:

MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.android);
mediaPlayer.start();

How to play a WAV file loaded from Assets folder in Android?

Your code is ok, i checked, its working.

  1. Please ensure the \assets folder is placed
    correctly(\app\src\main\assets)
  2. Check your device volume level.
  3. Play success.wav in PC media player and ensure it is audible.

Note:
Using device volume controls:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

If your app is media related, use setVolumeControlStream API at your onResume() of activity or fragment and use device volume hard keys to increase/decrease volume. This set the application to only modify stream_music volume /media volume, otherwise it will modifiy ring volume.
Ref:https://developer.android.com/guide/topics/media-apps/volume-and-earphones

Play media files located in assets folder

I've this method that returns the all files by extension in a folder inside asset folder:

public static String[] getAllFilesInAssetByExtension(Context context, String path, String extension){
Assert.assertNotNull(context);

try {
String[] files = context.getAssets().list(path);

if(StringHelper.isNullOrEmpty(extension)){
return files;
}

List<String> filesWithExtension = new ArrayList<String>();

for(String file : files){
if(file.endsWith(extension)){
filesWithExtension.add(file);
}
}

return filesWithExtension.toArray(new String[filesWithExtension.size()]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

if you call it using:

getAllFilesInAssetByExtension(yourcontext, "", ".mp3");

this will return all my mp3 files in the root of assets folder.

if you call it using:

getAllFilesInAssetByExtension(yourcontext, "somefolder", ".mp3");

this will search in "somefolder" for mp3 files

Now that you have list all files to open you will need this:

AssetFileDescriptor descriptor = getAssets().openFd("myfile");

To play the file just do:

MediaPlayer player = new MediaPlayer();

long start = descriptor.getStartOffset();
long end = descriptor.getLength();

player.setDataSource(this.descriptor.getFileDescriptor(), start, end);
player.prepare();

player.setVolume(1.0f, 1.0f);
player.start();

Hope this helps

Play audio-file from assets

Ok, now it works

I've added this to the helper file:

def audio_path(source, options = {})
path_to_asset(source, {type: :audio}.merge!(options))
end

and changed the code in the view-file to this:

<audio src="<%= audio_path 'jean.mp3'%>" type="audio/mpeg" controls>
Your browser does not support the audio element.
</audio>


Related Topics



Leave a reply



Submit