Path: blob/master/SLICK_HOME/src/org/newdawn/slick/Sound.java
1456 views
package org.newdawn.slick;12import java.net.URL;34import org.newdawn.slick.openal.Audio;5import org.newdawn.slick.openal.SoundStore;6import org.newdawn.slick.util.Log;78/**9* A single sound effect loaded from either OGG or XM/MOD file. Sounds are allocated to10* channels dynamically - if not channel is available the sound will not play.11*12* @author kevin13*/14public class Sound {15/** The internal sound effect represent this sound */16private Audio sound;1718/**19* Create a new Sound20*21* @param url The location of the OGG or MOD/XM to load22* @throws SlickException Indicates a failure to load the sound effect23*/24public Sound(URL url) throws SlickException {25SoundStore.get().init();26String ref = url.getFile();2728try {29if (ref.toLowerCase().endsWith(".ogg")) {30sound = SoundStore.get().getOgg(url.openStream());31} else if (ref.toLowerCase().endsWith(".wav")) {32sound = SoundStore.get().getWAV(url.openStream());33} else if (ref.toLowerCase().endsWith(".aif")) {34sound = SoundStore.get().getAIF(url.openStream());35} else if (ref.toLowerCase().endsWith(".xm") || ref.toLowerCase().endsWith(".mod")) {36sound = SoundStore.get().getMOD(url.openStream());37} else {38throw new SlickException("Only .xm, .mod, .aif, .wav and .ogg are currently supported.");39}40} catch (Exception e) {41Log.error(e);42throw new SlickException("Failed to load sound: "+ref);43}44}4546/**47* Create a new Sound48*49* @param ref The location of the OGG or MOD/XM to load50* @throws SlickException Indicates a failure to load the sound effect51*/52public Sound(String ref) throws SlickException {53SoundStore.get().init();5455try {56if (ref.toLowerCase().endsWith(".ogg")) {57sound = SoundStore.get().getOgg(ref);58} else if (ref.toLowerCase().endsWith(".wav")) {59sound = SoundStore.get().getWAV(ref);60} else if (ref.toLowerCase().endsWith(".aif")) {61sound = SoundStore.get().getAIF(ref);62} else if (ref.toLowerCase().endsWith(".xm") || ref.toLowerCase().endsWith(".mod")) {63sound = SoundStore.get().getMOD(ref);64} else {65throw new SlickException("Only .xm, .mod, .aif, .wav and .ogg are currently supported.");66}67} catch (Exception e) {68Log.error(e);69throw new SlickException("Failed to load sound: "+ref);70}71}7273/**74* Play this sound effect at default volume and pitch75*/76public void play() {77play(1.0f, 1.0f);78}7980/**81* Play this sound effect at a given volume and pitch82*83* @param pitch The pitch to play the sound effect at84* @param volume The volumen to play the sound effect at85*/86public void play(float pitch, float volume) {87sound.playAsSoundEffect(pitch, volume * SoundStore.get().getSoundVolume(), false);88}8990/**91* Play a sound effect from a particular location92*93* @param x The x position of the source of the effect94* @param y The y position of the source of the effect95* @param z The z position of the source of the effect96*/97public void playAt(float x, float y, float z) {98playAt(1.0f, 1.0f, x,y,z);99}100101/**102* Play a sound effect from a particular location103*104* @param pitch The pitch to play the sound effect at105* @param volume The volumen to play the sound effect at106* @param x The x position of the source of the effect107* @param y The y position of the source of the effect108* @param z The z position of the source of the effect109*/110public void playAt(float pitch, float volume, float x, float y, float z) {111sound.playAsSoundEffect(pitch, volume * SoundStore.get().getSoundVolume(), false, x,y,z);112}113/**114* Loop this sound effect at default volume and pitch115*/116public void loop() {117loop(1.0f, 1.0f);118}119120/**121* Loop this sound effect at a given volume and pitch122*123* @param pitch The pitch to play the sound effect at124* @param volume The volumen to play the sound effect at125*/126public void loop(float pitch, float volume) {127sound.playAsSoundEffect(pitch, volume * SoundStore.get().getSoundVolume(), true);128}129130/**131* Check if the sound is currently playing132*133* @return True if the sound is playing134*/135public boolean playing() {136return sound.isPlaying();137}138139/**140* Stop the sound being played141*/142public void stop() {143sound.stop();144}145}146147148