Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Game Freezes for Duration of Sounds  (Read 728 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2013-04-24 01:20:13 »

I have just implemented my very first sound "engine" for my game and did it probably very messily because I don't know a whole lot about sounds and Java. Anyways, I have this class:

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  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
package Engine;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class SoundEngine {

   private static final int BUFFER_SIZE = 128000;
   private static File soundFile;
   public static boolean isLoop = false;
   private static AudioInputStream audioStream;
   private static AudioFormat audioFormat;
   private static SourceDataLine sourceLine;

   public static void play(String filename) {

      String strFilename = filename;

      try {
         soundFile = new File(Frame.class.getResource("imgs/" + strFilename).getFile());
      } catch (Exception e) {
         e.printStackTrace();
         System.exit(1);
      }

      try {
         audioStream = AudioSystem.getAudioInputStream(soundFile);
      } catch (Exception e) {
         e.printStackTrace();
         System.exit(1);
      }

      audioFormat = audioStream.getFormat();

      DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
      try {
         sourceLine = (SourceDataLine) AudioSystem.getLine(info);
         sourceLine.open(audioFormat);
      } 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 && isLoop != false) {
         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();
   }

   public static void playSound(final String fileName, final boolean isLoopw) {
      isLoop = isLoopw;
      Thread t = new Thread(new Runnable() {
         public void run() {
            play(fileName);
         }
      });

      t.setDaemon(true);
      t.start();
   }

   public static void playSound(final String fileName) {
      Thread t = new Thread(new Runnable() {
         public void run() {
            play(fileName);
         }
      });

      t.setDaemon(true);
      t.start();
   }
}


and this class:

1  
2  
3  
4  
5  
6  
7  
8  
package Engine;

public class Sounds {

   public static String explosion = "explosion.wav";
        public static String inGameMusic = "inGameMusic.wav";

}

which holds all the names for my game. My problem is, whenever I call a sound, namely the explosion because that's the only one I have right now, the game freezes for the duration of the sound, and then continues when the sound is done. I think this definitely has something to do with Threads, but I am not confident in that area at all either so should I multi-thread, one for the game, and one for the sounds and if so, how?

Thanks,
-cMp

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Online HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-04-24 01:24:20 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
Thread t = new Thread(new Runnable()
{
    public void run()
    {
        SoundEngine.playSound(fileName);
    }
});

t.setDaemon(true);
t.start();


EDIT: Added in setDaemon(true); to avoid playing sounds after exit.

Offline Slyth2727
« Reply #2 - Posted 2013-04-24 01:27:07 »

Ok, I understand the basic thread calling and making after looking at that and a bit of research, but I wanted the method (playSound) to be something I could just call anywhere. Is there a way to do this, or would I have to type this out every time? I'm guessing just a simple method in my main class would do this but there may be side effects that I am unaware of.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2013-04-24 01:28:33 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public static void playSound(fileName)
{
    Thread t = new Thread(new Runnable()
    {
        public void run()
        {
            SoundEngine.playSound(fileName);
        }
    });
    t.setDaemon(true);
    t.start();
}

Offline Slyth2727
« Reply #4 - Posted 2013-04-24 01:30:57 »

Awesome, this is great and works perfectly! It's always good to learn something new Smiley

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Slyth2727
« Reply #5 - Posted 2013-04-24 02:05:24 »

EDIT: This is really awesome, but I have a single problem. For my background music, I start playing it at the beginning, but as soon as another sound is called the music stops. Any reasons this would happen with my current setup?
EDIT 2: The way I access this is by static access. e.g; SoundEngine.playSound(xxx, false);

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Online HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #6 - Posted 2013-04-24 02:18:03 »

You're using static, which means there is only one sound ever being played.

This should work:

New Code

Offline Slyth2727
« Reply #7 - Posted 2013-04-24 02:34:39 »

It does, and very well Smiley my game is totally different with music in it!

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline nsigma
« Reply #8 - Posted 2013-04-24 12:30:34 »

Never use a Thread per sound, or open multiple lines at the same time - it'll break or play badly on a variety of systems.  If you don't want to get into handling sound mixing yourself, check out TinySound or other libraries on here.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
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.

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

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

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

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

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

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

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

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

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
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!