I'm currently attempting to make a media player for audio. I'm currently running lollipop. I ran into a problem setting the dataSource for the media player. First, here's how I set the dataSource:
public void playSong() { player.reset(); Song selSong = songs.get(songPos); long currSong = selSong.getId(); //Get Uri of song Uri trackUri = ContentUris.withAppendedId( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, currSong); try { //Tell the player the song to play player.setDataSource(getApplicationContext(), trackUri); } catch (Exception e) { Log.e("MUSIC SERVICE", "Error setting data source", e); Log.d("URI Path", trackUri.toString()); } //Will prepare the song and call onPrepare of player player.prepareAsync(); }
And the Uri comes out to this:
content://media/external/audio/media/22
I did some research, and from my understanding, after Android 4.1, you can no longer use a URI for the dataSource. When I run this app with the code above, I'll get this error:
E/MediaPlayer: Unable to create media player E/MUSIC SERVICE: Error setting data source java.io.IOException: setDataSource failed.: status=0x80000000 at android.media.MediaPlayer.nativeSetDataSource(Native Method)
So now I need to convert the URI to a file path and provide that as the dataSource. And, after more research, it appears kitkat changed the way URI's are provided so its difficult to get the file path from the URI. However, I'm not sure if this change persisted into Android Lollipop 5.0.2.
Essentially, I have the URI of a song, but I need to provide something other than a URI to a dataSource. Is there any way that I can convert the URI on Lollipop, and, if not, how can I provide the dataSource only knowing the song's id? Thanks.