How to play audio file from raw/assets folder on the native/default media player?

后端 未结 4 1693
野的像风
野的像风 2020-12-03 20:43

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 fil         


        
相关标签:
4条回答
  • 2020-12-03 20:56

    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.

    0 讨论(0)
  • 2020-12-03 21:03

    It is not possible to use content from your RES folder outside of the App. The content is inside the App and not reachable from outside the App. If you want to make the content available you have to implement your own ContentProvider returning the data.

    @CommonsWare is right. See http://iserveandroid.blogspot.nl/2011/01/how-to-access-resources-from-other.html

    0 讨论(0)
  • 2020-12-03 21:08

    Any reason why don't you use the MediaPlayer directly? You can pass the resource id directly

    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();
    
    0 讨论(0)
  • 2020-12-03 21:18

    If you just need to set the sound URI on the Notification.Builder, use

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

    where R.raw.mysound can be something like mysoud.ogg in the raw resources folder.

    However I am not sure if MP3 is exactly supported. As these are just internal resources of your application, try to convert to OGG.

    0 讨论(0)
提交回复
热议问题