作者:baby小明君 | 来源:互联网 | 2023-05-19 00:38
Iamtryingtomakeasimplebuttonthatplaysasound,butIamgettinganerroron(this,R.raw.sh
I am trying to make a simple button that plays a sound, but I am getting an error on (this, R.raw.shotgun)
. I have the raw
folder and the sound file. I think the problem is with the this
but I don't know why. Thanks.
我正在尝试制作一个播放声音的简单按钮,但是我收到了一个错误(这个,R.raw.shotgun)。我有原始文件夹和声音文件。我认为问题在于此,但我不知道为什么。谢谢。
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(this, R.raw.shotgun);
mp.start();
}
});
}
}
1 个解决方案