开发者

Losing sound component on refresh

开发者 https://www.devze.com 2023-02-18 00:55 出处:网络
I\'ve written an applet that streams sound data from the server and plays it through the client\'s computer. I have the client get hold of the speaker line with the following function:

I've written an applet that streams sound data from the server and plays it through the client's computer. I have the client get hold of the speaker line with the following function:

private SourceDataLine getSourceDataLine() {
    try {
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        for (Mixer.Info mi : AudioSystem.getMixerInfo()) {
            SourceDataLine dataline = null;
            try {
                Mixer mixer = AudioSystem.getMixer(mi);
                dataline = (SourceDataLine)mixer.getLine(info);
                dataline.open(format);
                dataline.start();
                return dataline;
            }
            catch (Exception e) {  
                e.printstacktrace();  
            }
            if (dataline != null) 
                try {
                    dataline.close();
                }
                catch (Exception e) {  
                    e.printstacktrace();  
                }
        }
    }
    catch (Exception e) {  
            e.printstacktrace();  
    }
    return null;
}

This function gets called almost immediately after opening the web page and it works just fine the first time the page is opened. However, if you refresh the page, everything else works fine on restart, but you lose the sound. This is due to the fact that original applet isn't completely shut down by the time the new one is created, so the original SourceDataLine is still in use when the new applet tries to fetch one. It stills manages to find one, but it's not the one that actually connects to the speakers.

I suppose one solution would be to always have the function wait for a certain amount of time before trying to fetch a SourceDataLine, but that doesn't seem particularly elegant and I'd like the program to grab the right dataLine as quickly as possible.

So then, does anybody have any tips on how I could do this, or am I pretty much stuck?

--EDIT--

I added in some stack traces (per Andrew's suggestion) and here's what I got:

javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian not supported.
    at javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian not supported.
    at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:494)
    at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:107)
    at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:139)
    at view.Playback开发者_Python百科Thread.getSourceDataLine(PlaybackThread.java:120)
    at view.PlaybackThread.loadSoundData(PlaybackThread.java:99)
    at view.PlaybackThread.<init>(PlaybackThread.java:33)
    at model.GUIModule.<init>(GUIModule.java:77)
    at model.GUIMainApplet.createAndShowGUI(GUIMainApplet.java:24)
    at model.GUIMainApplet.access$0(GUIMainApplet.java:22)
    at model.GUIMainApplet$1.run(GUIMainApplet.java:17)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:642)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:603)
    at java.awt.EventQueue$1.run(EventQueue.java:601)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:612)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Line 6 of that dump is where we want to start looking, I believe.

0

精彩评论

暂无评论...
验证码 换一张
取 消