我有一个线程循环的声音剪辑:
(def f
(future
(let [sound-file (java.io.File. "/path/to/file.wav")
sound-in (javax.sound.sampled.AudioSystem/getAudioInputStream sound-file)
format (.getFormat sound-in)
info (javax.sound.sampled.DataLine$Info. javax.sound.sampled.Clip format)
clip (javax.sound.sampled.AudioSystem/getLine info)]
(.open clip sound-in)
(.loop clip javax.sound.sampled.Clip/LOOP_CONTINUOUSLY))))
问题是当我尝试杀死线程时:
(future-cancel f)
它不会停止播放永远播放的剪辑。我发现阻止它的唯一方法是(.stop clip)
显式调用。我的问题:这样做的最佳/惯用方式是什么?我对 Clojure 很陌生,future
所以到目前为止我只进行了实验,但也许 anagent
更适合这种情况?
更新:鉴于该.loop
功能是非阻塞的(如下所述),我通过摆脱最初的来简化我的设计future
:
(defn play-loop [wav-fn]
(let [sound-file (java.io.File. wav-fn)
sound-in (javax.sound.sampled.AudioSystem/getAudioInputStream sound-file)
format (.getFormat sound-in)
info (javax.sound.sampled.DataLine$Info. javax.sound.sampled.Clip format)
clip (javax.sound.sampled.AudioSystem/getLine info)]
(.open clip sound-in)
(.loop clip javax.sound.sampled.Clip/LOOP_CONTINUOUSLY)
clip))
连同一个控制atom
:
(def ^:dynamic *clip* (atom nil))
我用它开始循环:
(when (nil? @*clip*)
(reset! *clip* (play-loop "/path/to/file.wav")))
并停止它:
(when @*clip*
(future (.stop @*clip*) ; to avoid a slight delay caused by .stop
(reset! *clip* nil)))