Madona wambua
Madona wambua

Reputation: 1428

Adding Sound in a canvas class

I have this class which extends a view and i wanted to add a sound in my method below. Ordinarily i add button sounds with the below method. The class is a drawable canvas, It shows me a red line but no sign of what i need to change. So how can i add sound in my method below? I am calling my view in a different activity. Thanks in advance.

   if(checkCorrect == goal) {
   MediaPlayer mediaPlayer =  MediaPlayer.create(DrawGame.this,R.raw.button);
                mediaPlayer.start();
                score += 10;
                newGoal = true;
               }

Upvotes: 0

Views: 343

Answers (1)

Itzik Samara
Itzik Samara

Reputation: 2288

class GameActivity extends Activity {

    DrawGame game = new DrawGame();
    game.setContext(this);



}


class DrawGame {
      Context context;
    public void setContext(Context context) {
        this.context = context;
    }

   if(checkCorrect == goal) {
            MediaPlayer mediaPlayer =  MediaPlayer.create(context,R.raw.button);
            mediaPlayer.start();
            score += 10;
            newGoal = true;
           }

  }

Upvotes: 1

Related Questions