Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么按钮点击方式播放声音不起作用_Java_Android_Audio - Fatal编程技术网

Java 为什么按钮点击方式播放声音不起作用

Java 为什么按钮点击方式播放声音不起作用,java,android,audio,Java,Android,Audio,我想做的是在这个按钮上点击三种声音中的一种。声音在首选项屏幕中。在按钮上单击它也应该显示动画。目前正在发生的是,我将点击按钮,一切都将完美地工作,但我停止动画和声音,我第二次点击按钮。当我再次单击按钮开始备份动画和声音时,我听不到声音,但动画仍然有效。我真的不知道出了什么问题。这是我的密码 public void buttonClick() { imgView = (ImageView) findViewById(R.id.imageView); button = (Button

我想做的是在这个按钮上点击三种声音中的一种。声音在首选项屏幕中。在按钮上单击它也应该显示动画。目前正在发生的是,我将点击按钮,一切都将完美地工作,但我停止动画和声音,我第二次点击按钮。当我再次单击按钮开始备份动画和声音时,我听不到声音,但动画仍然有效。我真的不知道出了什么问题。这是我的密码

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();


                }


            }
    );
}
请看下面的图片。调用
stop()
后,您还需要调用
prepare()
(或
prepareAsync()
)才能再次播放

简而言之,请执行以下操作:

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