Better way to play sound effects
in
Contributed Library Questions
•
1 year ago
I am using the ESS sound library to play sound effects in a game. Every time a sound is played, an audio channel is created, the sound is loaded in, the channel plays, and the channel is deleted.
The problem with this method is that the audio file have to be pulled of the disk every time a sound needs to be played. I cannot just have one channel for each sound effect either, because then the sounds will be unable to play in rapid succession.
Here is my current audio class. I'm sure there is a much better way to play sounds, but I'm fairly new to OOP, and I can't think of a better way.
class Audio { void playSound(int selected, float volume) { //loads a sound into a channel myChannel = (AudioChannel[])expand(myChannel, myChannel.length + 1); //expand the array to
make room for the new audio track
myChannel[myChannel.length - 1] = new AudioChannel(selected + ".mp3"); //add the new rack myChannel[myChannel.length - 1].volume(volume); //set the volume myChannel[myChannel.length - 1].play(); //play the channel myChannel[myChannel.length - 1].cue(1); //increase the cue, to fix a problem where the cue
reads "0" for the first frame, setting off the terminator } void advance() { //runs every frame for (int i = 0; i < myChannel.length; i++) { //run for every channel if(myChannel[i].cue == 0) { //if the channel has finished playing... myChannel = (AudioChannel[])subset(myChannel,1,myChannel.length - 1); //delete the channel } } } }
Thanks for any help!
1