Note that there are some explanatory texts on larger screens.

plurals
  1. POpython 2.7 - subprocess control interaction with mpg123
    text
    copied!<p>I asked a question related to this several weeks ago on here: <a href="https://stackoverflow.com/questions/17159627/python-mpg123-and-subprocess-not-properly-using-stdin-write-or-communicate">Python, mpg123 and subprocess not properly using stdin.write or communicate</a></p> <p>Thanks to help from there I was able to do what I needed at the time. (Didn't call q, but terminated the subprocess to stop it).## Heading ## Now though I seem to be in another bit of a mess.</p> <pre><code> from subprocess import Popen, PIPE, STDOUT p = Popen(["mpg123", "-C", "test.mp3"], stdout=PIPE, stdin=PIPE, stderr=STDOUT) #wait a few seconds to enter this, "q" without a newline is how the controls for the player work to quit out if it were ran like "mpg123 -C test.mp3" on the command line p.communicate(input='q')[0] </code></pre> <p>much like before, I need this to be able to quit out of mpg123 like it would be with it's standard controls (like press 'q' to quit, or '-' to turn volume down, '+' to turn volume up, etc), now I use the code above, which should theoretically work, and it works with similar programs. Does anyone know of a way I can use the controls built into mpg123 (the one accessible by using "mpg123 -C whatever.mp3") using a subprocess? terminate isn't enough anymore as I will need the controls ^_^</p> <p>EDIT: Many thanks to abarnert for the amazing answer =) ok, so the new code is simply a slightly modified version of abarnert's answer, however mpg123 doesn't seem to be accepting the commands</p> <pre><code> import os import pty import sys import time pid, fd = os.forkpty() if pid: time.sleep(5) os.write(fd, 'b') #this should've restarted the file time.sleep(5) os.write(fd, 'q') #unfortunately doesn't quit here =( time.sleep(5) # quits after this is finished executing else: os.spawnl(os.P_WAIT, '/usr/bin/mpg123', '-C', 'TEST file.mp3') </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload