【问题标题】:Android RingtoneManager media path for custom ringtones not playing anymore不再播放自定义铃声的 Android RingtoneManager 媒体路径
【发布时间】:2015-07-05 18:26:37
【问题描述】:

我将旧的 nexus 设备升级到最新版本,现在 MediaPlayer 不会播放我通过 ringtoneManager 选择器检索的自定义铃声路径。

我有两个媒体的例子:

  1. content://media/internal/audio/media/86 -- 这个不播放,它是自定义铃声(我下载并添加到 /media/audio/ringtones/ 的 mp3)

  1. content://media/internal/audio/media/54 --这个播放

我正在尝试使用 android 中的 mediaplayer API 播放两者。 RingtoneManager 将两条媒体路径返回给我,如下所示:

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);

        // activity stack history, its a one time deal only
    //  intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,
                "Please Select A  Ringtone");


        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALL);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
        //intent.putExtra(RingtoneManager.EXTRA_RINGTONE_INCLUDE_DRM, true);


        try {// 44 arbitrary number to recognize our intent
            startActivityForResult(intent, 44);
}
//etc

这是我检索所选铃声的方法。一切正常,除了当用户从列表中选择自定义铃声时,我得到一个无法播放数据资源的媒体播放器 IO 异常。

// find out what ringtone the user selected and play the tone.
protected  void onActivityResult(int requestCode, int resultCode, Intent data) {
    Ringtone ringtone;

    if (requestCode == 44 && resultCode == RESULT_OK) {
        Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

}

//获取uri,取值为:content://media/internal/audio/media/86

这是我尝试播放此自定义铃声时收到的媒体播放器错误:

  e = {java.io.IOException@4499} "java.io.IOException: setDataSource failed.: status=0x80000000"
 cause = {java.io.IOException@4499} "java.io.IOException: setDataSource failed.: status=0x80000000"
 detailMessage = {java.lang.String@4503} "setDataSource failed.: status=0x80000000"

任何其他媒体都可以正常工作,只是不能自定义铃声。

【问题讨论】:

    标签: android ringtone


    【解决方案1】:

    看起来该路径不被接受,出于某种原因,我必须使用完整路径才能访问自定义铃声。谷歌,有什么原因吗?这是我的解决方法。使用 isValidUri 检查 uri 是否有效。如果它无效,我们调用 getRingtonePathFromContentUri 来获取资源的目录路径并改用它。问题报告here

     /**
    * checks if a url such as content://media/internal/audio/media/86 can be played.
     * if not returns no and we can fall back to something else.
    **/
        public boolean isValidUri(String contentUri){
    
            boolean result=true;
    
            MediaPlayer player = new MediaPlayer();
            try {
                player.setDataSource(contentUri);
            } catch (IOException e) {
                e.printStackTrace();
                result = false;
            }
    
            return result;
        }
    
        //gets the SD card path for a ringtone uri
        public  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;
        }
    

    【讨论】:

      【解决方案2】:

      您可以将您的 mp3 文件放在res/raw 文件夹中:

      MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.myringtone);
      mediaPlayer.start();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多