Skip to content

Instantly share code, notes, and snippets.

@chadselph
Created May 13, 2014 20:21
Show Gist options
  • Save chadselph/47ff982f1a9f9b0f8f30 to your computer and use it in GitHub Desktop.
Save chadselph/47ff982f1a9f9b0f8f30 to your computer and use it in GitHub Desktop.
Audio Futures
import java.net.URL
import javax.sound.sampled.{AudioSystem, LineListener, LineEvent}
import scala.concurrent.{Promise, Future, Await}
import scala.concurrent.duration.Duration
object PlayAudio extends App {
val f = playUrl(
new URL("http://www.wavsource.com/snds_2014-05-05_4169601282614752/movies/airplane/wrong_week4.wav")
)
Await.result(f, Duration.Inf)
def playUrl(url: URL) = {
val p: Promise[Unit] = Promise()
val clip = AudioSystem.getClip()
clip.open(AudioSystem.getAudioInputStream(url))
clip.addLineListener(new LineListener {
def update(l: LineEvent) = {
l.getType match {
case LineEvent.Type.STOP =>
clip.close
p.success(())
}
}
})
clip.start
p.future
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment