Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Trying to play audio outside my 'init' method!

$
0
0
So the problem I'm having is that I CAN play the audio when I do it from the init method in my AppMain class, but obviously that plays the clip right when the applet initializes. I'm making a multiple choice music game, so I want the music to play when the first question comes up. The game works, but I am only including two of my classes here (my main class that initializes it, and the questions class where I WANT the music to play) thanks.

Main class(works with music)
public class AppMain extends JApplet implements Runnable {

	private AppPanel panel;
	public static int WIDTH = 1440;
	public static int HEIGHT = 900;
	public Thread thread;
	private AudioClip seize;

	public int lives = 5;

	//Initializer Method
	public void init(){
	    seize = getAudioClip(getDocumentBase(),"Seize the Day 15.wav");
	    seize.play(); //This plays the sound perfectly, but from the initialization
		panel = new AppPanel(this);
		setContentPane(panel);
		thread = new Thread(this);
		thread.start();
		setSize(WIDTH, HEIGHT);
	}

	//Initializes the Applet
	public void run(){
		while (true) {
		try{
			Thread.sleep(10);
		}catch(Exception e){}
		panel.repaint();

	}

		}
}


That works, but I want the audio to be able to play in my next class, the actual game part of my code. The majority of this code just changes the questions/answers so ignore the big middle part. Methods are at the very end:
public class Questions extends Screen{


    private String q = "1. Name the BAND";
    private String y = "";
    private AudioClip seize;
    private int a = 0;
    private int b = 1;
    private int c = 2;
    private int d = 3;

    OptionButton button1 = new OptionButton(400,200,300,75, "Guns n Roses");
    OptionButton button2 = new OptionButton(800, 200, 300, 75, "Avenged Sevenfold");
    OptionButton button3 = new OptionButton(400, 375, 300, 75, "Aerosmith");
    OptionButton button4 = new OptionButton(800, 375, 300, 75, "AC/DC");

    OptionButton[] buttons = {
    button1,
    button2,
    button3,
    button4
};
          
             public Questions(final AppPanel panel) {
             super(panel);
     
            }

