2016-08-13 8 views
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に以下のスクリプトを作成しましたそれは欠けている。 デバッグは私を助けませんでした。 問題を解決する方法とループに戻る方法はありますか?

ありがとうございます。

+0

[ 'Popen.communicate()'](https://docs.python.org/3.4/library/subprocess.html#subprocess.Popen.communicate )は、EOFまでstdoutとstderrからデータを読み込み、プロセスが終了するのを待ちます。つまり、mplayerが終了するまでブロックされます。 –

+0

ヒントありがとうございます。 – immi1988

答えて

0

私はMPlayerのスレーブと、それを解決:

from time import sleep 
from subprocess import Popen 

pathtoControlFile = "/home/immi/mplayer-control" 
cmd = ["mplayer", "-slave", "-input", "file="+pathtoControlFile, "-shuffle", "-playlist", "/media/immi/9A005723005705A3/Musik/playlist.m3u"] 

if __name__ == '__main__': 
    first = False 
    i = 0 

    if first == False:  # initial start 
     print("player starting") 
     p = Popen(cmd) 
     print("player started") 
     first = True 

    while 1: 
     sleep(1) 
     i += 1 
     print(str(i)+ " " +str(first)) 

     if i == 5 and first == True: # each later button 
      i = 0 
      print("sending keystroke to mplayer") 
      with open(pathtoControlFile, "wb+", buffering=0) as fileInput: 
       p = Popen(["echo", "pt_step next"], stdout=fileInput) 
関連する問題