Reputation: 1543
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
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
Reputation: 4926
Here's a step by step short tutorial:
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