-1

Is it possible to have parameters of a method in android to be the value for R.raw.parametervalue, for example;

public void sound(SOMETHING sound){

m = MediaPlayer.create(getApplicationContext(), R.raw.sound);

}
Jongware
  • 22,200
  • 8
  • 54
  • 100
Android
  • 171
  • 1
  • 4
  • 16

3 Answers3

4

yes it is possible, as R.raw.sound is an int. So your method signature will have

(Context context, Int soundID)

For sound is not getting recognized, you need to create sound file in 'raw' folder, this problem is mentioned here - Check this one out

Community
  • 1
  • 1
Darpan
  • 5,623
  • 3
  • 48
  • 80
2

use it:

public void sound(int soundResource){

m = MediaPlayer.create(getApplicationContext(), soundResource);

}
Hamidreza Samadi
  • 637
  • 1
  • 7
  • 24
1

You can pass the id and retrieve raw file from resources using id.

Example:

public void sound (int soundId) {
    AssetFileDescriptor aFileDesc = context.getResources().openRawResourceFd(soundId);
    m = MediaPlayer.create(getApplicationContext(), soundId);
    //do something
}

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124