Search code examples
javaandroidexceptionandroid-mediaplayer

MediaPlayer - java.io.FileNotFoundException: No content provider


I've been writing an mp3 player for quite some time, but for some reason this exception:

W/MediaPlayer: Couldn't open /storage/emulated/0/Music/generic music file.mp3: java.io.FileNotFoundException: No content provider: /storage/emulated/0/Music/generic music file.mp3

keeps popping up every time I try to play any song.

The way I retrieve path to a song is:

file.getAbsolutePath()

where file is a File instance. And the way I play the song is:

try {
    mediaPlayer = MediaPlayer.create(this, Uri.parse(currentTrack.getPath()));
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            nextTrack();
        }
    });
} catch (Exception ex) {
    ex.printStackTrace();
}

where this is a reference to an instance of a class, which extends Service.

Any ideas?


Solution

  • Presumably, currentTrack has a File object. If so, replace Uri.parse(currentTrack.getPath()) with currentTrack.getUri(), where you implement getUri() to return the value of Uri.fromFile() for the File.

    This solves your immediate problem, which is that you have created an invalid Uri, as it has no scheme. It also sets you up to deal with Uri types that are not files (e.g., content Uri values) that you may wind up needing in the future.