2017-09-20 71 views
1

我正在編寫一個使用omplayer的音樂播放器系統,一切都完成了,但我有一個問題,我需要檢測音樂完成時通過subprocess.wait()直到進程和我必須寫入字節的過程,如'p'.encode()暫停,'q'.encode()停止等... 問題是,我不能寫字節到一個子進程等待它......如果有人知道如何檢測一個進程的結束並同時寫入,那麼歡迎您!無法寫入while 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

我想你可以把這個信號

 self.process.wait() 

更改爲一個while循環

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

即保持輪詢omxplayer過程是否完成。如果不是,請檢查是否有按鍵。