Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (515)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [LibGDX] Queue Sound Effects  (Read 883 times)
0 Members and 1 Guest are viewing this topic.
Offline Gibbo3771
« Posted 2014-04-22 08:00:56 »

Hi, I used this little class in a recent project to "Queue" or rather play 1 sound effect after another with a delay.

It worked quite nice, someone might find a use for it, very simple. It can be built on, to remove tracks and swap track order but heh I just made this quickly.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
public class SoundSequencer {

   /** A list of tracks to play in a sequence */
   Array<Sound> sounds = new Array<Sound>();

   /** The index to play */
   int index = 0;

   /** The delay between tracks */
   double delay;

   /** The last time a track was played */
   double lastPlayed;

   /** Add a sound to the sequencer */
   public void addSound(String path) {
      Sound sound = Gdx.audio.newSound(Gdx.files.internal(path));
      if (sounds.contains(sound, true))
         return;
      sounds.add(sound);
   }
   

   /**
    * Play the sound sequence with a given delay
    *
    * @param volume 0 - 1
    * @param delay
    *            in seconds
    */

   public void play(float volume, double delay) {
      if (TimeUtils.nanoTime() - lastPlayed > delay / 1000000000) {
         play(volume);
      }
   }

   /**
    * Play the sound track
    *
    * @param volume 0 - 1
    */

   public void play(float volume) {
      if (sounds.get(index) != null) {
         sounds.get(index).play(volume);
         lastPlayed = TimeUtils.nanoTime();
      }
      nextTrack();
   }

   /** Move to the next track on the play list */
   private void nextTrack() {
      if (index + 1 > sounds.size - 1) {
         index = 0;
      } else {
         index += 1;
      }
   }
}

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Grunnt

JGO Kernel


Medals: 92
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #1 - Posted 2014-04-22 12:20:51 »

How does this Queuing work? As far as I can see no queuing takes place (as in one sound plays after the other). How do you determine when a sound has finished playing?

Offline Gibbo3771
« Reply #2 - Posted 2014-04-22 12:32:01 »

How does this Queuing work? As far as I can see no queuing takes place (as in one sound plays after the other). How do you determine when a sound has finished playing?

I used it for my Space Invaders game, the play method would get called each time all the aliens would move.

It would then increase the index by one, meaning the next sound effect to be played is the one in the list. So it would sort of "queue" them in a sense.

Imagine you had an enemy that had 3 hearts, each time you damage them, the sound they make is different. So you could call this each time you damage the enemy, playing the next sound effect in the list.

You could easily expand on it to play a random sound effect in the list as well.

Here is what I used it in:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
// I added 3 sounds effects to each sequence using a loop

      SoundSequencer invadermove = new SoundSequencer();
      SoundSequencer invaderfast = new SoundSequencer();
     
      for(int x = 1; x <= 3; x++){
         invadermove.addSound("data/sounds/move"+x+".wav");
         invaderfast.addSound("data/sounds/fast"+x+".wav");
         
      }



Then at the bottom of my invaders move loop:

1  
2  
3  
4  
5  
6  
         if (alienCount > 1) {
            invadermove.play(1f);
         } else {
            invaderfast.play(1f,
                  0.10f);
         }


So if we have more than 1 alien, we just play the regular invader sound with a volume of 1f.

If we have 1 alien, we play the "fast" sound effect, with a volume of 1 with a delay of 0.10f. This moves to the next sound in the queue every 0.10s.

If that makes sense, maybe queue is not the right word.

Any suggestions?

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza
« Reply #3 - Posted 2014-04-22 14:44:37 »

Any suggestions?

Add a varargs parameter to addSound() (addSounds()?) that is either an int telling how many times to repeat the same sound, or multiple sounds, removes for-loop boilerplate.

Have an easy way to play multiple sounds at once, like a queue of bundles of sounds.
Offline Grunnt

JGO Kernel


Medals: 92
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #4 - Posted 2014-04-23 06:27:18 »

Okay, thanks for the explanation, sounds useful!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (25 views)
2014-10-26 10:14:04

theagentd (31 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!