How to play sounds in an Android activity

It is always useful to be able to play sounds to indicate to the user that his request has been taken into account, when an activity starts or ends, when a task is complete, etc, and nothing could be simpler on Android!

The first thing to do is to import or copy the sound files you want to use in the app, in the raw directory of your Eclipse project. A list of supported audio format is available in the Android developers guide . This will allow you to use the audio file as a resource.

In order to access and play this file, the Android class to use is MediaPlayer, and this is how you can use it if you want a sound or music played when an activity starts.

public class DemoSounds extends Activity{
    private MediaPlayer player;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demosoundslayout);
        //use setVolumeControlStream to allow the user to change 
        //the volume using the hardware controls
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        //we now instantiate the player using our music file;
        //note that since it is a resource in the "raw" directory,
        //we don't need the file's extension;
        //our file here is in fact called "soundfile.mp3";
        player = MediaPlayer.create(this, R.raw.soundfile);
        //we start playing the file!
        player.start();
    }
}

And that’s it! Extremely easy and straightforward!

Comments and questions are welcome!

12 Responses to “How to play sounds in an Android activity”

  1. max92 says:

    Method works fine. But when I try to play to different sounds then contradiction occurs. Both sounds keep playing.
    I have made two buttons. On clicking one button one sound plays and when I click second button it starts as well without stopping second sound. I have tried stopping (using sound.stop() ) one of the sound while other is playing using IF isPlaying(). Can you please help me out here?

  2. Vicky says:

    Thanks Man… It works perfect for me.Bundle of thanks heap :)

  3. sami says:

    hi i try this code. it realy work :-)but how can we stop sound on go to next activit.

  4. matan says:

    hi,

    i tryed this code,
    i have a “raw” directory under
    “res” and still it does’nt work
    the error is : “raw cannot be resolved or is not a type”
    any ideas?

    • fred.blesser says:

      Did you create the “raw” directory at the right place? Your directory structure should be: “projectname/res/raw”
      Let me know if this helps!

  5. Beatrice says:

    Fine way of telling, and good paragraph to take information concerning my presentation subject, which i
    am going to present in institution of higher education.

  6. Nour says:

    I tried this one and failed..appeared FORCE CLOSE..!! so what i gonna to do..??

    • fred.blesser says:

      Hi there, sorry, but the code does work!
      You need to be more specific about your issue, or I won’t be able to help:
      – which version of Android do you use? (I have just tried the code again in 2.2, and it works)
      – have you created the “raw” directory under the “res” directory?
      – does the “raw” directory contain a valid “soundfile.mp3″ file?
      Thanks

Leave a Reply