Created
May 13, 2014 20:21
-
-
Save chadselph/47ff982f1a9f9b0f8f30 to your computer and use it in GitHub Desktop.
Audio Futures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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