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…
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.