0

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?

the_redcar
  • 129
  • 1
  • 1
  • 10

1 Answers1

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