gpt4 book ai didi

java - 为什么这种在按钮单击方法上播放声音不起作用

转载 作者:行者123 更新时间:2023-11-30 01:48:22 26 4
gpt4 key购买 nike

我想要做的是在单击此按钮时调用三种声音之一。声音在首选项屏幕中。在按钮上单击它也应该显示动画。目前正在发生的事情是我将点击按钮,一切都会完美运行,但随后我停止动画并在我第二次点击按钮时发出声音。当我再次单击按钮开始备份动画和声音时,我听不到声音,但动画仍然有效。老实说,我不知道出了什么问题。这是我的代码...

public void buttonClick() {
imgView = (ImageView) findViewById(R.id.imageView);
button = (Button) findViewById(R.id.button);
blade = (ImageView)findViewById(R.id.imageView4);
final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
1stSound = MediaPlayer.create(this, R.raw.file.mp3);
2ndSound = MediaPlayer.create(this, R.raw.file.mp3);
3rdSOund = MediaPlayer.create(this, R.raw.file.mp3);

button.setOnClickListener(

new View.OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean option1 = getPrefs.getBoolean("alternate", false);
boolean option2 = getPrefs.getBoolean("white", false);
boolean option3 = getPrefs.getBoolean("standard",false);

if (blade.getAnimation() == null) {
// no animation, start it
if (option1 == true){
1stSound.start();
blade.startAnimation(animRotate);

} else if (option2 == true){
3rdSound.start();
blade.startAnimation(animRotate);

} else if (option3 == true) {
2ndFan.start();
blade.startAnimation(animRotate);

}

} else {
//animation is showing, stop it
blade.clearAnimation();
3rdSound.stop();
2ndSound.stop();
1stSound.stop();

}





current_image_index++;
current_image_index = current_image_index % images.length;
imgView.setImageResource(images[current_image_index]);
imgView.invalidate();


}


}
);
}

最佳答案

看看 MediaPlayer state diagram .调用 stop() 后,您还需要调用 prepare()(或 prepareAsync())才能再次播放。

简而言之,这样做:

3rdSound.stop();
3rdSound.prepareAsync();
2ndSound.stop();
2ndSound.prepareAsync();
1stSound.stop();
1stSound.prepareAsync();

关于java - 为什么这种在按钮单击方法上播放声音不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33403698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com