Android

How to Play Mp3 File Programmatically

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
About author

Geethu is a teacher by profession, but she likes to research new things and is also interested in writing. Devdeeds is her blog, where she writes all the blog posts related to technology, gadgets, mobile apps, games, and related content.

Leave a Reply

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