title: How to play audio file from raw/assets folder on the native/default media player?
body: <p>I know i can play an mp3 file in the media player like that:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>Following <a href="http://androidbook.blogspot.co.uk/2009/08/referring-to-android-resources-using.html" rel="nofollow">this link</a> I tried to get the URI like:</p>
  
  <pre><code>Uri audio = Uri.parse("android.resource://com.audio.test/"+R.raw.audio1);
  Log.d(TAG,"uri:"+audio.toString());
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Uri audio = Uri.parse("android.resource://com.audio.test/raw/audio");
  Log.d(TAG,"uri:"+audio.toString());
  </code></pre>
  
  <p>Which outputs the expected result:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>But it doesn't work. The media player does not start. Any ideas why?</p>
  
  <p><strong>Update</strong></p>
  
  <p>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:</p>
  
  <pre><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");
        }
  </code></pre>
  
  <p><strong>Update 2</strong></p>
  
  <p>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? </p>
  
  <p><strong>Update 3</strong> </p>
  
  <p>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.</p>
  
  <pre><code> 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);
        }
  </code></pre>