audio - Java How to wait for a clip to end -


i trying play few small sound effects, , i'm doing via method loads clip, plays it, , should close clip allow new 1 loaded later on without causing lineunavailable issues. here's method use:

public void playclip(string filename){      clip clip = null;      try{          audioinputstream inputstream = audiosystem.getaudioinputstream(getclass().getresource(filename));         audioformat format = inputstream.getformat();         dataline.info info = new dataline.info(clip.class, format);         clip = (clip)audiosystem.getline(info);         clip.open(inputstream);         clip.start();      }catch(exception e){          e.printstacktrace();      }  } 

right now, following clip.start(), clip remains somewhere, using line, until following error:

javax.sound.sampled.lineunavailableexception: line format pcm_signed 44100.0 hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported. @ com.sun.media.sound.directaudiodevice$directdl.implopen(directaudiodevice.java:513) @ com.sun.media.sound.directaudiodevice$directclip.implopen(directaudiodevice.java:1304) @ com.sun.media.sound.abstractdataline.open(abstractdataline.java:121) @ com.sun.media.sound.directaudiodevice$directclip.open(directaudiodevice.java:1085) @ com.sun.media.sound.directaudiodevice$directclip.open(directaudiodevice.java:1175) @ com.andrewlalisofficial.chatclientcore.playclip(chatclientcore.java:233) @ com.andrewlalisofficial.chatclientwindow$4.mouseclicked(chatclientwindow.java:207) @ java.awt.component.processmouseevent(component.java:6519) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3312) @ java.awt.component.processevent(component.java:6281) @ java.awt.container.processevent(container.java:2229) @ java.awt.component.dispatcheventimpl(component.java:4872) @ java.awt.container.dispatcheventimpl(container.java:2287) @ java.awt.component.dispatchevent(component.java:4698) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4832) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4501) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4422) @ java.awt.container.dispatcheventimpl(container.java:2273) @ java.awt.window.dispatcheventimpl(window.java:2719) @ java.awt.component.dispatchevent(component.java:4698) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:747) @ java.awt.eventqueue.access$300(eventqueue.java:103) @ java.awt.eventqueue$3.run(eventqueue.java:706) @ java.awt.eventqueue$3.run(eventqueue.java:704) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:87) @ java.awt.eventqueue$4.run(eventqueue.java:720) @ java.awt.eventqueue$4.run(eventqueue.java:718) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:717) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:242) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:161) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:150) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:146) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:138) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:91) 

javax.sound.sampled.lineunavailableexception: line format pcm_signed 44100.0 hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported. @ com.sun.media.sound.directaudiodevice$directdl.implopen(directaudiodevice.java:513) @ com.sun.media.sound.directaudiodevice$directclip.implopen(directaudiodevice.java:1304) @ com.sun.media.sound.abstractdataline.open(abstractdataline.java:121) @ com.sun.media.sound.directaudiodevice$directclip.open(directaudiodevice.java:1085) @ com.sun.media.sound.directaudiodevice$directclip.open(directaudiodevice.java:1175) @ com.andrewlalisofficial.chatclientcore.playclip(chatclientcore.java:233) @ com.andrewlalisofficial.chatclientwindow$4.mouseclicked(chatclientwindow.java:207) @ java.awt.component.processmouseevent(component.java:6519) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3312) @ java.awt.component.processevent(component.java:6281) @ java.awt.container.processevent(container.java:2229) @ java.awt.component.dispatcheventimpl(component.java:4872) @ java.awt.container.dispatcheventimpl(container.java:2287) @ java.awt.component.dispatchevent(component.java:4698) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4832) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4501) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4422) @ java.awt.container.dispatcheventimpl(container.java:2273) @ java.awt.window.dispatcheventimpl(window.java:2719) @ java.awt.component.dispatchevent(component.java:4698) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:747) @ java.awt.eventqueue.access$300(eventqueue.java:103) @ java.awt.eventqueue$3.run(eventqueue.java:706) @ java.awt.eventqueue$3.run(eventqueue.java:704) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:87) @ java.awt.eventqueue$4.run(eventqueue.java:720) @ java.awt.eventqueue$4.run(eventqueue.java:718) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:717) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:242) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:161) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:150) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:146) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:138) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:91)

how add clip.close() after clip finishes?

try adding block close connection

finally { if (clip!= null) { try { clip.close() } catch (exceptione e) { /* ignored */} }


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -