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

ⅰ亾dé卋堺 提交于 2019-11-27 15:24:40

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.

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!