How to Play the Audio Files Directly from Res/Raw Folder

How to play the audio files directly from res/raw folder?

add this code in onItemClickListener.

listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {
TextView txtView=(TextView)view.findViewById(R.id.txt_view);
String fname=txtView.getText().toString().toLowerCase();

int resID=getResources().getIdentifier(fname, "raw", getPackageName());

MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.start();
}
});

How do I play an mp3 in the res/raw folder of my android app?

When starting the activity i.e on onCreate put the following code.

  public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.main);

MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
mPlayer.start();

}

When stopping the activity i.e on onDestroy put the following code.

   public void onDestroy() {

mPlayer.stop();
super.onDestroy();

}

Hope it helps :)

How to play a different sound file from resraw

You can do something like this :

var a1 = R.raw.a1
var a2 = R.raw.a2
var a3 = R.raw.a3

Or you can do it with JAVA like this :

int setMusic(String mMusic){
return this.getResources().getIdentifier(mMusic, "raw", this.getPackageName());
}

and call it like this :

mp = MediaPlayer.create(this, setMusic("a1"))

or

mp = MediaPlayer.create(this, setMusic("a2"))

Where is the file directory res/raw?

You need to create new directory named raw inside res folder of your project. Then put your .mp3 file inside res>raw folder of your project

I am using MediaPlayer for custom sound. And This is Working fine for me. This is working for all devices.

private MediaPlayer player;

For play custom sound:

 try {
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.siren);

player = MediaPlayer.create(this, uri);
player.setLooping(true); // This will play sound in repeatable mode.
player.start();

} catch (Exception e) {
e.printStackTrace();
}

For stop sound:

  if (player != null)
player.stop();

This is working for me. Hope this will also helps you.

Android: playing audio files in /res/raw by file name

MediaPlayer player = new MediaPlayer();
String path = "android.resource://" + getPackageName() + "/raw" + filename;
try {
player.setDataSource(context, Uri.parse(path));
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}

try it with or without the extention ".mp3"

hope i could help you

play mp3 file in resource folder not working

It will help more if you can post the StackTrace in your question.

But, as per the information in your question, the below code should work for playing the media file from the raw resource folder.

If you use the create() method, prepare() gets called internally and you don't need to explicitly call it.

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.notify);
mediaPlayer.start();

But, the point to consider is that prepare() generally throws an IllegalStateException, and in your case, you are getting an IOException. So it would be worth checking if the file is in fact present in raw folder and/or the file is corrupt.



Related Topics



Leave a reply



Submit