的MediaPlayer的setDataSource失败,状态= 0x80000000的的铃声通过文件路径上2.3.4设置路径、状态、文件、铃声

2023-09-05 08:53:40 作者:好看的微信号设计英文 起个漂亮的微信号英文

标题说大部分。

我的应用程序一直玩指出通过URI如铃声内容:// /内部/音频/媒体/ 387 内容://媒体/外部/音频/媒体/ 1655 (上SD卡,我相信自定义铃声)同时使用的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失败:状态= 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失败:状态= 0x80000000的与路径的文件,如 /系统/媒体/音频/铃声/TwirlAway.ogg

我还收到一个日志关于 MediaPlayer.onErrorListener.onError(INT什么,整型附加)方法调用么= 1 额外= -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 with what=1 and extra=-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(内容:// /内部/音频/媒体/ 52) 抛出异常:的setDataSource失败:状态= 0x80000000的的setDataSource(mContext,/system/media/audio/ringtones/TwirlAway.ogg ) works with setDataSource("content://media/internal/audio/media/52") throws exception: setDataSource failed.: status=0x80000000 for setDataSource(mContext, "/system/media/audio/ringtones/TwirlAway.ogg")

有趣的是,前几行的setDataSource的(上下文的背景下,开放的URI,头头)方法被称为由的setDataSource(上下文的背景下,乌里URI)是(from grep的code源代码2.3.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(/系统/媒体/音频/铃声/ TwirlAway.ogg)。我已经采取的路径从URI的铃声使用:

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));
}

任何想法可以导致错误引发?也许这些都是缺乏所引起的阅读权限的一些问题? 我猜源$ C ​​$ C本机的setDataSource(字符串路径)功能将有很大的帮助,但我没能找到它。

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.

推荐答案

由洛恩回答时,处理这个问题下面是最有帮助的。

Answer by Lorne below was most helpful when dealing with this problem.

有关别人用它挣扎着,这里是code我一直在使用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

内容:// /内部/音频/媒体/ 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;
}