I am trying to play an mp3 file called alarm.mp3 in Android Studios, but other answers state that the audio file should be stored in a directory called res/raw. The raw directory does not exist in my res folder, however. Should I create my own raw directory, or is there another reason the raw directory is not showing up?
Asked
Active
Viewed 568 times
1 Answers
1
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.

Bhoomika Patel
- 1,895
- 1
- 13
- 30
-
I put the audio file in the newly created raw folder, but trying R.raw.alarm.mp3 does not work. What is the problem with this code? – the_redcar Sep 26 '19 at 04:46
-
how you are trying to play?? – Bhoomika Patel Sep 26 '19 at 04:47
-
I'm using the following code: MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1); mediaPlayer.start(); – the_redcar Sep 26 '19 at 04:47
-
@the_redcar check my updated answer – Bhoomika Patel Sep 26 '19 at 04:49
-
@the_redcar hope you got your solution. – Bhoomika Patel Sep 26 '19 at 04:54
-
does this work with mp3 audio files? Or does it need to be a raw audio file? sorry for my confusion, this is my first time working with audio files in android studios. – the_redcar Sep 26 '19 at 04:57
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199990/discussion-between-the-redcar-and-bhoomika-patel). – the_redcar Sep 26 '19 at 04:57
-
yes. its work with .mp3 file. check my updated answer. You need to pass URI to media player. Here My file name is siren. You need to set your file name there. that's it. – Bhoomika Patel Sep 26 '19 at 04:59
-
Sure. Glad that it helps you. :) – Bhoomika Patel Sep 26 '19 at 05:03