Leyton Taylor
Leyton Taylor

Reputation: 55

Why is this playing sound on button click method not working

what I am trying to do is to call on one of three sounds on this button click. The sounds are in a preference screen. On the button click it is also supposed to display an animation. What is currently happening is I will click on the button and everything will work perfectly, but then I stop the animation and sound the second time I click the button. When I click the button again to start back up the animation and sound I cant hear the sound, but the animation still works. I honestly have no idea whats wrong. Here's my code...

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


                }


            }
    );
}

Upvotes: 1

Views: 56

Answers (1)

Buddy
Buddy

Reputation: 11028

Take a look at the MediaPlayer state diagram. After calling stop() you'll need to also call prepare() (or prepareAsync()) to be able to play it again.

In short, do this:

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

Upvotes: 1

Related Questions