1

I am creating a game where whenever user clicks the right picture he increases the score and in addition to that I want to play a beep sound when accomplished. How can I do that?

Here is the related method:

 public void increaseScore(View view){
    score=score+1;
    scoreText.setText("Score: "+score);

How can I implement sound in this method?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95

1 Answers1

1

You can use MediaPlayer class for this.

Globe Variable:

  MediaPlayer mp;

Add it in onCreate:

 mp = MediaPlayer.create(context, R.raw.sound);

Here the function:

     public void increaseScore(View view)
     {
        score=score+1;
        scoreText.setText("Score: "+score);
           try {
                if (mp.isPlaying()) {
                       mp.stop();
                         mp.release();
                         mp = MediaPlayer.create(context, R.raw.sound);
                        }
                    mp.start();
                    } catch(Exception e) { e.printStackTrace(); }
        }
ahmad bajwa
  • 966
  • 2
  • 10
  • 30