Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
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  
  REALLY Simple Sound Loader  (Read 1165 times)
0 Members and 1 Guest are viewing this topic.
Offline kpars

JGO Ninja


Medals: 57
Projects: 4
Exp: 2 years


Kemoy Labs/Radirius Game/Engine Developer


« Posted 2013-06-11 21:14:57 »

I was messing around seeing what I could do with a basic soundloader, and this was the result.

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  
64  
65  
66  
67  
68  
69  
70  
71  
72  
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Sound {
   public static Sound exampleSound = loadSound("/soundPath/yoursound.wav");

   private Clip clip;

   public static Sound loadSound(String fileName) {
      Sound sound = new Sound();
      try {
         AudioInputStream ais = AudioSystem.getAudioInputStream(Sound.class
               .getResource(fileName));
         Clip clip = AudioSystem.getClip();
         clip.open(ais);
         sound.clip = clip;
      } catch (Exception e) {
         System.out.println(e);
      }
      return sound;
   }

   public void loop(final int count) {
      try {
         if (clip != null)
            new Thread() {
               public void run() {
                  synchronized (clip) {
                     clip.stop();
                     clip.setFramePosition(0);
                     clip.loop(count);
                  }
               }
            }.start();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public void stop() {
      try {
         if (clip != null)
            new Thread() {
             public void run() {
                synchronized (clip) {
                   clip.stop();
                }
             }
         }.start();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public void play() {
      try {
         if (clip != null)
            new Thread() {
               public void run() {
                  synchronized (clip) {
                     clip.stop();
                     clip.setFramePosition(0);
                     clip.start();
                  }
               }
            }.start();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}


It's not brilliant or anything amazing, but I figured I'd share it.  Smiley

"Living is easy with eyes closed, misunderstanding all you see. It's getting hard to be someone, but it all works out." ¤¤ Kemoy Labs: http://www.kemoy.net/
Offline davidc

Senior Member


Medals: 5
Projects: 2



« Reply #1 - Posted 2013-06-12 02:20:31 »

For the sake of anyone who might want to use this code, I would advise you get rid of those catch blocks and declare the exceptions using throws. You are not doing anything useful with those exceptions, and the end result will be that your users will keep getting NullPointerExceptions after the initial failure. Let the programmers using your code decide how they want to handle the error rather than hiding it from them.

I have no idea why people insist on catching everything. 95% of the time it is the worst thing you can do, and yet the net is full of example showing this. And new coders just blindly follow.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2013-06-12 17:27:11 »

Not a single Exception can be thrown in the 3 last methods anyway. If anything goes wrong, it's on another (short lived) thread.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie
« Reply #3 - Posted 2013-06-12 17:40:28 »

I have no idea why people insist on catching everything. 95% of the time it is the worst thing you can do, and yet the net is full of example showing this. And new coders just blindly follow.

"Because Eclipse complains unless I fix it".  So they blindly take the fix without ever bothering to understand why.  It doesn't help that the default fix in every IDE is a crappy one (a better one would have been to convert it to a RuntimeException).  IDEs are a double-edged sword, and this sort of thing is Exhibit A for the downside.
Offline gimbal

JGO Coder


Medals: 25



« Reply #4 - Posted 2013-06-13 09:49:13 »

I have no idea why people insist on catching everything. 95% of the time it is the worst thing you can do, and yet the net is full of example showing this. And new coders just blindly follow.

"Because Eclipse complains unless I fix it".  So they blindly take the fix without ever bothering to understand why.  It doesn't help that the default fix in every IDE is a crappy one (a better one would have been to convert it to a RuntimeException).  IDEs are a double-edged sword, and this sort of thing is Exhibit A for the downside.


Thanks, that's a really logical explanation for something I was wondering about for a very long time. IDE fixes, of course.
Offline davidc

Senior Member


Medals: 5
Projects: 2



« Reply #5 - Posted 2013-06-13 10:08:45 »

I have no idea why people insist on catching everything. 95% of the time it is the worst thing you can do, and yet the net is full of example showing this. And new coders just blindly follow.

It doesn't help that the default fix in every IDE is a crappy one (a better one would have been to convert it to a RuntimeException).


Or even better, just use throws and let it be handled in a more appropriate place, without killing the original stack trace. So much better in so many different ways, and costs less effort. But anyway, this is getting a bit off topic (apologies on my part).

I'm wondering if it will work under OpenJDK on Linux when multiple sounds are played at once? I've seen other users from JGO report issues with using Clip in this way.
Offline nsigma
« Reply #6 - Posted 2013-06-13 11:32:34 »

I'm wondering if it will work under OpenJDK on Linux when multiple sounds are played at once? I've seen other users from JGO report issues with using Clip in this way.

Don't use Clip, period!  They perform horribly everywhere for doing this sort of thing, though particularly noticeable on Linux, because they open a line to the soundcard for every sound.  Simply open a single audio line and mix all sounds to it, or even simpler use a library that already does this - eg. TinySound.

Also, what's the deal with all those Threads?  Sorry, but this code should be mercifully euthanised!   Wink

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline kpars

JGO Ninja


Medals: 57
Projects: 4
Exp: 2 years


Kemoy Labs/Radirius Game/Engine Developer


« Reply #7 - Posted 2013-06-13 14:51:55 »

Actually, yes. Yesterday I tried running 25 sounds at once as a bit of a stress test.
Eventually about 10 of them stopped playing.

I can completely agree with you, this isn't a great sound system.
Still works for the simplest uses, but if you are using a ton of sounds, music, etc. then you are definitely going to change to a different sound loader.

"Living is easy with eyes closed, misunderstanding all you see. It's getting hard to be someone, but it all works out." ¤¤ Kemoy Labs: http://www.kemoy.net/
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.

xsi3rr4x (11 views)
2014-04-15 18:08:23

BurntPizza (10 views)
2014-04-15 03:46:01

UprightPath (23 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (107 views)
2014-04-08 23:06:04

tom_mai78101 (207 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!