MediaPlayer setDataSource 失败,状态 = 0x80000000 为 2.3.4 上的文件路径设置的铃声 [英] MediaPlayer setDataSource failed with status=0x80000000 for Ringtone set by filepath on 2.3.4
问题描述
标题说明了大部分内容.
Title says most of it.
我的应用程序一直在播放 uri 指向的铃声,例如 content://media/internal/audio/media/387
或 content://media/external/audio/media/1655
(我相信对于 SDcard 上的自定义铃声)同时使用 setDataSource(fileInfo)
和 setDataSource(mContext, Uri.parse(fileInfo))
.
My application has been playing ringtones pointed by uri like content://media/internal/audio/media/387
or content://media/external/audio/media/1655
(for custom ringtones on SDcard I believe) using both setDataSource(fileInfo)
and setDataSource(mContext, Uri.parse(fileInfo))
.
在每种情况下,我都收到了有关 setDataSource failed.: status=0x80000000
异常信息的日志,在使用 Android 4.x(不同版本)的手机上.
In each case I have received logs with information about setDataSource failed.: status=0x80000000
exception on phones using Android 4.x (different versions).
看到错误只发生在由内容 uri 指向的铃声,而不是由路径指向的单个文件,我决定使用铃声的路径以及解决上述手机上的问题(同时仍然使用 setDataSource(mContext, Uri.parse(fileInfo))
)
Seeing that the error happens only to ringtones pointed by content uri, but not to single files pointed by path, I have decided to use paths for ringtones as well which fixed problem on above phones (while still using setDataSource(mContext, Uri.parse(fileInfo))
)
然而,它在使用 Android 2.3.4-2.3.6 的手机上开始出现问题(虽然不是在我的 2.3.3 上):
It has however started problems on phones with Android 2.3.4-2.3.6 (not on mine 2.3.3 though):
- 我收到了一些异常的日志:
setDataSource failed.: status=0x80000000
对于路径如/system/media/audio/ringtones/TwirlAway.ogg
我还收到了关于
MediaPlayer.onErrorListener.onError(int what, int extra)
方法调用的日志,带有what=1
和extra=-2147483648
,据我所知,这表明该文件丢失或已损坏.但是我执行
- I have received few logs with exception:
setDataSource failed.: status=0x80000000
for files with paths like/system/media/audio/ringtones/TwirlAway.ogg
I have also received a log about
MediaPlayer.onErrorListener.onError(int what, int extra)
method call withwhat=1
andextra=-2147483648
, which, from what I know, suggest either that file is missing or it is corrupted. However I perform
File file = new File(fileInfo);
if (!file.exists())
检查这种情况并返回该文件确实存在 - 那么它是否已损坏?内存中的音乐文件极不可能.
check in such situation and it returned that file does exist - is it corrupted then? Highly unlikely for music file in internal memory.
总结:
- 使用
setDataSource("content://media/internal/audio/media/52")
- 抛出异常:
setDataSource failed.: status=0x80000000
forsetDataSource(mContext, "/system/media/audio/ringtones/TwirlAway.ogg")
有趣的是,setDataSource(Context context, Uri uri)
调用的setDataSource(Context context, Uri uri, Headers headers)
方法的前几行是(来自 2.3 的 GrepCode 源代码.4):
Interestingly, first few lines of setDataSource(Context context, Uri uri, Headers headers)
method which is called by setDataSource(Context context, Uri uri)
are (from GrepCode source for 2.3.4):
String scheme = uri.getScheme();
if(scheme == null || scheme.equals("file")) {
setDataSource(uri.getPath());
return;
}
所以,毕竟,它只是针对 setDataSource("/system/media/audio/ringtones/TwirlAway.ogg")
失败了.我已经通过使用从 uris 获取铃声的路径:
So, after all, it just fails for setDataSource("/system/media/audio/ringtones/TwirlAway.ogg")
. I have taken paths to ringtones from uris by using:
private static String getRingtonePathFromContentUri(Context context,
Uri contentUri) {
String[] proj = { MediaStore.Audio.Media.DATA };
Cursor ringtoneCursor = context.getContentResolver().query(contentUri,
proj, null, null, null);
ringtoneCursor.moveToFirst();
return ringtoneCursor.getString(ringtoneCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
}
任何想法可能导致错误抛出?也许这些是由于缺乏阅读权限引起的一些问题?我想本机 setDataSource(String path) 函数的源代码会有很大帮助,但我找不到它.
Any ideas what can be causing error throwing? Maybe those are some issues caused by lack of reading permissions? I guess source code for native setDataSource(String path) function would help a lot, but I wasn't able to find it.
推荐答案
下面 Lorne 的回答对处理这个问题最有帮助.
Answer by Lorne below was most helpful when dealing with this problem.
对于其他为此苦苦挣扎的人,这是我使用了 6 个月以上的代码,几乎不再报告错误.
For anyone else struggling with it, here is the code that I have been using for over 6 months now with errors almost not reported anymore.
fileinfo
可以是以下两者(示例):
fileinfo
can be both of below (examples):
/system/media/audio/alarms/Walk_in_the_forest.ogg
content://media/internal/audio/media/20
public static void setMediaPlayerDataSource(Context context,
MediaPlayer mp, String fileInfo) throws Exception {
if (fileInfo.startsWith("content://")) {
try {
Uri uri = Uri.parse(fileInfo);
fileInfo = getRingtonePathFromContentUri(context, uri);
} catch (Exception e) {
}
}
try {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
try {
setMediaPlayerDataSourcePreHoneyComb(context, mp, fileInfo);
} catch (Exception e) {
setMediaPlayerDataSourcePostHoneyComb(context, mp, fileInfo);
}
else
setMediaPlayerDataSourcePostHoneyComb(context, mp, fileInfo);
} catch (Exception e) {
try {
setMediaPlayerDataSourceUsingFileDescriptor(context, mp,
fileInfo);
} catch (Exception ee) {
String uri = getRingtoneUriFromPath(context, fileInfo);
mp.reset();
mp.setDataSource(uri);
}
}
}
private static void setMediaPlayerDataSourcePreHoneyComb(Context context,
MediaPlayer mp, String fileInfo) throws Exception {
mp.reset();
mp.setDataSource(fileInfo);
}
private static void setMediaPlayerDataSourcePostHoneyComb(Context context,
MediaPlayer mp, String fileInfo) throws Exception {
mp.reset();
mp.setDataSource(context, Uri.parse(Uri.encode(fileInfo)));
}
private static void setMediaPlayerDataSourceUsingFileDescriptor(
Context context, MediaPlayer mp, String fileInfo) throws Exception {
File file = new File(fileInfo);
FileInputStream inputStream = new FileInputStream(file);
mp.reset();
mp.setDataSource(inputStream.getFD());
inputStream.close();
}
private static String getRingtoneUriFromPath(Context context, String path) {
Uri ringtonesUri = MediaStore.Audio.Media.getContentUriForPath(path);
Cursor ringtoneCursor = context.getContentResolver().query(
ringtonesUri, null,
MediaStore.Audio.Media.DATA + "='" + path + "'", null, null);
ringtoneCursor.moveToFirst();
long id = ringtoneCursor.getLong(ringtoneCursor
.getColumnIndex(MediaStore.Audio.Media._ID));
ringtoneCursor.close();
if (!ringtonesUri.toString().endsWith(String.valueOf(id))) {
return ringtonesUri + "/" + id;
}
return ringtonesUri.toString();
}
public static String getRingtonePathFromContentUri(Context context,
Uri contentUri) {
String[] proj = { MediaStore.Audio.Media.DATA };
Cursor ringtoneCursor = context.getContentResolver().query(contentUri,
proj, null, null, null);
ringtoneCursor.moveToFirst();
String path = ringtoneCursor.getString(ringtoneCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
ringtoneCursor.close();
return path;
}
这篇关于MediaPlayer setDataSource 失败,状态 = 0x80000000 为 2.3.4 上的文件路径设置的铃声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!