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.htmlhttp://www.javazoom.net/mp3spi/mp3spi.htmlHowever, 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){ 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){ 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); 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); din = getAudioInputStream(decodedFormat, in); } } catch (Exception e){ e.printStackTrace(); } } private SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException{ SourceDataLine res = null; DataLine.Info info = getInfo(audioFormat); try{ res = getLineInfo(info); }catch(Exception e){ e.printStackTrace(); } res.open(audioFormat); return res; } } } |
Any help is greatly appreciated.