    {
        for (int i = 0; i<buttons.length; i++){
            panel.add(button1);
            panel.add(button2);
            panel.add(button3);
            panel.add(button4);
           
            buttons[i].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                playSoundOnce(event); //This is where I want the music to play, right when the first question comes up, but nothing happens. The game still works, but it is silent. The "playSoundOnce" method is all the way at the bottom.
            }
        });
            

            if(i==a){
                
                buttons[i].addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                        correctAnswer(event);
                        //Go to Question 2
                        button1.setSize(0,0);
                        button2.setSize(0,0);
                        button3.setSize(0,0);
                        button4.setSize(0,0);
                        q = "2. Name the SONG";

                        button1 = new OptionButton(400, 200, 300, 75, "Civil War");
                        button2 = new OptionButton(800, 200, 300, 75, "Seize the Day");
                        button3 = new OptionButton(400, 375, 300, 75, "Walk this Way");
                        button4 = new OptionButton(800, 375, 300, 75, "Back in Black");

                        OptionButton[] buttons = {
                                button1,
                                button2,
                                button3,
                                button4
                        };

                                for(int i=0; i<buttons.length; i++){
                                    panel.add(button1);
                                    panel.add(button2);
                                    panel.add(button3);
                                    panel.add(button4);
                                if(i==B)/>{
                                    buttons[i].addActionListener(new ActionListener(){
                                        public void actionPerformed(ActionEvent event){
                                            correctAnswer(event);

                                //Go to Question 3
                                button1.setSize(0,0);
                                button2.setSize(0,0);
                                button3.setSize(0,0);
                                button4.setSize(0,0);
                                q = "3. Name the DECADE";

                                button1 = new OptionButton(400, 200, 300, 75, "90's");
                                button2 = new OptionButton(800, 200, 300, 75, "00's");
                                button3 = new OptionButton(400, 375, 300, 75, "70's");
                                button4 = new OptionButton(800, 375, 300, 75, "80's");

                                OptionButton[] buttons3 = {
                                        button1,
                                        button2,
                                        button3,
                                        button4
                                };

                                        for(int i=0; i<buttons3.length; i++){
                                            panel.add(button1);
                                            panel.add(button2);
                                            panel.add(button3);
                                            panel.add(button4);
                                        if(i==c){
                                            buttons3[i].addActionListener(new ActionListener(){
                                                public void actionPerformed(ActionEvent event){
                                                    correctAnswer(event);

                                                    //Go to Question 4
                                                    button1.setSize(0,0);
                                                    button2.setSize(0,0);
                                                    button3.setSize(0,0);
                                                    button4.setSize(0,0);
                                                    q = "4. Name the SONG";

                                                    button1 = new OptionButton(400, 200, 300, 75, "Sharp Dressed Man");
                                                    button2 = new OptionButton(800, 200, 300, 75, "Landslide");
                                                    button3 = new OptionButton(400, 375, 300, 75, "Learn to Fly");
                                                    button4 = new OptionButton(800, 375, 300, 75, "Going Home");

                                                    OptionButton[] buttons4 = {
                                                            button1,
                                                            button2,
                                                            button3,
                                                            button4
                                                    };

                                                            for(int i=0; i<buttons4.length; i++){
                                                                panel.add(button1);
                                                                panel.add(button2);
                                                                panel.add(button3);
                                                                panel.add(button4);
                                                            if(i==d){
                                                                buttons4[i].addActionListener(new ActionListener(){
                                                                    public void actionPerformed(ActionEvent event){
                                                                        correctAnswer(event);

                                                                        //Go to Question 5
                                                                        button1.setSize(0,0);
                                                                        button2.setSize(0,0);
                                                                        button3.setSize(0,0);
                                                                        button4.setSize(0,0);
                                                                        q = "5. Name the BAND";

                                                                        button1 = new OptionButton(400, 200, 300, 75, "ZZ Top");
                                                                        button2 = new OptionButton(800, 200, 300, 75, "Fleetwood Mac");
                                                                        button3 = new OptionButton(400, 375, 300, 75, "Pantera");
                                                                        button4 = new OptionButton(800, 375, 300, 75, "Sublime");

                                                                        OptionButton[] buttons5 = {
                                                                                button1,
                                                                                button2,
                                                                                button3,
                                                                                button4
                                                                        };

                                                                                for(int i=0; i<buttons5.length; i++){
                                                                                    panel.add(button1);
                                                                                    panel.add(button2);
                                                                                    panel.add(button3);
                                                                                    panel.add(button4);
                                                                                if(i==a){
                                                                                    buttons5[i].addActionListener(new ActionListener(){
                                                                                        public void actionPerformed(ActionEvent event){
                                                                                            correctAnswer(event);

                                                                                            //Go to Question 6
                                                                                            button1.setSize(0,0);
                                                                                            button2.setSize(0,0);
                                                                                            button3.setSize(0,0);
                                                                                            button4.setSize(0,0);
                                                                                            q = "6. Name the DECADE";

                                                                                            button1 = new OptionButton(400, 200, 300, 75, "90's");
                                                                                            button2 = new OptionButton(800, 200, 300, 75, "70's");
                                                                                            button3 = new OptionButton(400, 375, 300, 75, "00's");
                                                                                            button4 = new OptionButton(800, 375, 300, 75, "80's");

                                                                                            OptionButton[] buttons6 = {
                                                                                                    button1,
                                                                                                    button2,
                                                                                                    button3,
                                                                                                    button4
                                                                                            };

                                                                                                    for(int i=0; i<buttons6.length; i++){
                                                                                                        panel.add(button1);
                                                                                                        panel.add(button2);
                                                                                                        panel.add(button3);
                                                                                                        panel.add(button4);
                                                                                                    if(i==B)/>{
                                                                                                        buttons6[i].addActionListener(new ActionListener(){
                                                                                                            public void actionPerformed(ActionEvent event){
                                                                                                                correctAnswer(event);

                                                                                                                //Go to Question 7
                                                                                                                button1.setSize(0,0);
                                                                                                                button2.setSize(0,0);
                                                                                                                button3.setSize(0,0);
                                                                                                                button4.setSize(0,0);
                                                                                                                q = "7. Name the BAND";

                                                                                                                button1 = new OptionButton(400, 200, 300, 75, "Queen");
                                                                                                                button2 = new OptionButton(800, 200, 300, 75, "Arctic Monkeys");
                                                                                                                button3 = new OptionButton(400, 375, 300, 75, "Radiohead");
                                                                                                                button4 = new OptionButton(800, 375, 300, 75, "Muse");

                                                                                                                OptionButton[] buttons7 = {
                                                                                                                        button1,
                                                                                                                        button2,
                                                                                                                        button3,
                                                                                                                        button4
                                                                                                                };

                                                                                                                        for(int i=0; i<buttons7.length; i++){
                                                                                                                            panel.add(button1);
                                                                                                                            panel.add(button2);
                                                                                                                            panel.add(button3);
                                                                                                                            panel.add(button4);
                                                                                                                        if(i==c){
                                                                                                                            buttons7[i].addActionListener(new ActionListener(){
                                                                                                                                public void actionPerformed(ActionEvent event){
                                                                                                                                    correctAnswer(event);

                                                                                                                                    //Go to Question 8
                                                                                                                                    button1.setSize(0,0);
                                                                                                                                    button2.setSize(0,0);
                                                                                                                                    button3.setSize(0,0);
                                                                                                                                    button4.setSize(0,0);
                                                                                                                                    q = "8. Name the SONG";

                                                                                                                                    button1 = new OptionButton(400, 200, 300, 75, "Stairway to Heaven");
                                                                                                                                    button2 = new OptionButton(800, 200, 300, 75, "5 Minutes Alone");
                                                                                                                                    button3 = new OptionButton(400, 375, 300, 75, "Issues");
                                                                                                                                    button4 = new OptionButton(800, 375, 300, 75, "Heaven");

                                                                                                                                    OptionButton[] buttons8 = {
                                                                                                                                            button1,
                                                                                                                                            button2,
                                                                                                                                            button3,
                                                                                                                                            button4
                                                                                                                                    };

                                                                                                                                            for(int i=0; i<buttons8.length; i++){
                                                                                                                                                panel.add(button1);
                                                                                                                                                panel.add(button2);
                                                                                                                                                panel.add(button3);
                                                                                                                                                panel.add(button4);
                                                                                                                                            if(i==d){
                                                                                                                                                buttons8[i].addActionListener(new ActionListener(){
                                                                                                                                                    public void actionPerformed(ActionEvent event){
                                                                                                                                                        correctAnswer(event);

                                                                                                                                                        //Go to Question 9
                                                                                                                                                        button1.setSize(0,0);
                                                                                                                                                        button2.setSize(0,0);
                                                                                                                                                        button3.setSize(0,0);
                                                                                                                                                        button4.setSize(0,0);
                                                                                                                                                        q = "9. Name the DECADE";

                                                                                                                                                        button1 = new OptionButton(400, 200, 300, 75, "70's");
                                                                                                                                                        button2 = new OptionButton(800, 200, 300, 75, "80's");
                                                                                                                                                        button3 = new OptionButton(400, 375, 300, 75, "90's");
                                                                                                                                                        button4 = new OptionButton(800, 375, 300, 75, "00's");

                                                                                                                                                        OptionButton[] buttons9 = {
                                                                                                                                                                button1,
                                                                                                                                                                button2,
                                                                                                                                                                button3,
                                                                                                                                                                button4
                                                                                                                                                        };

                                                                                                                                                                for(int i=0; i<buttons9.length; i++){
                                                                                                                                                                    panel.add(button1);
                                                                                                                                                                    panel.add(button2);
                                                                                                                                                                    panel.add(button3);
                                                                                                                                                                    panel.add(button4);
                                                                                                                                                                if(i==a){
                                                                                                                                                                    buttons9[i].addActionListener(new ActionListener(){
                                                                                                                                                                        public void actionPerformed(ActionEvent event){
                                                                                                                                                                            correctAnswer(event);

                                                                                                                                                                            //Go to Question 10
                                                                                                                                                                            button1.setSize(0,0);
                                                                                                                                                                            button2.setSize(0,0);
                                                                                                                                                                            button3.setSize(0,0);
                                                                                                                                                                            button4.setSize(0,0);
                                                                                                                                                                            q = "10. Name the SONG";

                                                                                                                                                                            button1 = new OptionButton(400, 200, 300, 75, "Smells Like Teen Spirit");
                                                                                                                                                                            button2 = new OptionButton(800, 200, 300, 75, "Bohemian Rhapsody");
                                                                                                                                                                            button3 = new OptionButton(400, 375, 300, 75, "One");
                                                                                                                                                                            button4 = new OptionButton(800, 375, 300, 75, "Hotel California");

                                                                                                                                                                            OptionButton[] buttons10 = {
                                                                                                                                                                                    button1,
                                                                                                                                                                                    button2,
                                                                                                                                                                                    button3,
                                                                                                                                                                                    button4
                                                                                                                                                                            };

                                                                                                                                                                                    for(int i=0; i<buttons10.length; i++){
                                                                                                                                                                                        panel.add(button1);
                                                                                                                                                                                        panel.add(button2);
                                                                                                                                                                                        panel.add(button3);
                                                                                                                                                                                        panel.add(button4);
                                                                                                                                                                                    if(i==B)/>{
                                                                                                                                                                                        buttons10[i].addActionListener(new ActionListener(){
                                                                                                                                                                                            public void actionPerformed(ActionEvent event){
                                                                                                                                                                                                correctAnswer(event);
                                                                                                                                                                        }
                                                                                                                                                                    });
                                                                                                                                                                }
                                                                                                                                                                    else {buttons10[i].addActionListener(new ActionListener(){
                                                                                                                                                                        public void actionPerformed(ActionEvent event){
                                                                                                                                                                            wrongAnswer(event);
                                                                                                                                                                        }
                                                                                                                                                                    });
                                                                                                                                                                    }
                                                                                                                                                                }
                                                                                                                                                                }
                                                                                                                                                    });
                                                                                                                                                }
                                                                                                                                                else {buttons9[i].addActionListener(new ActionListener(){
                                                                                                                                                    public void actionPerformed(ActionEvent event){
                                                                                                                                                        wrongAnswer(event);
                                                                                                                                                    }
                                                                                                                                                });
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                            }
                                                                                                                                });
                                                                                                                            }
                                                                                                                                            else {buttons8[i].addActionListener(new ActionListener(){
                                                                                                                                                public void actionPerformed(ActionEvent event){
                                                                                                                                                    wrongAnswer(event);
                                                                                                                                                }
                                                                                                                                            });
                                                                                                                                            }
                                                                                                                                            }
                                                                                                                                }
                                                                                                                            });
                                                                                                                        }
                                                                                                                        else {buttons7[i].addActionListener(new ActionListener(){
                                                                                                                            public void actionPerformed(ActionEvent event){
                                                                                                                                wrongAnswer(event);
                                                                                                                            }
                                                                                                                        });
                                                                                                                        }
                                                                                                                        }
                                                                                                            }
                                                                                                        });
                                                                                                    }
                                                                                                    else {buttons6[i].addActionListener(new ActionListener(){
                                                                                                        public void actionPerformed(ActionEvent event){
                                                                                                            wrongAnswer(event);
                                                                                                        }
                                                                                                    });
                                                                                                    }
                                                                                                    }
                                                                                        }
                                                                                    });
                                                                                }
                                                                                else {buttons5[i].addActionListener(new ActionListener(){
                                                                                    public void actionPerformed(ActionEvent event){
                                                                                        wrongAnswer(event);
                                                                                    }
                                                                                });
                                                                                }
                                                                                }
                                                                    }
                                                                });
                                                            }

                                                            else {buttons4[i].addActionListener(new ActionListener(){
                                                                public void actionPerformed(ActionEvent event){
                                                                    wrongAnswer(event);
                                                                }
                                                            });
                                                            }
                                                            }
                                                }
                                            });
                                        }
                                        else {buttons3[i].addActionListener(new ActionListener(){
                                            public void actionPerformed(ActionEvent event){
                                                wrongAnswer(event);
                                            }
                                        });
                                        }
                                        }
                                        }
                                    });
                                }

                                else {buttons[i].addActionListener(new ActionListener(){
                                    public void actionPerformed(ActionEvent event){
                                        wrongAnswer(event);
                                    }
                                });
                                }
                                }


                    }
                });
            }
                else {buttons[i].addActionListener(new ActionListener(){
                                    public void actionPerformed(ActionEvent event){
                                        wrongAnswer(event);
                                    }
                                });
            }
        }
        }

    public void correctAnswer(ActionEvent e){
                        q = "Correct!";
                        y = "";
                    }

    public void wrongAnswer(ActionEvent e){
                main.lives--;
                if(main.lives<=0){
                    panel.screen=0;
                    main.lives=5;
                }
                    q = "Wrong";
                    y = ":-(";
                }

    public void playSoundOnce(ActionEvent e)
    {
        seize = getAudioClip(getDocumentBase(), "Seize the Day 15.wav");
        seize.play();
    }
}


sorry for the lengthiness, but if I can figure this out I'm golden. There are no syntax errors, but clearly something is wrong with what I am trying to do here. Thanks!

Viewing all articles
Browse latest Browse all 51036

Latest Images

Trending Articles



Latest Images