I'm trying to send command via pipe to mplayer when running it under slave mode like this:
import subprocess, time
# start mplayer
song = 'mysong.mp3'
cmd = ['mplayer', '-slave', '-quiet', song]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
# send a command every 3 seconds.
# Full command reference here: http://www.mplayerhq.hu/DOCS/tech/slave.txt
while True:
print('sleep 3 seconds ...')
time.sleep(3)
cmd = 'get_meta_artist'
print('send command: {}'.format(cmd))
p.stdin.write(cmd)
output = p.communicate()[0]
print(output)
But the output was nothing.
I took the example from this question.
Running the same mplayer command in the terminal works fine. What am I missing here?
UPDATE:
I changed my cmd from "get_meta_artist" to "get_meta_artist\n" so that a line-break is sent to the pipe as well but still I got nothing in the output.
UPDATE2:
I changed the cmd into "\npause\n" and the music was paused. So that means sending command via stdin worked. It means the output string of "\nget_meta_artist\n" command didn't get piped back as expected....