[Haskell-cafe] using createProcess and waitForProcess, but mpg123 plays for only 6 seconds

rnons remotenonsense at gmail.com
Thu Nov 15 17:09:30 CET 2012

Herbert Valerio Riedel <hvr at gnu.org> wrote:

> Try to keep hin/hout "alive" a bit longer, and mpg123 should survive
> longer. Also you should most probably consume the output comming from
> 'mpg123 -R' via 'hout', as otherwise it the buffer might build up and
> mpg123 might block.
>  [1]:  
> http://hackage.haskell.org/packages/archive/base/

Shame of me, I've opened that link several times without noticing that
You're quite right. After understanding the details, I think there can be
many solutions.

But at present, I'm satisfied with this quick fix:

import System.Exit
import System.IO
import System.Process
import Control.Concurrent

mpgLoop = do
       let sh = "mpg123 -R"
       (Just hin, Just hout, _, hdl) <- createProcess (shell sh){ std_in =
CreatePipe, std_out = CreatePipe }
       hPutStrLn hin "SILENCE"  -- mildly prevent buffer blocking
       hPutStrLn hin "LOAD /home/rnons/Music/test.mp3"
       hFlush hin
       waitForProcess hdl >>= exitWith
       hPutStrLn hin "STOP"  -- keep hin "alive" a bit longer
       hGetContents hout
       hFlush hin
       return ()

main = do
       forkIO mpgLoop

Thank you very much!


More information about the Haskell-Cafe mailing list