Search code examples
javaandroidandroid-mediaplayerandroid-resourcesandroid-audiomanager

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?.


Solution

  • 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"/>