2

我知道我可以像这样在媒体播放器中播放 mp3 文件:

Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW);  
File file = new File(YOUR_SONG_URI);  
intent.setDataAndType(Uri.fromFile(file), "audio/*");  
startActivity(intent);

按照此链接,我尝试获取如下 URI:

Uri audio = Uri.parse("android.resource://com.audio.test/"+R.raw.audio1);
Log.d(TAG,"uri:"+audio.toString());

Uri audio = Uri.parse("android.resource://com.audio.test/raw/audio");
Log.d(TAG,"uri:"+audio.toString());

输出预期结果:

01-24 15:28:23.190: D/MyClass(30234): uri:android.resource://com.audio.test/2131034112
01-24 15:29:13.: D/MyClass(30234): uri:android.resource://com.audio.test/raw/audio1

但它不起作用。媒体播放器不启动。任何想法为什么?

更新

我包含了一个 createChooser,而不是预期的玩家列表,我收到“无法找到执行此操作的应用程序”消息。这是我的确切代码:

 public void playAudio(){
          Intent viewMediaIntent = new Intent();   
          viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);        
          Uri audio = Uri.parse("android.resource://com.audio.test/raw/"+R.raw.audio1);       
          Log.d(TAG,"uri:"+audio.toString());
          viewMediaIntent.setDataAndType(audio, "video/*");   
          viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
          Log.d(TAG,"Starting");
          Intent i = Intent.createChooser(viewMediaIntent, "Play Music");
            mGap.startActivity(i);
          Log.d(TAG,"Started");
      }

更新 2

谢谢@CommonsWare 的解释。现在我明白为什么它不起作用了。但问题依然存在,我能达到我想要的吗?使用 file:// 方案获取存储在 raw/assets 文件夹中的文件的 Uri?

更新 3

我找到了一种方法来做到这一点,虽然它不是最好的。我只有 3 个文件,这根本不会延迟执行。我正在将文件从 res/raw 复制到手机上的本地目录并从该文件中获取 Uri。任何有关如何避免该步骤的建议表示赞赏。

 public void copyFileToDir(String audioFile){
          File test = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "/" + audioFile + ".mp3");
          if (test.exists()){
              Toast.makeText(mGap, "Exists", Toast.LENGTH_SHORT).show();
              return;
          }
          File dest = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
          int i = mGap.getResources().getIdentifier("raw/"+audioFile, "string", mGap.getPackageName());
          InputStream in = mGap.getResources().openRawResource(i);
          // Used the File-constructor
          OutputStream out;
        try {
            out = new FileOutputStream(new File(dest, audioFile + ".mp3"));
              // Transfer bytes from in to out
              byte[] buf = new byte[1024];
              int len;
              try {
                  // A little more explicit
                  while ( (len = in.read(buf, 0, buf.length)) != -1){
                       out.write(buf, 0, len);
                  }
              } finally {
                  // Ensure the Streams are closed:
                  in.close();
                  out.close();
              }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 


      }


      public void playAudio(String audioFile){
          copyFileToDir(audioFile);
          File dest = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "/" + audioFile + ".mp3");
          Uri r = Uri.fromFile(dest);         
          Intent viewMediaIntent = new Intent();   
          viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);                                 
          viewMediaIntent.setDataAndType(r, "audio/*");   
          viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);          
          Intent i = Intent.createChooser(viewMediaIntent, "Play Music");
            mGap.startActivity(i);

      }

      public void playVideo(String movieurl){
          Intent intentToPlayVideo = new Intent(Intent.ACTION_VIEW);
          intentToPlayVideo.setDataAndType(Uri.parse(movieurl), "video/*");
          Log.d(TAG,"Playing:" + movieurl);
          mGap.startActivity(intentToPlayVideo);
      }
4

4 回答 4

4

有什么理由不直接使用 MediaPlayer?可以直接传资源id

int resourceId = R.raw.your_media_resource;
MediaPlayer mediaPlayer = MediaPlayer.create( context, resourceId );
mediaPlayer.setOnCompletionListener( new OnCompletionListener()
{
    @Override
    public void onCompletion( MediaPlayer mp )
    {
        mp.release();
    }
} );
mediaPlayer.start();
于 2013-01-24T15:55:00.983 回答
3

当您打电话时startActivity(),您正在尝试开始一项活动。Intent您传递给的startActivity()指示您要启动的活动(或从一组可用活动中选择)。在您的情况下,您正在尝试查看android.resource:// Uri. 这不是一个http:// Uri,也不是一个https:// Uri,也不是一个file:// Uri

将自己宣传为支持此类运营的活动在<intent-filter>声明中说明了Uri他们支持的计划。您假设用户设备上有一个支持android.resource://方案的应用程序。就个人而言,我不认为这是一个安全的假设。 http://, https://, andfile://应该是安全的,并且content://(for a ContentProvider) 也很有可能。

例如,AOSP 音乐应用程序不支持该android.resource方案,基于其当前清单内容

于 2013-01-24T16:07:33.580 回答
0

无法在应用程序之外使用 RES 文件夹中的内容。内容在 App 内部,无法从 App 外部访问。如果要使内容可用,则必须实现自己的 ContentProvider 返回数据。

@CommonsWare 是对的。见http://iserveandroid.blogspot.nl/2011/01/how-to-access-resources-from-other.html

于 2013-01-24T15:56:56.643 回答
0

如果您只需要在Notification.Builder上设置声音 URI ,请使用

 Uri.parse("android.resource://com.my.great.application/"
                        + R.raw.mysound);

whereR.raw.mysound可以是mysoud.ogg原始资源文件夹中的内容。

但是我不确定是否完全支持 MP3。由于这些只是您应用程序的内部资源,请尝试转换为 OGG。

于 2013-01-24T15:58:47.603 回答