用于自定义铃声的Android RingtoneManager媒体路径不再播放

时间:2015-07-05 18:26:37

标签: android ringtone

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

我有两个媒体作为例子:

  1. 内容://媒体/内部/音频/媒体/ 86 - 这个不播放,它是一个自定义铃声(我下载并添加到/ media / audio / ringtones /的mp3)
    1. content:// media / internal / audio / media / 54 - 这个播放
    2. 我试图在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"
      

      任何其他媒体工作正常,而不是自定义铃声。

2 个答案:

答案 0 :(得分:0)

看起来路径不被接受,我必须使用完整路径来获取自定义铃声由于某种原因。谷歌,有什么理由吗?这是我的解决方法。检查uri是否对isValidUri有效。如果它无效,我们调用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;
    }

答案 1 :(得分:-1)

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

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