Sound
The JPlay, for now, only accepts wav files.
1 - Playing a sound
To execute a sound, just do:
//This command can be called from anywhere in the code. new Sound(nome_do_arquivo).play();
2 - Sound class methods
//Returns true if the sound is being played, otherwise, returns false. public boolean isExecuting(); //Pauses the sound. public void pause(); //Stops playing the sound. public void stop(); //Starts playing the sound. public void play(); //Sets the volume at which the sound will be played. public void setVolume(float value); //Decreases the sound volume. public void decreaseVolume(float value); //Increases the sound volume. public void increaseVolume(float value); //Pass true, if you want the sound to be repeated, false otherwise. //By default, the sound is set to play only once. public void setRepeat(boolean value);
Example: When it detects that the two objects collided, it plays a sound of explosion.
package Sound001; import jplay.Animation; import jplay.GameImage; import jplay.Sound; import jplay.Sprite; import jplay.Window; /** * @author Gefersom Cardoso Lima * Universidade Federal Fluminense - UFF - Brasil - 2010 * Ciência da Computação */ public class Sound001 { //At the time the bird collides with the rock it explodes and a sound is played. public static void main(String[] args) { Window window = new Window(800,600); GameImage stone = new GameImage("stone.png"); stone.x = 396; stone.y = 63; GameImage sky = new GameImage("sky.png"); Sprite bird = new Sprite("bird.png", 3); bird.setTotalDuration(360); Animation explosion = new Animation("explosion.png",20); explosion.setTotalDuration(1400); explosion.setLoop(false);//The animation will run only once. bird.x = 100; bird.y = 460; boolean collided = false; while(true) { sky.draw(); stone.draw(); if (collided == false) { bird.draw(); bird.update(); bird.moveTo(600, bird.y, 0.8); if (bird.collided(stone)) { collided = true; explosion.x = bird.x - 70; explosion.y = bird.y - 50; new Sound("explosion.wav").play(); } } else { explosion.update(); explosion.draw(); } if (explosion.isPlaying() == false && collided == true) explosion.hide(); window.update(); window.delay(10); } } }
UFF - Universidade Federal Fluminense - Institudo de Computação - Ciência da Computação