Java TargetDataLine和SourceDataLine重新打开不起作用

时间:2012-11-22 11:08:57

标签: java audio applet javasound

我的设置如下:

我在浏览器中运行了一个Java applet,用于记录和播放音频。

我的问题是:

当我刷新浏览器时,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");
        }

}

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

正如Andrew所说,你需要关闭打开的sourceDataline和TargetData行。刷新浏览器时,会再次调用init(),因为它是applet的新实例。如果datalines已经打开并且你试图再次打开它会抛出异常,有点“不支持Line:audioFormat”。每当你结束applet的实例时都会调用destroy(),你需要处理它中打开的datalines的关闭。