简体   繁体   中英

Java TargetDataLine and SourceDataLine reopen doesn't work

My setup is as followed:

I have a Java applet running in a browser which records and plays audio.

My problem is:

When I refresh the browser, the SourceDataLine is reopening properly after refresh, while the TargetDataLine isn't reopening itself.

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");
        }

}

What is wrong with my code?

As Andrew said, you need to close the opened sourceDataline and TargetData line. As you refresh browser the init() is called again as it's new instance of your applet. If datalines are already open and you are trying to open it again it throws you exception kinda "Line is not supported:audioFormat". As destroy() is called every time you ends the instance of an applet, you need to handle closing of opened datalines in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM