1

I am quite new to Android-Development. I have troubles to access my songs, stored in the Music folder. From adb shell the path is: /storage/emulated/legacy/Music. Following code causes the exception:

try {
    Log.i("path", Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+Environment.DIRECTORY_MUSIC + "/" + name + ".mp3");
    mediaPlayer.setDataSource(getApplicationContext(),
    Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()
                                + "/"
                                + Environment.DIRECTORY_MUSIC
                                + "/"
                                + name + ".mp3"));
    mediaPlayer.prepare();
    mediaPlayer.start();
}

Here is the exception:

I/path: /storage/emulated/0/Music/A Wunder.mp3
java.io.IOException: setDataSource failed.

Any help appreciated!

EDIT: It seems that i have problems with some of the files. My "Player" can‘t open all of them, but other player can. So here is the code that works for me:

    mediaPlayer.setDataSource(getApplicationContext(),
                         Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/" + name + ".mp3"));
mendias
  • 11
  • 3
  • According to the error, you aren't accessing `/storage/emulated/legacy/Music` – OneCricketeer Jan 01 '16 at 19:59
  • Already answered here: http://stackoverflow.com/questions/9657280/mediaplayer-setdatasource-causes-ioexception-for-valid-file – Viral Patel Jan 01 '16 at 20:04
  • try (getCanonicalPath()) intead of (getAbsolutePath()) --http://stackoverflow.com/questions/1099300/whats-the-difference-between-getpath-getabsolutepath-and-getcanonicalpath -- you never know – Tasos Jan 01 '16 at 20:14
  • this should work (if the file is valid) : `Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/" + name + ".mp3";` – Mark Jan 01 '16 at 20:17

0 Answers0