我正在编写一个在Linux,windows和OS X上运行的Haskell命令行应用程序.我现在必须从中播放音频文件(.wav,.ogg和.mp3).我将如何实现一个功能
playAudiofile :: filePath -> IO ()
甚至更好
playAudio :: ByteString -> IO ()
只是适用于所有系统?
(我很高兴调用常见的命令行工具,也不介意将它们捆绑为windows发行版.)
最佳答案这是我提出的代码,使用SDL-1.2:module PlaySound (withSound,playSound) whereimport Control.Monadimport Sy@[email protected] Sy@[email protected] Data.Foldableimport Control.Exceptionimport qualifIEd Data.ByteString.Lazy as Bimport Foreign.ForeignPtrimport Graphics.UI.SDL as SDLimport Graphics.UI.SDL.mixer as MixwithSound :: IO a -> IO awithSound = bracket_ init cleanup where init = do SDL.init [SDL.InitAudio] getError >>= traverse_ putStrLn ok <- Mix.tryOpenAudio Mix.defaultFrequency Mix.AudioS16LSB 2 4096 unless ok $ putStrLn "Failed to open SDL audio device" cleanup = do Mix.closeAudio SDL.quitplaySound :: B.ByteString -> IO ()playSound content = do dir <- getTemporaryDirectory (tmp,h) <- openTempfile dir "sdl-input" B.hPutStr h content hClose h mus <- Mix.loadMUS tmp Mix.playMusic mus 1 wait -- This would double-free the Music,as it is also freed via a -- finalizer --Mix.freeMusic mus finalizeforeignPtr mus removefile tmpwait :: IO ()wait = do SDL.delay 50 stillPlaying <- Mix.playingMusic when stillPlaying wait
该程序最终工作正常,但是
>在windows下编译SDL绑定很棘手.我跟着this nice explanation on how to do it
> SDL-1.2的SDL绑定似乎没有维护和do not even compile with GHC-7.8 or newer.我最初没有注意到,因为我的发行版(Debian)修补了这些问题,但这意味着我的用户不能轻易地再次安装依赖项.
> SDL-2有绑定,但SDL_mixer没有绑定,我在这里需要(我相信).
所以我很乐意阅读更好的答案. 总结
以上是内存溢出为你收集整理的linux – 如何从跨平台的Haskell代码播放音频文件全部内容,希望文章能够帮你解决linux – 如何从跨平台的Haskell代码播放音频文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)