0

I am trying to play an audio file present in res/raw directory using the default audio player from my app.

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(String.format("android.resource://%s/%s/%s",getContext().getPackageName(),"raw","fade")), "audio/*");     

startActivity(intent);

However, every time I am getting exception:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.example.android.punjabi/raw/fade typ=audio/* launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } }

I have tried out the ways mentioned in here:

Get URI of .mp3 file stored in res/raw folder in android

It still gives me the same exception.

Any help please. I don't want to use the MediaPlayer class to play the audio. I want to use the default audio player on the phone to play it.

Manpreet
  • 570
  • 5
  • 20

1 Answers1

0

There is no requirement for any device to have an app capable of supporting playing back audio from a Uri using the under-utilized android:resource scheme.

Either:

  • Play it yourself using MediaPlayer, or

  • Copy the contents of that resource to a file, then use FileProvider to serve it up using a content Uri

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi CommonsWare, Playing it using MediaPlayer is not really an option. Doing so, I'll have to manage play/pause/stop, seekbar in a runnable, save the state on orientation change etc. I would like to keep it as simple as possible. About your point 2, can you give me some example by which I can achieve this? or may be if I move my files to assests directory? Will this help? – Manpreet Feb 20 '18 at 21:17
  • @Manpreet: "can you give me some example by which I can achieve this?" -- get a `Resources` object via `getResources()` on your activity. Call `openRawResource()` to get an `InputStream` on your raw resource. From there, it's standard Java I/O. "if I move my files to assests directory? Will this help?" -- no, sorry. – CommonsWare Feb 20 '18 at 21:30