Skip to content

Instantly share code, notes, and snippets.

@rnons
Created March 4, 2013 14:08
Show Gist options
  • Save rnons/5082441 to your computer and use it in GitHub Desktop.
Save rnons/5082441 to your computer and use it in GitHub Desktop.
a simple wrapper to mplayer
import System.IO
import System.IO.Unsafe (unsafePerformIO)
import System.Process
import Control.Concurrent
mhin = unsafePerformIO newEmptyMVar
mhout = unsafePerformIO newEmptyMVar
ended = unsafePerformIO newEmptyMVar
main = do
forkIO mpgLoop
forkIO wait
forkIO $ play []
getChar >>= print
mpgLoop = do
let sh = "mplayer -msglevel global=6:statusline=6 -slave -idle -really-quiet -cache 2048 -cache-min 5 -novideo"
(Just hin, Just hout, Just herr, hdl) <-
createProcess (shell sh) { std_in = CreatePipe
, std_out = CreatePipe
, std_err = CreatePipe
}
putMVar mhin hin
putMVar mhout hout
waitForProcess hdl
hClose herr -- Keep `herr` alive until explicitly closed here.
wait = do
hout <- readMVar mhout
line <- hGetLine hout
case line of
"EOF code: 1 " -> do
putMVar ended ()
_ -> return ()
wait
play [] = do
let playlist = [ "/home/rnons/Music/01.mp3"
, "/home/rnons/Music/02.mp3"
, "/home/rnons/Music/03.mp3"
]
play playlist
play (x:xs) = do
hin <- readMVar mhin
let cmd = "loadfile " ++ x
hPutStrLn hin cmd
hFlush hin
putStrLn cmd
takeMVar ended
putStrLn "next"
play xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment