Sunkat
Sunkat

Reputation: 393

Android: how to add R.raw... to a method as a general parameter?

I am trying to create a method, to play some sound in different activities.

 public void playSound(View view, ??? sound){
        mpSound = MediaPlayer.create(this, R.raw.sound);
        mpSound.start();
    }

What type should have a "sound" parameter?

Upvotes: 0

Views: 855

Answers (2)

blueware
blueware

Reputation: 5381

Passing resource values can be integers like this:

public void playSound(View view, @RawRes int sound){
    mpSound = MediaPlayer.create(this, sound);
    mpSound.start();
}

Calling your method would be:

playSound(mView, R.raw.sound);

Upvotes: 4

MarcGV
MarcGV

Reputation: 1262

It should be a int type with @RawRes Annotation like this:

public void playSound(View view, @RawRes int sound){
    mpSound = MediaPlayer.create(this, sound);
    mpSound.start();
}


You will see this error if you don't pass a raw resource:

Expected resource of type raw

Upvotes: 1

Related Questions