Android: Play sound or Mp3 file from Assets


Hi,

I spend lots of time to play sound in Android Application.
Basically I wanted to play a sound file from android Assets, rather than from SDCard!

it was a small code like this:

MediaPlayer m = new MediaPlayer();
   try{
	   AssetFileDescriptor descriptor = AmbeMaAartiActivity.this.getAssets().openFd("myfile.mp3");
	   m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength() );
	   descriptor.close();
	   m.prepare();
	   m.start();
   } catch(Exception e){
	   // handle error here.. 
   }

Cheers!!

Advertisements

3 thoughts on “Android: Play sound or Mp3 file from Assets

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s