Android Dev: Playing a MP3 from assets

{lang: 'en-GB'}


Quick one today. I wanted to play an MP3 file from the assets folder. Here is the code:

MediaPlayer mp = new MediaPlayer();

AssetFileDescriptor descriptor;
descriptor = getAssets().openFd( "filename.mp3" );
mp.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength() );
descriptor.close();
mp.prepare();
mp.start();

All you need to do is open your project from your workspace and put your mp3 in the assets folder.

Simple.


Google+ Comments

Comments are closed.