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

Rojer is a programmer by profession, but he likes to research new things and is also interested in writing. Devdeeds is his blog, where he 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 *