2017-09-20 32 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过程是否完成。如果不是,请检查是否有按键。