1

I am beginner in android development. I need to play media from raw file as STREAM_ALARM for my alarm clock. I found a similar question here : Android MediaPlayer - how to play in the STREAM_ALARM?

The accepted answer was like that on that link:

MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.setDataSource(this,Uri.parse("android.resource://PACKAGE_NAME/"+R.raw.soundfile));
mp.prepare();
mp.start();

My project's first line is like this:

package buet.mushfiq.classfriendsupport;

So, I used buet.mushfiq.classfriendsupport as my project name and implemented code in this way:

Uri uri = Uri.parse("android.resource://buet.mushfiq.classfriendsupport/"+R.raw.mytone);
MediaPlayer mp=new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
try{
    mp.setDataSource(this,uri);
    mp.prepare();
    mp.start();}
catch (Exception e){
    }

But unfortunately no audio plays and android studio shows this error:

setDataSource IOException | SecurityException happend: 
java.io.FileNotFoundException: No package found for authority: 
android.resource://buet.mushfiq.classfriendsupport/2131099648

mytone.mp3 is surely present in raw folder. Actual location: app/src/main/res/raw. Android studio plays media finely when I apply this code:

mp = MediaPlayer.create(this, R.raw.mytone);
mp.start();

So how can I locate my file in Uri.parse function?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Change this line

  Uri uri = Uri.parse("android.resource://buet.mushfiq.classfriendsupport/"+R.raw.mytone);

to

Uri uri = Uri.parse("android.resource://"+this.getPackageName()+"/"+R.raw.mytone);

hope then it will working fine. thanks.

Md. Juyel Rana
  • 540
  • 5
  • 10