Android:如何将R.raw ...添加到作为常规参数的方法中?
问题描述:
我想创建一个方法,在不同的活动中播放一些声音。Android:如何将R.raw ...添加到作为常规参数的方法中?
public void playSound(View view, ??? sound){
mpSound = MediaPlayer.create(this, R.raw.sound);
mpSound.start();
}
什么类型应该有“声音”参数?
答
传递资源值可以integers
这样的:
public void playSound(View view, @RawRes int sound){
mpSound = MediaPlayer.create(this, sound);
mpSound.start();
}
调用你的方法是:
playSound(mView, R.raw.sound);
答
它应该与@RawRes诠释这样一个整型:
public void playSound(View view, @RawRes int sound){
mpSound = MediaPlayer.create(this, sound);
mpSound.start();
}
你会看到这个错误如果您未通过原始资源:
Expected resource of type raw