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

Playing and Stopping Audio

$
0
0
Hey there, I need some help figuring out how to play an audio clip for a multiple choice game. I want the music to start right off the bat, and when they get the question right, the music stops and a new clip begins to play. The game itself works, I just need to know how to play my audio clips. Thanks!

public class Questions extends Screen {


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

	OptionButton button1 = new OptionButton(50,200,200,50, "Guns n Roses");
	OptionButton button2 = new OptionButton(450, 200, 200, 50, "A7X");
	OptionButton button3 = new OptionButton(50, 300, 200, 50, "Aerosmith");
	OptionButton button4 = new OptionButton(450, 300, 200, 50, "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);

			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 = "Name the SONG";

						button1 = new OptionButton(50, 200, 200, 50, "Civil War");
						button2 = new OptionButton(450, 200, 200, 50, "Seize the Day");
						button3 = new OptionButton(50, 300, 200, 50, "Walk this Way");
						button4 = new OptionButton(450, 300, 200, 50, "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 = "Name the DECADE";

								button1 = new OptionButton(50, 200, 200, 50, "90's");
								button2 = new OptionButton(450, 200, 200, 50, "00's");
								button3 = new OptionButton(50, 300, 200, 50, "70's");
								button4 = new OptionButton(450, 300, 200, 50, "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);
												}
											});
										}

										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=3;
				}
					q = "Wrong";
					y = ":-(";
				}

	public void playSoundOnce()
	{
		seize.play();
	}
}
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>