MediaPlayer-java.io.FileNotFoundException:没有内容提供者 [英] MediaPlayer - java.io.FileNotFoundException: No content provider
问题描述
我已经写了mp3播放器已经有一段时间了,但是出于某种原因这个例外:
I've been writing an mp3 player for quite some time, but for some reason this exception:
W / MediaPlayer:可能打开/存储/仿真/ 0 /音乐/通用音乐
file.mp3:java.io.FileNotFoundException:无内容提供者:
/存储/仿真/ 0 /音乐/通用音乐file.mp3
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
每次我尝试播放 any 歌曲时都会弹出提示音。
keeps popping up every time I try to play any song.
我检索歌曲路径的方法是:
The way I retrieve path to a song is:
file.getAbsolutePath()
其中 file 是一个File实例。我播放歌曲的方式是:
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();
}
其中 this 是对以下实例的引用一个类,扩展了 Service 。
where this is a reference to an instance of a class, which extends Service.
有什么想法吗?
推荐答案
大概是 currentTrack
有一个 File
对象。如果是这样,请将 Uri.parse(currentTrack.getPath())
替换为 currentTrack.getUri()
,在此实现 getUri()
返回 File的
。 Uri.fromFile()
的值
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
.
这解决了您的直接问题,即您创建了无效的 Uri
,因为它没有计划。它还使您可以处理不是文件的 Uri
类型(例如,内容
Uri
值),您将来可能会需要。
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.
这篇关于MediaPlayer-java.io.FileNotFoundException:没有内容提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!