Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  Problem with playing sound files simultaneously  (Read 1201 times)
0 Members and 1 Guest are viewing this topic.
Offline Chlorachrida

Junior Newbie




Java games rock!


« Posted 2005-01-22 22:44:10 »

Hello,

I am using the java sound to play sound files.

I'm using the javazoom spi:s for ogg and mp3 support.

http://www.javazoom.net/vorbisspi/vorbisspi.html

http://www.javazoom.net/mp3spi/mp3spi.html

However, i have a problem with playing two sound files at the same time with the same application. The weird thing is that some sound files work, you can play them simultaneously , but some other files might not work...

Anyone have any clue what may cause this?

Here is the code i'm using. SoundManager.java
It has two play methods, playMusic() and play(). It is coded so that it is only possible to play one single music at a time. But the problem occurs when for example calling playMusic() to start a music file, and then calling play() to play other sound fx on top of it. Some of the fx files will play, others wont...

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  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
import java.io.File;
 
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.DataLine;
 
public class SoundManager{
     
      private String basePath;
     
      private PlayThread music;
     
      public SoundManager(String path){
            this.basePath = path;
      }
     
      synchronized private AudioInputStream getAudioInputStream(File file)
            throws Exception{
            return AudioSystem.getAudioInputStream(file);
      }
     
      synchronized private AudioInputStream getAudioInputStream(
                  AudioFormat af, AudioInputStream in){
            return AudioSystem.getAudioInputStream(af, in);
      }
     
      synchronized private DataLine.Info getInfo(AudioFormat af){
            return new DataLine.Info(SourceDataLine.class, af);
      }
     
      synchronized private SourceDataLine getLineInfo(DataLine.Info info)
      throws Exception{
            return (SourceDataLine) AudioSystem.getLine(info);
      }
     
      public void play(String filename){
            // This should open up a new play thread for the sound to play.
           PlayThread pThread = new PlayThread(basePath+"/"+filename);
            pThread.start();
      }
     
      public void playMusic(String filename){
           
            if (music != null){
                  music.abort();
            }
           
            music = new PlayThread(basePath+"/"+filename);
            music.start();
      }
     
      public void stopMusic(){
            if (music != null){
                  music.abort();
            }
      }
     
     
      private class PlayThread extends Thread{
            String filename;
           
            AudioInputStream din = null;
            AudioFormat  decodedFormat;
            AudioInputStream in;
           
            SourceDataLine line;
           
            boolean running = true;
           
            public PlayThread(String filename){
                  this.filename = filename;
                  playFilename(filename);
            }
           
            public void run(){
                 
                  try{
                       
                        byte[] data = new byte[4096];
                        line = getLine(decodedFormat);            
                       
                        if (line != null){
                             // Start
                           line.start();
                           
                            int nBytesRead = 0, nBytesWritten = 0;
                           
                            while (nBytesRead != -1 && running){
                                nBytesRead = din.read(data, 0, data.length);
                                 
                                if (nBytesRead != -1)
                                      nBytesWritten = line.write(data, 0, nBytesRead);
                            }
                        }
                       
                        line.drain();
                      line.stop();
                      line.close();
                     
                      din.close();
                      in.close();
                     
                  }catch(Exception e){
                        e.printStackTrace();
                        System.out.println("Failed to play sound file: "+filename);
                  }
            }
           
            public void abort(){
                  this.running = false;
            }
           
            private void playFilename(String filename){
                  try{
               
                        File file = new File(filename);
                        // Get AudioInputStream from given file.      
                       in = getAudioInputStream(file);
                       
                        if (in != null){
                              AudioFormat baseFormat = in.getFormat();
                             
                              decodedFormat = new AudioFormat(
                                          AudioFormat.Encoding.PCM_SIGNED,
                                          baseFormat.getSampleRate(),
                                          16,
                                          baseFormat.getChannels(),
                                          baseFormat.getChannels() * 2,
                                          baseFormat.getSampleRate(),
                                          false);
                              // Get AudioInputStream that will be decoded by underlying VorbisSPI
                             din = getAudioInputStream(decodedFormat, in);            
                        }
                  }
                  catch (Exception e){
                        e.printStackTrace();
                  }
            }
 
            private SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException{
             
                  SourceDataLine res = null;
                  //DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
                 DataLine.Info info = getInfo(audioFormat);
                  //res = (SourceDataLine) AudioSystem.getLine(info);
                 try{
                        res = getLineInfo(info);
                  }catch(Exception e){
                        e.printStackTrace();
                  }
                  res.open(audioFormat);
                  return res;
            }
      }
}



Any help is greatly appreciated.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (76 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (119 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (125 views)
2013-05-15 15:01:13

theagentd (112 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (178 views)
2013-05-12 17:10:36

kutucuk (178 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.41 seconds with 20 queries.