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  
  Game behaves weird when sound is playing  (Read 2072 times)
0 Members and 1 Guest are viewing this topic.
Offline alexxz4

Junior Duke


Projects: 1



« Posted 2008-02-16 01:50:55 »

Here is my Sound class (im completely clueless of what all this stuff means).
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  
class Sound{
   private AudioFileFormat baseFileFormat;
   private AudioFormat baseFormat,decodedFormat;
   private AudioInputStream din,in;
   private AudioFileFormat.Type type;
   private float frequency;
   public Sound(String filename){
      try{
         baseFileFormat = AudioSystem.getAudioFileFormat(new File(filename));
      }   catch(UnsupportedAudioFileException ex){
         System.out.println("Audio File not Supported");  
      }   catch(IOException ex2){}
      baseFormat = baseFileFormat.getFormat();
      type = baseFileFormat.getType();
      frequency = baseFormat.getSampleRate();
      in=null;
      try{
         in= AudioSystem.getAudioInputStream(new File(filename));
      }   catch(UnsupportedAudioFileException ex){}
         catch(IOException ex2){}
      din = null;
      baseFormat = in.getFormat();
      decodedFormat =new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                    baseFormat.getSampleRate(),
                    16,
                    baseFormat.getChannels(),
                    baseFormat.getChannels() * 2,
                    baseFormat.getSampleRate(),
                    false);
      din = AudioSystem.getAudioInputStream(decodedFormat, in);
      //in.close();
   }
   public void play(){
      try{
         rawplay(decodedFormat, din);
      }   catch(LineUnavailableException ex){}
         catch(IOException ex2){}
   }  
   public void stop(){
      try{
         in.close();
      }   catch(IOException ex2){}
   }
   private void rawplay(AudioFormat targetFormat,AudioInputStream din)
         throws IOException, LineUnavailableException{
       byte[] data = new byte[4096];
        SourceDataLine line = getLine(targetFormat);
        if (line != null){
          line.start();
          int nBytesRead = 0, nBytesWritten = 0;
          while (nBytesRead != -1){
              nBytesRead = din.read(data, 0, data.length);
              if (nBytesRead != -1)
               nBytesWritten = line.write(data, 0, nBytesRead);
          }
          line.flush();
          line.stop();
          line.close();
          din.close();
        }
   }
   private SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException{
        SourceDataLine res = null;
        DataLine.Info info =
          new DataLine.Info(SourceDataLine.class, audioFormat);
        res = (SourceDataLine) AudioSystem.getLine(info);
        res.open(audioFormat);
        return res;
   }
}

When i try playing a sound with it, the game freezes untill the sound stops playing. When i try playing it again, it wont play. The stop() function does not stop the sound. I have another question: how would i implement loop() function (one that works properly). To make long story short: is there a library-thingy for a java application which provides play(), stop() and loop() functions?  Sad

[ Motherload Unlimited looking for programmers ] [ Solitaire ]
Offline broumbroum

Junior Duke





« Reply #1 - Posted 2008-02-18 15:42:09 »

you may try to launch the player with a new Thread and stop should work.
Try like this :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
/** within the Sound class block add a Thread member */
Thread player = null;
/** add a new Thread for the play() to launch in the background **/
public Thread playInBg() {
// you may add some checks and conditions for avoiding simultaneous multiple threadings
if(player instanceof Thread) {
          if(player.isAlive() && !player.isInterrupted()) {
               player.interrupt();
          }
}
Thread t = player = new Thread(new Runnable() {public void run() { play(); Thread.currentThread().interrupt();}}, "T-soundplayer");
t.start();
return t;
}
Cool

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
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 (6 views)
2014-10-31 21:44:48

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

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

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

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

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

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

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

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

Norakomi (61 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!