Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
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  
  Sound Blocking Problem[SOLVED]  (Read 532 times)
0 Members and 1 Guest are viewing this topic.
Offline SteM

Junior Newbie





« Posted 2012-01-01 11:18:56 »

Hello guys,

i need help with playing sound effects in a game.

i have the following class for playing sound effects

SoundManager:

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  
public class SoundManager implements Runnable
{
   private int soundID;
   
   public SoundManager(int soundID)
   {
      this.soundID = soundID;
   }

   @Override
   public void run()    
   {  
     
      File file = new File (".\\Sounds\\sound" + soundID + ".wav");
     
      AudioInputStream audioStream = null;
      AudioFormat format = null;
      SourceDataLine sourceLine = null;
      int BUFFER_SIZE = 128000;
     
      try {
         audioStream = AudioSystem.getAudioInputStream(file);
      } catch (UnsupportedAudioFileException e) {
         // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
        e.printStackTrace();
      }
      format = audioStream.getFormat();
     
          DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
           try {
               sourceLine = (SourceDataLine) AudioSystem.getLine(info);
               sourceLine.open(format);
           } catch (LineUnavailableException e) {
               e.printStackTrace();
               System.exit(1);
           } catch (Exception e) {
               e.printStackTrace();
               System.exit(1);
           }

           sourceLine.start();

           int nBytesRead = 0;
           byte[] abData = new byte[BUFFER_SIZE];
           while (nBytesRead != -1) {
               try {
                   nBytesRead = audioStream.read(abData, 0, abData.length);
               } catch (IOException e) {
                   e.printStackTrace();
               }
               if (nBytesRead >= 0) {
                   @SuppressWarnings("unused")
                   int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
               }
           }

           sourceLine.drain();
           sourceLine.close();
   }
}      


And when there is a collision, i create enw instance of the sound manager and call .run() method:

1  
2  
SoundManager soundManager = new SoundManager(1);
soundManager.run();


but this is blocking the game rendering until the sound is played, i thought running a seperate thread for sound, fixes the problem but it did not.
What can i do for sounds not to block the game ?
Offline Waterwolf

Junior Member


Medals: 3



« Reply #1 - Posted 2012-01-01 11:27:48 »

That's not a thread. You're just calling a method normally. To make it run in a thread replace
1  
soundManager.run()
with
1  
new Thread(soundManager).start();
That's not an optimal solution though because you load sounds from a file everytime you play them, thus generating massive IO traffic. Instead you should cache SoundManagers beforehand and use single instances of them from the main logic thread. And if you for some reason (there are pretty much none) decide to go with threaded way, at least use thread pools.
Offline SteM

Junior Newbie





« Reply #2 - Posted 2012-01-01 11:52:31 »

Thanks for your reply, it works well now. I know there is a io time also but my sound clips are small and short .wav files like laser 'peow' sound thus it works. Thanks for your help i appreciate it Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2012-01-02 01:21:32 »

Because fact that your peow sound clips are small means that you'll call it rapidly. That's the io problem.

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.

CogWheelz (16 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!