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();
private void startSound(String filename){
AssetFileDescriptor afd = getAssets().openFd(filename);
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
player.prepare();
player.start();
}
You can use the following code to play a specific audio file (i.e your_file.mp3
) in your assets folder with:
MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor afd = context.getAssets().openFd("your_file.mp3");
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
mediaPlayer.start();
And to play a raw resource file (i.e your_file
) with:
MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.your_file));
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
mediaPlayer.start();