Sign in to follow this  
nickthelegend100

[java] Adding multiple sound clips to an Applet

Recommended Posts

This is my code to load the sound clips into the applet, but my problem is that only the first sound is playing and i'm not really sure what to do to make it actually work =/ import javax.swing.*; import java.applet.AudioClip; import java.awt.*; import java.awt.event.*; /** * *

Title: ICGGSApplet

*

Description: An applet that can run a game or show a movie

*

Copyright: Copyright (c) 2005

* @author Cathy French * @version 1.1 2006 */ public class ICGGSApplet extends JApplet implements Runnable { // constants describing possible game states private static final int SHOW_MOVIE = 1; private static final int PLAY_GAME = 2; private static final int STOPPED = 3; private int gameState; // the current game state private String name; // the player's name private long frameStartTime; // the time the last frame was displayed private Thread gameLoop; // thread to run the game and movie // associated objects private Movie theMovie; // Panel to display the movie private MonsterGame theGame; // Panel to display the game private ICGGSPanel currentPanel; // the currently displayed Panel (can be null) /** * Called when applet is first loaded * sets up the attributes, load media, creates game objects * and sets up interface */ public void init() { // neither the movie nor the game is currently playing currentPanel = null; gameState = STOPPED; name = ""; // load up the audio files AudioClip theAudio = getAudioClip(getDocumentBase(), "../media/chimes.wav"); AudioClip laser1 = getAudioClip(getDocumentBase(), "../media/laser1.wav"); AudioClip laser2 = getAudioClip(getDocumentBase(), "../media/laser2.wav"); AudioClip laser3 = getAudioClip(getDocumentBase(), "../media/laser3.wav"); AudioClip laser4 = getAudioClip(getDocumentBase(), "../media/laser4.wav"); // create a media tracker to monitor the loading of the images MediaTracker theTracker = new MediaTracker(this); // load up the images for the movie Image movieImages[] = new Image[24]; for (int i=0; i<movieImages.length; i++) { movieImages[i] = getImage(getDocumentBase(), "../media/movie" + (i+1) + ".gif"); theTracker.addImage(movieImages[i], i); } // load up the images for the munchie Image image1 = getImage(getDocumentBase(), "../media/munchie1.gif"); theTracker.addImage(image1, 4); Image image2 = getImage(getDocumentBase(), "../media/munchie2.gif"); theTracker.addImage(image2, 5); // load up the images for the player Image monster = getImage(getDocumentBase(), "../media/monster.gif"); theTracker.addImage(monster, 6); // load background image Image background = getImage(getDocumentBase(), "../media/background.png"); theTracker.addImage(background, 7); // wait for all the images to be loaded try { theTracker.waitForAll(); } catch (InterruptedException e) { } // create the movie object theMovie = new Movie(movieImages); // create the game object theGame = new MonsterGame(image1, image2, theAudio, laser1, laser2, laser3, laser4, monster); this.getContentPane().setBackground(Color.WHITE); // Initialise key-input functionality addKeyListener(new Input()); // add keyboard listener to applet //addMouseListener(this); // add mouse listener to applet } /** * Called when the Applet is ready to start executing */ public void start() { // ask the user for their name name = JOptionPane.showInputDialog(this, "What is your name?"); // start running the movie/game animation if (gameLoop == null) { gameLoop = new Thread(this); gameLoop.start(); } } /** * Called when the gameLoop thread is started * loops continuously until user quits */ public void run() { int input = Input.NONE; // game loop while (input != Input.QUIT) { // what is the current time? frameStartTime = System.currentTimeMillis(); input = checkInput(); if (gameState != STOPPED) // only necessary if the movie or game is showing { currentPanel.processInput(input); currentPanel.update(); currentPanel.repaint(); } else showStatus("Press 'G' to start game, 'V' to show movie"); frameDelay(10); } } /** * called when the Applet is stopped * stops the gameLoop thread */ public void stop() { if (gameLoop != null) { gameLoop.interrupt(); // stops the animation thread gameLoop = null; } } /** * called from the gameLoop * checks for the last key stroke input by the user * @return int - Input class constant representing user input */ public int checkInput() { int input = Input.checkUserInput(); switch (input) // how the input is handled depends on the current game state { case Input.GAME: // the user wants to play the game if (gameState == STOPPED) // not currently playing a game or movie { // display the panel showing the game currentPanel = theGame; this.getContentPane().add(BorderLayout.CENTER, currentPanel); this.validate(); // set up a new game theGame.resetGame(); gameState = PLAY_GAME; showStatus("Use W,A,S,D keys to move Ship, and B,N,M to fire Specials, Q key to stop"); } // otherwise ignore break; case Input.MOVIE: // the user wants to see the movie if (gameState == STOPPED) // not currently playing a game or movie { // display the panel showing the movie currentPanel = theMovie; this.getContentPane().add(BorderLayout.CENTER, currentPanel); this.validate(); gameState = SHOW_MOVIE; showStatus("Press Q key to stop"); } // otherwise ignore break; case Input.STOP: // the user wants to stop the movie or game being shown if (gameState == SHOW_MOVIE ) { this.getContentPane().remove(currentPanel); currentPanel = null; JOptionPane.showMessageDialog(this, "OK " + name + " I've stopped the movie"); } else if (gameState == PLAY_GAME) { this.getContentPane().remove(currentPanel); currentPanel = null; JOptionPane.showMessageDialog(this, "OK " + name + " I've stopped the game"); } gameState = STOPPED; showStatus("Press 'G' to start game, 'V' to show movie"); break; } Input.reset(); // reset the input module requestFocus(); this.repaint(); // redisplay the applet return input; // return for further processing (eg to control movement of Monster) } //public void mouseClicked(MouseEvent e) //{ //} /** * called from the gameLoop * pauses between frames * @param millis long - minimum frame time */ public void frameDelay(long millis) { long currentTime = System.currentTimeMillis(); // what time is it now? long elapsedTime = frameStartTime - currentTime; // how long has it been since the start of the frame? if (elapsedTime < millis) // is it time to showing the new frame yet? try { Thread.sleep(millis - elapsedTime); // pause for the remaining time } catch(Exception e) { } } /** * Put any clean-up code here */ public void finaliseGame() { } }

Share this post


Link to post
Share on other sites
it's actually all the sounds that i am trying to play, the first one, theAudio will play but the rest of them will not. I'm not sure if there is a special method needed to play multiple sounds, not at once but just to play them when a key is pressed.

public MonsterGame(Image image1, Image image2, AudioClip theAudio, AudioClip laser1, AudioClip laser2,
AudioClip laser3, AudioClip laser4, Image monster)
{
// initialise weapon sounds
weaponStandard = laser1;
weapon1 = laser2;
weapon2 = laser3;
weapon3 = laser4;

monsterBumpWallSound = theAudio;
}

public void update()
{
boardHeight = this.getHeight();
// update moster direction and animation
switch (monsterDirection)
{
case Input.RIGHT:
// monster is moving right, increase its X-coordinate
monsterXPos = monsterXPos + monsterSpeed;
if (monsterXPos > (boardWidth - monsterSize)) // has the monster hit the right wall?
{
monsterXPos = boardWidth - monsterSize; // move it back to just touching right wall
monsterBumpWallSound.play(); // play an appropriate sound
}
break;
case Input.LEFT:
// monster is moving left, decrease its X-coordinate
monsterXPos = monsterXPos - monsterSpeed;
if (monsterXPos < 0) // has the monster hit the left wall?
{
monsterXPos = 0; // move it back to just touching left wall
monsterBumpWallSound.play(); // play an appropriate sound
}
break;
case Input.UP:
// monster is moving up, decrease its Y-coordinate
monsterYPos = monsterYPos - monsterSpeed;
if (monsterYPos < 80) // has the monster hit the top wall?
{
monsterYPos = 80; // move it back to just touching top wall
monsterBumpWallSound.play(); // play an appropriate sound
}
break;
case Input.DOWN:
// monster is moving down, increase its Y-coordinate
monsterYPos = monsterYPos + monsterSpeed;
if (monsterYPos > (boardHeight - monsterSize)) // has the monster hit the bottom wall?
{
monsterYPos = boardHeight - monsterSize; // move it back to just touching bottom wall
monsterBumpWallSound.play(); // play an appropriate sound
}
break;

}
//monster weapon selection and sounds
switch (monsterWeapons)
{
case Input.SHOOT:
monsterShootXPos = (monsterXPos + 80);
monsterShootYPos = (monsterYPos + 40);
weaponStandard.play();
break;
case Input.WEAPON1:
monsterShootXPos = (monsterXPos + 80);
monsterShootYPos = (monsterYPos + 40);
weapon1.play();
break;
case Input.WEAPON2:
monsterShootXPos = (monsterXPos + 80);
monsterShootYPos = (monsterYPos + 40);
weapon2.play();
break;
case Input.WEAPON3:
monsterShootXPos = (monsterXPos + 80);
monsterShootYPos = (monsterYPos + 40);
weapon3.play();
break;
}
}

Share this post


Link to post
Share on other sites
Do you reset monsterWeapons after every frame? I.e., sometime after the update code is called.

If you don't, the weapon handling code in the //monster weapon selection and sounds section is going to play weapon sounds every single frame. So the sounds would be getting played, but they would only get to play a very short amount before they get played again (from the start) in the very next frame.

The appearance would be that the sound isn't playing, when in fact it is - but it keeps getting restarted before it gets anywhere.

Share this post


Link to post
Share on other sites
What happens if you replace monsterBumpWallSound with the other sounds temporarily? I.e., try putting "monsterBumpWallSound = weaponStandard;" in MonsterGame's constructor, at the end.

This will let you see if there's something with those sound files in particular that Java isn't liking. The weapon sounds should play in place of the bump-wall sound. (Checking for an error explicitly, like via Applet.getAudioClip's return value or an exception from AudioClip.play would be nicer, but the docs indicate that they don't do either of those)

If the weapon sounds works when put in place of the bump-wall sound, are you sure that a case in your 'monster weapon selection and sounds' switch statement is being executed when you expect it to be? Try stepping through it with a debugger, or adding some tracing logs.

Share this post


Link to post
Share on other sites
Yeh, there was a problem with the sound files, I had downloaded them as mp3's and not wav files. I converted them to wav and they play under the monsterBumpWallSound variable but not as thier own. I don't think it's a problem with the switch statement because the monsterShootXPos and monsterShootYPos values are being recorded and the animation is correct and functioning, it's just the sound that won't play...

Share this post


Link to post
Share on other sites

/**
* Called when the Applet is ready to start executing
*/

public void start()
{
// ask the user for their name
name = JOptionPane.showInputDialog(this, "What is your name?");
// start running the movie/game animation
if (gameLoop == null) {
gameLoop = new Thread(this);
gameLoop.start();
}
}

/**
* Called when the gameLoop thread is started
* loops continuously until user quits
*/


public void run()
{
int input = Input.NONE;
// game loop
while (input != Input.QUIT)
{
// what is the current time?
frameStartTime = System.currentTimeMillis();

input = checkInput();

if (gameState != STOPPED) // only necessary if the movie or game is showing
{
currentPanel.processInput(input);
currentPanel.update();
currentPanel.repaint();
}
else
showStatus("Press 'G' to start game, 'V' to show movie");
frameDelay(50);
}
}

/**
* called when the Applet is stopped
* stops the gameLoop thread
*/


public void stop()
{
if (gameLoop != null)
{
gameLoop.interrupt(); // stops the animation thread
gameLoop = null;
}
}

/**
* called from the gameLoop
* checks for the last key stroke input by the user
* @return int - Input class constant representing user input
*/

public int checkInput()
{
int input = Input.checkUserInput();
switch (input) // how the input is handled depends on the current game state
{
case Input.GAME:
// the user wants to play the game
if (gameState == STOPPED) // not currently playing a game or movie
{
// display the panel showing the game
currentPanel = theGame;
this.getContentPane().add(BorderLayout.CENTER, currentPanel);
this.validate();
// set up a new game
theGame.resetGame();
gameState = PLAY_GAME;
showStatus("Use W,A,S,D keys to move Ship, and B,N,M to fire Specials, Q key to stop");
} // otherwise ignore
break;
case Input.GAMEOVER:
this.getContentPane().remove(currentPanel);
currentPanel = null;
JOptionPane.showMessageDialog(this,
"GAME OVER");

gameState = STOPPED;
showStatus("Press 'G' to start game, 'V' to show movie");
break;
case Input.MOVIE:
// the user wants to see the movie
if (gameState == STOPPED) // not currently playing a game or movie
{
// display the panel showing the movie
currentPanel = theMovie;
this.getContentPane().add(BorderLayout.CENTER, currentPanel);
this.validate();
gameState = SHOW_MOVIE;
showStatus("Press Q key to stop");
} // otherwise ignore
break;
case Input.STOP:
// the user wants to stop the movie or game being shown
if (gameState == SHOW_MOVIE )
{
this.getContentPane().remove(currentPanel);
currentPanel = null;
JOptionPane.showMessageDialog(this,
"OK " + name + " I've stopped the movie");
}
else if (gameState == PLAY_GAME)
{
this.getContentPane().remove(currentPanel);
currentPanel = null;
JOptionPane.showMessageDialog(this,
"OK " + name + " I've stopped the game");
}
gameState = STOPPED;
showStatus("Press 'G' to start game, 'V' to show movie");
break;
}

Input.reset(); // reset the input module
requestFocus();
this.repaint(); // redisplay the applet
return input; // return for further processing (eg to control movement of Monster)

}

/**
* called from the gameLoop
* pauses between frames
* @param millis long - minimum frame time
*/


public void frameDelay(long millis)
{
long currentTime = System.currentTimeMillis(); // what time is it now?
long elapsedTime = frameStartTime - currentTime; // how long has it been since the start of the frame?
if (elapsedTime &lt; millis) // is it time to showing the new frame yet?
try
{
Thread.sleep(millis - elapsedTime); // pause for the remaining time
}
catch(Exception e)
{
}
}



/**
* Put any clean-up code here
*/


public void finaliseGame()
{
}

Share this post


Link to post
Share on other sites
I managed to get the relevant code out of your other thread.

Like I said earlier, you need to be resetting monsterWeapons each frame, which you're not doing.

Try changing this part of code, in MonsterGame.processInput:

if (input > 8 && input < 12)
{
monsterWeapons = input;
}

to something like:

if (input > 8 && input < 12)
{
monsterWeapons = input;
}
else
{
monsterWeapons = 0;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this