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

时间:2013-05-06 09:17:10

标签: android android-mediaplayer ringtone

标题说的大部分都是。

我的应用程序一直在使用content://media/internal/audio/media/387content://media/external/audio/media/1655(我相信SD卡上的自定义铃声)使用setDataSource(fileInfo)setDataSource(mContext, Uri.parse(fileInfo))播放铃声指向的铃声。

在每种情况下,我都会收到有关使用Android 4.x(不同版本)的手机上setDataSource failed.: status=0x80000000例外信息的日志。

看到错误仅发生在内容uri指向的铃声上,而不是路径指向的单个文件,我已经决定使用铃声路径来修复上述手机上的问题(同时仍使用setDataSource(mContext, Uri.parse(fileInfo))

然而,它已经在Android 2.3.4-2.3.6手机上出现了问题(不是我的2.3.3):

  • 我收到的日志很少,例外情况为setDataSource failed.: status=0x80000000,其中包含/system/media/audio/ringtones/TwirlAway.ogg
  • 等路径的文件
  • 我还收到了MediaPlayer.onErrorListener.onError(int what, int extra) what=1方法调用日志extra=-2147483648File file = new File(fileInfo); if (!file.exists()) ,据我所知,该文件显示该文件丢失或已损坏。但是我执行

    setDataSource("content://media/internal/audio/media/52")

检查这种情况并返回该文件确实存在 - 它是否已损坏?内部存储器中的音乐文件极不可能。

总结一下:

  • 适用于setDataSource failed.: status=0x80000000
  • 引发例外setDataSource(mContext, "/system/media/audio/ringtones/TwirlAway.ogg") setDataSource(Context context, Uri uri, Headers headers)

有趣的是,由setDataSource(Context context, Uri uri)调用的 String scheme = uri.getScheme(); if(scheme == null || scheme.equals("file")) { setDataSource(uri.getPath()); return; } 方法的前几行是(from GrepCode source for 2.3.4):

setDataSource("/system/media/audio/ringtones/TwirlAway.ogg")

所以,毕竟,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)); } 失败了。我使用以下方法从uris采取了铃声的路径:

{{1}}

任何可能导致错误抛出的想法?也许那些是缺乏阅读权限导致的一些问题? 我想原生setDataSource(String path)函数的源代码会有很多帮助,但我找不到它。

6 个答案:

答案 0 :(得分:11)

在处理此问题时,Lorne的答案最有帮助。

对于任何正在努力解决它的人来说,这里是我已经使用超过6个月的代码,现在几乎没有报告错误了。

fileinfo可以是以下两个(示例):

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

答案 1 :(得分:4)

由于Android 4.1.1中的错误修复,setDataSource(String path)的行为发生了变化。在4.1.1或更高版本中,您需要使用本地路径(不使用协议)。但是,在4.0.4及更早版本中,您需要使用URI(例如,使用file://协议)。

以下是一个不完整的代码段,应说明解决方法:

// as of 4.1.1 (JELLY_BEAN) we need to use a local path (without protocol)
// on 4.0.4 and earlier we needed a URI (with file:// protocol)
final String cachedFile = android.os.Build.VERSION.SDK_INT >= 16 // android.os.Build.VERSION_CODES.JELLY_BEAN
                        ? getCacheFilePath(file)
                        : getCacheFileUri(file);


// for the purpose of this example 
// assume cacheFolder is a String and getCacheFile returns a String

public String getCacheFilePath(String file) {
    return cacheFolder + getCacheFile(file);
}

public String getCacheFileUri(String file) {
    return "file://" + cacheFolder + getCacheFile(file);
}

答案 2 :(得分:1)

您必须明确设置文件的长度。使用重载方法:
AssetFileDescriptor afd = ctx.getAssets().openFd([your asset name]); mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

答案 3 :(得分:0)

当我尝试播放.wav文件时,我遇到了同样的错误。我们举一个例子:

 private void start_player(){
// create raw folder inside the res folder if not present
MediaPlayer mPlayer = MediaPlayer.create(activity, R.raw.your_wave_audio_file);
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mPlayer.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();
               // Your Stuff
            }
        });  
         mPlayer.start();
     }
    }

我也得到状态= 0x80000000错误。在我的情况下,解决方案是重新编码音频文件(例如,对于.wav文件为16-BIT PCM),一切都按预期工作。

答案 4 :(得分:0)

MediaPlayer mPlayer = MediaPlayer.create(activity, R.raw.your_wave_audio_file);
    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

您无法执行此操作,因为在创建功能中调用了prepare,因此您无法在之后更改音频流类型。

下面的代码对我来说很好:

sMediaPlayer = new MediaPlayer();
sMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
AssetFileDescriptor assetFileDescriptor = context.getResources().
         openRawResourceFd(R.raw.cool_song);

 if(assetFileDescriptor == null) return;

 try {
    sMediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(),
                               assetFileDescriptor.getStartOffset(), 
                               assetFileDescriptor.getLength());
    assetFileDescriptor.close();

    sMediaPlayer.setOnCompletionListener(new OnCompletionListener() {           
        @Override
        public void onCompletion(MediaPlayer mp) {
            if(sMediaPlayer != null){
                sMediaPlayer.release();
                sMediaPlayer = null;
            }
        }
    });

    sMediaPlayer.setOnPreparedListener(new OnPreparedListener() {               
        @Override
        public void onPrepared(MediaPlayer mp) {
            sMediaPlayer.start();                   
        }
    });
    sMediaPlayer.prepare();

} catch (IllegalArgumentException e) {
    HelpFunctions.showLog("ERROR = " + e);
} catch (IllegalStateException e) {
    HelpFunctions.showLog("ERROR = " + e);
} catch (IOException e) {
    HelpFunctions.showLog("ERROR = " + e);
}

答案 5 :(得分:0)

这可能是由于您尝试播放或压缩的文件格式而发生的。我正在压缩一个效果很好的mp4文件,但是当我压缩一个mov文件时,应用程序崩溃,给出了setDataSource失败的异常。