how to play audio file in android

AndroidAudioAndroid WidgetMedia Player

Android Problem Overview


I have a mp3 file in my android mobile, lets it's a xyz.mp3 somewhere in my sdcard. How to play it through my application?

Android Solutions


Solution 1 - Android

Simply you can use MediaPlayer and play the audio file. Check out this nice example for playing Audio:

 public void audioPlayer(String path, String fileName){
    //set up MediaPlayer    
    MediaPlayer mp = new MediaPlayer();
 
    try {
        mp.setDataSource(path + File.separator + fileName);
        mp.prepare();
        mp.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Solution 2 - Android

If the audio is in the local raw resource:

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start(); // no need to call prepare(); create() does that for you

To play from a URI available locally in the system:

Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();

Solution 3 - Android

@Niranjan, If you are using a raw file from res/raw folder, ie., reading a file stored inside the project, we can use:

mediaplayer.setDataSource(context, Uri.parse("android.resource://urpackagename/res/raw/urmp3name");

If you have to use from SD card:

 MediaPlayer mediaPlayer = new MediaPlayer();
 File path = android.os.Environment.getExternalStorageDirectory();
 mediaPlayer.setDataSource(path + "urmp3filename");

See this related question: https://stackoverflow.com/questions/6018153/media-player-problem-between-raw-folder-and-sdcard-on-android

Solution 4 - Android

    public class MainActivity extends Activity implements OnClickListener {
	Button play;
	MediaPlayer mp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        play=(Button)findViewById(R.id.button1);
        play.setOnClickListener(this);
        
    }
	@Override
	public void onClick(View arg0)
	{
		mp=MediaPlayer.create(getApplicationContext(),R.raw.song);// the song is a filename which i have pasted inside a folder **raw** created under the **res** folder.//
		mp.start();
		
		
	}
	
	@Override
	protected void onDestroy() {
		mp.release();
		super.onDestroy();
	}
    
}

Solution 5 - Android

The replay from https://stackoverflow.com/users/726863/lalit-poptani is great one, it worked the first time, but as I used to have the full path of the file, I did it this way

public void audioPlayer(String path){
        //set up MediaPlayer
        MediaPlayer mp = new MediaPlayer();

        try {
            mp.setDataSource(path );
            mp.prepare();
            mp.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Credit to http://www.helloandroid.com/tutorials/how-play-video-and-audio-android

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondIvYaNsH sInGhView Question on Stackoverflow
Solution 1 - AndroidLalit PoptaniView Answer on Stackoverflow
Solution 2 - AndroidA-SharabianiView Answer on Stackoverflow
Solution 3 - AndroidSuvView Answer on Stackoverflow
Solution 4 - AndroidNarenView Answer on Stackoverflow
Solution 5 - AndroidilidiocnView Answer on Stackoverflow