Skip to content

How to Play Mp3 File Programmatically

  • by

There are some cases where you need to play play mp3 file. Here it is achieving with the help of MediaPlayer Class. Here are the steps

Step 1: Create asset folder under java directory
Step 2: Save your mp3 file in asset

Step 3: write the following code in your activity class

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.name_of_the_mp3_file);  // instantiate 
                          mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  // set as media sound
                          mediaPlayer.setLooping(false); // disable repetition 
                          mediaPlayer.start();  // plays the mp3 sound

Leave a Reply

Your email address will not be published. Required fields are marked *