Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
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  
  Skipping Clips  (Read 1546 times)
0 Members and 1 Guest are viewing this topic.
Offline Kakashi266

Junior Duke





« Posted 2010-09-22 15:24:59 »

Hi all. It's my first time working with sound in Java and I feel like I'm back in high school trying to figure out a monster. I wanted to create sound effects for my game. I started with this class, that seemed very helpful at first:

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  
59  
60  
61  
62  
63  
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
  
/**
 * This enum encapsulates all the sound effects of a game, so as to separate the sound playing
 * codes from the game codes.
 * 1. Define all your sound effect names and the associated wave file.
 * 2. To play a specific sound, simply invoke SoundEffect.SOUND_NAME.play().
 * 3. You might optionally invoke the static method SoundEffect.init() to pre-load all the
 *    sound files, so that the play is not paused while loading the file for the first time.
 * 4. You can use the static variable SoundEffect.volume to mute the sound.
 */

public enum SoundEffect {
   EXPLODE("explode.wav"),   // explosion
   GONG("gong.wav"),         // gong
   SHOOT("shoot.wav");       // bullet
  
   // Nested class for specifying volume
   public static enum Volume {
      MUTE, LOW, MEDIUM, HIGH
   }
  
   public static Volume volume = Volume.LOW;
  
   // Each sound effect has its own clip, loaded with its own sound file.
   private Clip clip;
  
   // Constructor to construct each element of the enum with its own sound file.
   SoundEffect(String soundFileName) {
      try {
         // Use URL (instead of File) to read from disk and JAR.
         URL url = this.getClass().getClassLoader().getResource(soundFileName);
         // Set up an audio input stream piped from the sound file.
         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
         // Get a clip resource.
         clip = AudioSystem.getClip();
         // Open audio clip and load samples from the audio input stream.
         clip.open(audioInputStream);
      } catch (UnsupportedAudioFileException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } catch (LineUnavailableException e) {
         e.printStackTrace();
      }
   }
  
   // Play or Re-play the sound effect from the beginning, by rewinding.
   public void play() {
      if (volume != Volume.MUTE) {
         if (clip.isRunning())
            clip.stop();   // Stop the player if it is still running
         clip.setFramePosition(0); // rewind to the beginning
         clip.start();     // Start playing
      }
   }
  
   // Optional static method to pre-load all the sound files.
   static void init() {
      values(); // calls the constructor for all the elements
   }
}


It does work pretty well. But let's take one specific case. When I walk into a wall it's supposed to play a sound. That works, but if I hold the key down, sometimes the sound does not play. Seems like something is getting overwhelmed playing the sound over and over. It's not a total deal breaker for using this code, but I just wanted to know if there's any way to fix it. Also the sounds in my game are very simple, and only come from one basic direction, so I don't think I should have to use anything fancy like JOAL. However I could be wrong so let me know?

EDIT - That is not my code. I simply used it as a jumping off point. It is relatively unchanged in my game, aside from the constructor calls up top.

EDIT 2 - Well I figured out how to get past my problem for now, in a way I like, but I'd still like to know if there is anything I can do in the future if I need to rapidly play sounds. Thanks!

Offline paulscode

Senior Duke


Medals: 12


Staff Sergeant


« Reply #1 - Posted 2010-09-22 15:56:26 »

One easy way is to create a pool of clips that you draw from to play the sound effects.  That way you can for example play a particular sample multiple times simultaneously (if say 5 explosions happened at around the same time), and this fixes the issue of continuously stopping and restarting a single clip in order to play a particular sample in rapid succession.  Your clip pool size is generally going to be around 32, since that is the standard maximum.

We love death.  The US loves life.  That is the difference between us.  -Osama bin Laden, mass murderer
Offline Kakashi266

Junior Duke





« Reply #2 - Posted 2010-09-22 16:09:38 »

Ah. Yeah I was looking around the forum and it looked like you were the man to ask. I'll take a look at that option. I hope I don't need it. The game I'm working on is a Pokemon clone, and I consider those to be pretty much the ultimate closed system. Nothing new ever needs to be generated, so if there was ever a need for 5 explosions at once (like the move Explosion or Self Destruct, I just realized), there would probably be a sound effect just for it. But since the point of the game is to learn (and oh boy have I learned things), I'll give your suggestion a shot. Thanks, man.

EDIT - Woo I figured it all out. Mods you can close this puppy up. The solution I used can be found here: http://www.dreamincode.net/forums/index.php?app=blog&module=display&section=blog&blogid=114&showentry=1631

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.

DarkCart (16 views)
2014-10-31 21:44:48

DarkCart (21 views)
2014-10-31 21:43:57

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

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

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

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

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

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

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

Norakomi (62 views)
2014-10-16 15:22:06
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!