0
私はRPI上のボタンを接続してmplayerを制御しようとします。最初のボタンを押すとプレイヤーが起動し、後でボタンを押すたびにプレイリスト。Python 3.4:popen.communicate()プログラムが失われた後
from time import sleep
from subprocess import Popen, PIPE, DEVNULL
cmd = ["mplayer", "-shuffle", "-playlist", "/path/to/playlist.m3u"]
if __name__ == '__main__':
first = False
p = None
i = 0
if first == False: # should simulate first button
print("player starting")
p = Popen(cmd, stdin=PIPE, stdout=DEVNULL)
print("player started")
first = True
while 1:
sleep(1)
i += 1
print(str(i)+ " " +str(first))
if i == 5 and first == True: # should simulate each later button
i = 0
print("sending keystroke to mplayer")
p.communicate(b"\n")[0] # mplayer plays next song, but the program is lost
print("sended keystroke to mplayer - never printed")
、出力は次のとおりです:
player starting
player started
1 True
2 True
3 True
4 True
5 True
sending keystroke to mplayer
そして今、私はループの再起動を期待していますが、 最小限の例として、私はLinuxのミント18とPython3.4.3に以下のスクリプトを作成しましたそれは欠けている。 デバッグは私を助けませんでした。 問題を解決する方法とループに戻る方法はありますか?
ありがとうございます。
[ 'Popen.communicate()'](https://docs.python.org/3.4/library/subprocess.html#subprocess.Popen.communicate )は、EOFまでstdoutとstderrからデータを読み込み、プロセスが終了するのを待ちます。つまり、mplayerが終了するまでブロックされます。 –
ヒントありがとうございます。 – immi1988