Mark F
Mark F

Reputation: 1543

Audio File Path On Android Device

I have an mp3 file on my Galaxy S6 called song. I have a simple app with one Button that when clicked, I'd like my song file to play.

How can I get the path of that song and assign in to my MediaPlayer object?.

Upvotes: 1

Views: 3661

Answers (2)

Mark F
Mark F

Reputation: 1543

I made a short video here showing how you can set a piece of audio already stored on your phone to a MediaPlayer object. Hope it helps.

Upvotes: 0

Here's a step by step short tutorial:

  • 1st - Create a "raw" directory in your res folder.
  • 2nd - Put your mp3 file inside that directory (res/raw/mySong.mp3).
  • 3rd - In your onCreate method put the following code:

    MediaPlayer mp = MediaPlayer.create(this,R.raw.mySong);
    mp.start();
    

If you want to access an external file:

MediaPlayer mp = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/mySong.mp3")););
mp.start();

And ofc, don't forget the permissions:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Upvotes: 3

Related Questions