how to play audio file in android

dIvYaNsH sInGh picture dIvYaNsH sInGh · Sep 3, 2011 · Viewed 95.3k times · Source

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?

Answer

Lalit Poptani picture Lalit Poptani · Sep 3, 2011

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();
    }
}