Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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 1354 times)
0 Members and 1 Guest are viewing this topic.
Offline Chlorachrida

Junior Newbie




Java games rock!


« Posted 2005-01-22 21: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.

 

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

The first screenshot will be displayed as a thumbnail.

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

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

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
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!