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.
- Please ensure the
\assets
folder is placed
correctly(\app\src\main\assets
) - Check your device volume level.
- 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
Reference One String from Another String in Strings.Xml
Android Spinner with Multiple Choice
The Completest Cocos2D-X Tutorial & Guide List
How to Automatically Update Application on Android
Remove Data from Notification Intent
Changing Viewpager to Enable Infinite Page Scrolling
Is There Any Way in Android to Get the Height of Virtual Keyboard of Device
Android Webview: Handling Orientation Changes
How to Make Toolbar Transparent
How to Create Jar for Android Library Project
How to Create a Circular Progressbar in Android Which Rotates on It
How to Capture the Android Device Screen Content
Sync Data Between Android App and Webserver
Webview Load HTML from Assets Directory
What Is the Size Limit for Logcat and How to Change Its Capacity
Android Turn On/Off Wifi Hotspot Programmatically
How to Escape Special Characters Like ' in SQLite in Android