我的设置如下:
我有一个在记录和播放音频的浏览器中运行的 Java 小程序。
我的问题是:
当我刷新浏览器时,SourceDataLine 会在刷新后正确重新打开,而 TargetDataLine 不会自行重新打开。
public void init() {
try {
DataLine.Info sourceDataLineInfo = new DataLine.Info(
SourceDataLine.class, audioFormat);
DataLine.Info targetDataLineInfo = new DataLine.Info(
TargetDataLine.class, audioFormat);
// Setup a Line.Info instance specifically of the TargetDataLine class.
Line.Info targetDLInfo = new Line.Info(TargetDataLine.class);
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
Mixer currentMixer = null;
try {
for(int cnt = 0; cnt < mixerInfo.length; cnt++) {
// Get a temporary instance of the current mixer
currentMixer = AudioSystem.getMixer(mixerInfo[cnt]);
if( currentMixer.isLineSupported(targetDLInfo) ) {
Log.log("Found mixer:" + mixerInfo[cnt].getName());
System.out.println(mixerInfo[cnt].getName());
break;
}
//currentMixer = null;
}
} catch(Exception e) {
Log.log("Found no mixer");
}
if(!Client.refresh) {
try {
sourceDataLine = (SourceDataLine) AudioSystem
.getLine(sourceDataLineInfo);
}catch(Exception e){
Log.log("Unable to stream audio not starting playthread");
}
play = new PlayThread();
if(sourceDataLine != null) {
sourceDataLine.open(audioFormat);
sourceDataLine.start();
play.start();
}
try {
targetDataLine = (TargetDataLine) currentMixer.getLine(targetDataLineInfo);
}catch(Exception e) {
connection.addMessage("[WARNING] Your microphone is not working.");
}
capture = new CaptureThread();
if(currentMixer != null) {
if(targetDataLine != null) {
targetDataLine.open(audioFormat);
targetDataLine.start();
capture.start();
}
}else {
connection.addMessage("[WARNING] No compatible microphone found.");
Log.log("Not able to record data since no mixer was found");
}
} else {
sourceDataLine.open(audioFormat);
sourceDataLine.start();
targetDataLine.open(audioFormat);
targetDataLine.start();
}
} catch (Exception e) {
Log.log("An exception occured when trying to startup the audio");
}
}
我的代码有什么问题?