我是一个菜鸟,想尝试一些东西并从中学到东西。
我有两个图像按钮,当我单击它们时,会得到一种“ Flutter ”的声音,而不是/ res / raw /目录中的声音文件。
这是我的代码:
public void button_clicked1(View v)
{
text1.setText("1"+width);
mp = MediaPlayer.create(GameScreen.this, R.raw.a);
mp.start();
}
public void button_clicked2(View v)
{
text1.setText("2"+height);
mp = MediaPlayer.create(GameScreen.this, R.raw.b);
mp.start();
}
我究竟做错了什么?
谢谢!
好的,将上面的代码更改为:
public void button_clicked1(View v)
{
text1.setText("1"+width);
mp = MediaPlayer.create(GameScreen.this, R.raw.piano_a);
try {
mp .prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
public void button_clicked2(View v)
{
text1.setText("2"+height);
mp = MediaPlayer.create(GameScreen.this, R.raw.piano_b);
try {
mp .prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
而且仍然无法正常工作
最佳答案
编辑:尝试以下操作:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
在您的主要应用程序代码中。这将告诉
AudioManager
,当您的应用程序获得焦点时,音量键应调整音乐音量(found that here)。之后,请确保您的音量调高了-可能只是在播放没有音量的声音。
关于android - Android:Imagebutton,onclick播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7059892/