替代解决方案 #1:使用 Resources.getIdentifier()
为什么不使用 getResources().getIdentifier() 来获取资源的 id 并像往常一样使用静态 MediaPlayer.create() 呢?
public int getIdentifier (String name, String defType, String defPackage)
getIdentifier() 获取您的资源名称(test0)、资源类型(raw)、您的包名称并返回实际的资源 ID。
MediaPlayer mp;
//String filename = "android.resource://" + this.getPackageName() + "/raw/test0";
mp=MediaPlayer.create(getApplicationContext(), getResources().getIdentifier("test0","raw",getPackageName()));
mp.start();
我已经测试了这段代码,它可以工作。
更新 #1:
替代解决方案 #2:使用 Uri.parse()
我也测试了这段代码,它也可以工作。将您的资源路径作为 URI 传递给 setDataSource()。我刚刚对您的代码进行了更改以使其正常工作。
String filename = "android.resource://" + this.getPackageName() + "/raw/test0";
mp = new MediaPlayer();
try { mp.setDataSource(this,Uri.parse(filename)); } catch (Exception e) {}
try { mp.prepare(); } catch (Exception e) {}
mp.start();
更新 #2:答案是否定的
关于 setDataSource(String) 调用
看到您的评论后,您似乎确实希望将 setDataSource(string) 用于您的目的。我不明白为什么。但是,我认为,出于某种原因,您正试图避免使用“上下文”。如果不是这种情况,那么上述两种解决方案应该非常适合您,或者如果您试图避免上下文,恐怕使用签名 setDataSource(String) 调用的函数是不可能的。原因如下,
MediaPlayer setDataSource() 函数有以下这些选项,您只对 setDataSource(String) 感兴趣,
setDataSource(String) 在内部调用 setDataSource(String path, String[] keys, String[] values) 函数。如果你可以查看它的source,
public void setDataSource(String path)
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
setDataSource(path, null, null);
}
如果您检查 setDataSource(String path, String[] keys, String[] values) 代码,您将看到以下条件根据其方案过滤路径,特别是如果它是“文件”方案,它会调用 setDataSource( FileDescriptor)或者如果方案不是“文件”,它会调用本机 JNI 媒体函数。
{
final Uri uri = Uri.parse(path);
final String scheme = uri.getScheme();
if ("file".equals(scheme)) {
path = uri.getPath();
} else if (scheme != null) {
// handle non-file sources
nativeSetDataSource(
MediaHTTPService.createHttpServiceBinderIfNecessary(path),
path,
keys,
values);
return;
}
final File file = new File(path);
if (file.exists()) {
FileInputStream is = new FileInputStream(file);
FileDescriptor fd = is.getFD();
setDataSource(fd);
is.close();
} else {
throw new IOException("setDataSource failed.");
}
}
在上面的代码中,你的资源文件 URI 方案不会为空 (android.resource://) 并且 setDataSource(String) 将尝试使用原生 JNI 函数 nativeSetDataSource() 认为你的路径是 http/ https/rtsp 显然该调用也会失败而不会引发任何异常。这就是为什么您对 setDataSource(String) 的调用无异常地转义并使用以下异常进行 prepare() 调用。
Prepare failed.: status=0x1
所以 setDataSource(String) 覆盖无法处理您的资源文件。您需要为此选择另一个覆盖。
另一方面,检查 setDataSource(Context context, Uri uri) 使用的 setDataSource(Context context, Uri uri, Map headers),它使用 AssetFileDescriptor, ContentResolver from your context 和 openAssetFileDescriptor 打开成功的 URI因为 openAssetFileDescriptor() 可以打开您的资源文件,最后生成的 fd 用于调用 setDataSource(FileDescriptor) 覆盖。
AssetFileDescriptor fd = null;
try {
ContentResolver resolver = context.getContentResolver();
fd = resolver.openAssetFileDescriptor(uri, "r");
// :
// :
// :
if (fd.getDeclaredLength() < 0) {
setDataSource(fd.getFileDescriptor());
} else {
setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength());
}
总而言之,您不能像使用资源 mp3 文件一样使用 setDataSource(String) 覆盖。相反,如果您想使用字符串播放资源文件,您可以使用 MediaPlayer.create() 静态函数和上面给出的 getIdentifier() 或 Update#1 中给出的 setDataSource(context,uri)。
更多理解请参考完整源代码:Android MediaPlayer
更新 #3:
openFrameworks setDataSource(String):
正如我在下面的 cmets 中提到的,openFrameworks 使用 android MediaPlayer 代码 asis。如果可以参考第4行,
import android.media.MediaPlayer;
和线号:26、27、28 和 218
player = new MediaPlayer(); //26
player.setDataSource(fileName); //27
player.prepare(); //28
private MediaPlayer player; //218
因此,如果您尝试使用 openFrameworks 将 ardroid.resource//+ this.getPackageName() + "raw/test0" 传递给 setDataSource(),您仍然会遇到与我相同的异常在更新#2 中解释。话虽如此,我只是碰碰运气,在 Google 上搜索以确认我在说什么,并找到了 openFrameworks forum link,其中一位 openFrameworks 核心开发人员 arturo 说,
不知道 mediaPlayer 是如何工作的,但一切都在 res/raw 中
或 bin/data 被复制到 /sdcard/cc.openframeworks.packagename
根据该评论,您可以尝试在 setDataSource() 中使用复制的路径。在 MediaPlayer 的 setDataSource(String) 上使用资源文件是不可能的,因为它不能接受资源文件路径。请注意,我说“资源文件路径”以 android.resource// 方案开头,它实际上是一个 jar 位置(在您的 apk 中),而不是物理位置。本地文件将使用以 file:// 开头的 setDataSource(String)。
为了让您清楚地了解您要对资源文件执行的操作,请尝试执行以下代码并在 logcat 中查看结果,
try{
Log.d("RESURI", this.getClass().getClassLoader().getResource("res/raw/test0").toURI().toString());
}
catch(Exception e) {
}
你会得到这样的结果,
jar:file:/data/app/<packagename>/<apkname>.apk!/res/raw/test0
这是为了向您展示您尝试访问的资源文件实际上不是物理路径中的文件,而是您无法使用 setDataSource(String) 方法访问的 jar 位置(在 apk 中)。 (尝试使用 7zip 解压您的 apk 文件,您将在其中看到 res/raw/test0)。
希望对您有所帮助。
PS:我知道它有点冗长的答案,但我希望这能详细解释它。如果可以帮助其他人,请将替代解决方案留在顶部。