简体   繁体   中英

Can't write while subprocess.wait

I am codding a music player system using omxplayer, everything is done but i have a problem, I need to detect when the music is finished by subprocess.wait() until the end of the process and I have to write bytes to the process such as 'p'.encode() to pause, 'q'.encode() to stop etc... The problem is that I can't write bytes to a subprocess while waiting for it... If anyone knows how to detect the end of a process and writing to it at the same time, you're welcome !

Here is my code:

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

Thank you really much for your answers! Best regards, Julien

I think you can change this line

        self.process.wait()

to a while loop

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

That is keep polling whether the omxplayer process finished. If not, check if there is a key press.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM