如何发挥从原料/资产的文件夹的音频文件的本地/默认媒体播放器? [英] How to play audio file from raw/assets folder on the native/default media player?

查看:137
本文介绍了如何发挥从原料/资产的文件夹的音频文件的本地/默认媒体播放器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以在媒体播放器一样,播放MP3文件:

 意向意图=新的意图();
intent.setAction(android.content.Intent.ACTION_VIEW);
档案文件=新的文件(YOUR_SONG_URI);
intent.setDataAndType(Uri.fromFile(文件),音频/ *);
startActivity(意向);
 

我这个链接试图得到这样的URI:

 开放的音频= Uri.parse(android.resource://com.audio.test/+ R.raw.audio1);
Log.d(TAG,URI:+ audio.toString());
 

 开放的音频= 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和而不是预期的名单的球员,我收到了找不到应用程序来执行此操作的消息。这是我的确切code:

 公共无效playAudio(){
          意图viewMediaIntent =新意图();
          viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);
          乌里音频= Uri.parse(android.resource://com.audio.test/raw/+ R.raw.audio1);
          Log.d(TAG,URI:+ audio.toString());
          viewMediaIntent.setDataAndType(音,视频/ *);
          viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
          Log.d(TAG,启动);
          意图I = Intent.createChooser(viewMediaIntent,播放音乐);
            mGap.startActivity(ⅰ);
          Log.d(TAG,启动);
      }
 

更新2

感谢您@CommonsWare的解释。现在我明白为什么它不工作。但问题仍然存在,我可以实现我想要的吗?得到一个文件的URI存储在原始/资产文件夹的文件://方案?

更新3

我找到了一种方法来做到这一点,虽然这不是它的工作原理是最好的。我只有3个文件,这不耽误执行的。我复制从RES /生到手机上的本地目录中的文件并从该文件中获取的URI。如何避免这一步骤的任何建议都AP preciated。

 公共无效copyFileToDir(字符串AUDIOFILE){
          文件测试= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC +/+ AUDIOFILE +.MP3);
          如果(test.exists()){
              Toast.makeText(mGap,存在,Toast.LENGTH_SHORT).show();
              返回;
          }
          文件DEST = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
          INT I = mGap.getResources()则getIdentifier(原始/+的AudioFile,弦,mGap.getPackageName())。
          InputStream的时间= mGap.getResources()openRawResource(我)。
          //使用的文件,构造
          的OutputStream出来;
        尝试 {
            OUT =新的FileOutputStream(新文件(DEST,AUDIOFILE +.MP3));
              //传输的字节从以出
              byte []的BUF =新的字节[1024];
              INT LEN;
              尝试 {
                  //有一点更加明确
                  而((LEN = in.read(buf中,0,buf.length))!=  -  1){
                       out.write(BUF,0,的len);
                  }
              } 最后 {
                  //确保流被关闭:
                  附寄();
                  out.close();
              }
        }赶上(FileNotFoundException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }


      }


      公共无效playAudio(字符串AUDIOFILE){
          copyFileToDir(AUD​​IOFILE);
          文件DEST = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC +/+ AUDIOFILE +.MP3);
          乌里R = Uri.fromFile(DEST);
          意图viewMediaIntent =新意图();
          viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);
          viewMediaIntent.setDataAndType(R,音频/ *);
          viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
          意图I = Intent.createChooser(viewMediaIntent,播放音乐);
            mGap.startActivity(ⅰ);

      }

      公共无效的playVideo(字符串movieurl){
          意图int​​entToPlayVideo =新的意图(Intent.ACTION_VIEW);
          intentToPlayVideo.setDataAndType(Uri.parse(movieurl),视频/ *);
          Log.d(TAG,饰演:+ movieurl);
          mGap.startActivity(intentToPlayVideo);
      }
 

解决方案

当你调用 startActivity(),你想开始一个活动。该意图传递给 startActivity()表示什么活动 - 或者选择了一组可用的活动中 - 要启动。在你的情况,您要查看 android.resource:// 乌里。这不是一个的http:// 乌里,也不是的https:// 乌里,也不是文件:// 乌里

这是宣传自己配套的操作类似这样的有,在他们的&LT活动;意向滤光器> 的说明是什么乌里方案所支持。你是假设有一个应用程序,用户的设备上,支持的 android.resource:// 方案。就个人而言,我不认为这是一个安全的假设。 的http:// 的https:// 文件:// 应该是安全的,而内容:// (对于的ContentProvider )是相当有可能为好。

例如,在AOSP音乐应用程序不支持 android.resource 方案的基础上的当前清单内容的。

I know i can play an mp3 file in the media player like that:

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

Following this link I tried to get the URI like:

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

and

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

Which outputs the expected result:

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

But it doesn't work. The media player does not start. Any ideas why?

Update

I included a createChooser and instead of the expected list with players i get a "Unable to find application to perform this action" message. This is my exact code:

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

Update 2

Thank you @CommonsWare for the explanation. Now I understand why it doesn't work. But the problem remains, can I achieve what I want? Get a Uri of a file stored in the raw/assets folder with a file:// scheme?

Update 3

I found a way to do it, although it's not the best it works. I have only 3 files and this doesn't delay the execution at all. I am copying the file from the res/raw to a local directory on the phone and getting the Uri from that file. Any suggestions on how to avoid that step are appreciated.

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

解决方案

When you call startActivity(), you are trying to start an activity. The Intent you pass to startActivity() indicates what activity -- or selection out of a set of available activities -- you want to start. In your case, you are trying to view an android.resource:// Uri. This is not an http:// Uri, nor an https:// Uri, nor a file:// Uri.

Activities that advertise themselves as supporting operations like this have, in their <intent-filter> a statement of what Uri schemes they support. You are assuming that there is an app, on the user's device, that supports an android.resource:// scheme. Personally, I do not think that this is a safe assumption. http://, https://, and file:// should be safe, and content:// (for a ContentProvider) is fairly likely as well.

For example, the AOSP Music app does not support the android.resource scheme, based on its current manifest contents.

这篇关于如何发挥从原料/资产的文件夹的音频文件的本地/默认媒体播放器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