enter image description here

I have an MP3 in my sdcard and I need to use its path for my setdatasource command. I have tried mp.setDataSource("/mnt/media_rw/sdcard/mymusic/thebomb.mp3"); along with many variations but it still won't work. And I don't think it has to do with the rest of the code.

How would I accomplish this?

有帮助吗?

解决方案 2

Use this way:

 Uri uri = Uri.parse(Environment.getExternalStorageDirectory()+"/mymusic/thebomb.mp3");
 musicPlayer.setDataSource(uri); 

Add permission to your manifest.xml file.

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

其他提示

I guess you are using Emulator.So the path of the SDCard can be get through Environment.getExternalStorageDirectory().Moreover your sdcard must contain that file.So you have to push your file in the SD card.

try this like...Simple mediaplayer play mp3 from file path?. Hope so it will help you.

 mpintro = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/intro.mp3"));
 mpintro.setLooping(true);
 mpintro.start();

Use the below code:

 musicPlayer.setDataSource("/sdcard/mymusic/thebomb.mp3"); 

Try with this -

mp.setDataSource(Environment.getExternalStorageDirectory()+"/mymusic/thebomb.mp3");

Try the following code and is working:

mp.setDataSource("/sdcard/mymusic/thebomb.mp3"); 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top