64

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?

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
dIvYaNsH sInGh
  • 1,943
  • 3
  • 21
  • 40
  • You may also put `xyz.mp3` into `res/raw/` and then reference it via `R.raw.xyz`. However, then you have two choices: `MediaPlayer` and `SoundPool`. For efficient memory management, you should use a library to work with those classes: https://github.com/delight-im/Android-Audio – caw Apr 01 '15 at 22:44

5 Answers5

125

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();
    }
}
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
18

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();
A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
11

@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: MediaPlayer issue between raw folder and sdcard on android

Community
  • 1
  • 1
Suv
  • 121
  • 1
  • 3
  • If you let me know why -1, I will learn whats wrong with the above answer? – Suv Feb 16 '15 at 06:19
  • 2
    Probably the -1 is because you posted an answer to the original question (i.e. how to play an audio file); if you wanted to reply to Niranjan's comment, you should have added a comment yourself, under Lalit's answer – ocramot Mar 30 '15 at 13:10
  • Hmmm......They should have told me that in the comment, I would have changed it......anyway, thanks for letting me know........ – Suv Mar 31 '15 at 13:38
  • I'd rather see: try { mp.setDataSource(context, Uri.parse("android.resource://" + context.getPackageName() +"/res/raw/urmp3name")); } catch (IOException e) { e.printStackTrace(); } – jobbert Jun 07 '18 at 06:44
4
    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();
    }

}
Venki WAR
  • 1,997
  • 4
  • 25
  • 38
Naren
  • 99
  • 3
1

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

ilidiocn
  • 323
  • 2
  • 5