2017-09-20 25 views
1

omxplayerを使用して音楽プレーヤシステムをコーディングしていますが、すべてが完了しましたが問題があります。音楽が終了した時点を検出する必要があります。プロセスを停止するために 'q'.encode()を停止するために' p'.encode() 'などのプロセスにバイトを書き込む必要があります。 問題は、バイトをサブプロセスに書き込むことができないことですそれを待っている...誰かがプロセスの終了を検出し、同時にそれに書き込む方法を知っているなら、あなたは大歓迎です!ここでsubprocess.wait中に書き込むことはできません

は私のコードです:

class MusicPlayer(Thread): 
    def __init__(self, manager, playlist=[]): 
     self.playlist = [] 
     self.index = 0 
     self.process = None 
     self.paused = False 
     self.stopped = False 
     self.manager = manager 
     self.progress_bar = MusicProgressBar(self) 
     self.progress_bar.start() 
     Thread.__init__(self) 
    def run(self): 
     while True: 
      if len(self.playlist) is 0: 
       time.sleep(1) 
       continue 

      self.process = subprocess.Popen(['omxplayer' , '-o' , 'local', self.playlist[self.index].file_url]) 
      self.progress_bar.play(self.playlist[0].file_url) 
      self.paused = False 
      self.process.wait() 
      self.index += 1 
      if self.index >= len(self.playlist): 
       self.index = 0 
    def play(self, playlist): 
     self.playlist = playlist 
     self.index = 0 
    def next(self): 
     if self.process is None: return 
     self.process.stdin.write('q'.encode()) 
     self.process.stdin.flush() 

    def before(self): 
     for i in range(0,2): 
      self.index -= 1 
      if self.index < 0: 
       self.index = len(self.playlist-1) 

    def stop(self): 
     if self.process is None: return 
     self.process.stdin.write('q'.encode()) 
     self.process.stdin.flush() 
     self.stopped = True 

    def pause(self): 
     if self.process is None: return 
     if self.paused: return 
     self.process.stdin.write('p'.encode()) 
     self.process.stdin.flush() 
     self.paused = True 

    def resume(self): 
     if self.process is None: return 
     if not self.paused: return 
     self.process.stdin.write('p'.encode()) 
     self.process.stdin.flush() 
     self.paused = False 

は、あなたの答えのために本当にありがとうございました! 敬具、ジュリアン

+0

['Popen.communicate()'](https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate)を見てください。 – martineau

答えて

0

は、私はあなたがomxplayerプロセスが終了したかどうかポーリングを保つあるwhileループ

while self.process.poll() not None: 
    c = stdscr.getch() 
    if c != -1: 
    # check p, q 

にこのライン

 self.process.wait() 

を変更することができると思います。そうでない場合は、キーが押されているかどうかを確認します。

関連する問題