在python3中,如何正确地使程序在播放完歌曲之前通过循环?

问题描述 投票:0回答:1

我正在用python3写一个虚拟助手。我有一个while循环,播放两首随机选择的歌曲。我按ctrl c跳到下一首歌。这是很长的if语句的一部分。歌曲开始后,我可以通过按ctrl c转到下一首歌曲,但是最后一首歌曲结束后,程序将卡住。我需要再次按ctrl c才能继续。顺便说一句,当音乐正在播放时,麦克风停止工作,这很好,但这意味着我不能大喊“停止”或“下一步”,因此最好按一下键。

# next command
    elif 'music' in command:
        if playcounter == 1:
            talktome.talkToMe("Choosing random song . . . ")
        with open('/home/bard/Code/Juliet/mymusiclist.txt') as f:
            if playcounter == 1:
                print("Total songs to play " + str(totalsongstoplay) + ".")
            mymusic = f.read().splitlines()
            random_index = randrange(len(mymusic))
            song = mymusic[random_index]
            print("Playing song number " + str(playcounter) + ".")
            print("Song file:")
            print(song)
            playthis = 'mpg123 -q ' + song
            #subprocess.call(playthis, shell=True)
            p1=subprocess.Popen(playthis, shell=True)
            try:
                #while True:
                while p1.poll is not None:
                    pass
            except KeyboardInterrupt:
                # Ctrl-C was pressed (or user knew how to send SIGTERM to the python process)
                pass # not doing anything here, just needed to get out of the loop
            # nicely ask the subprocess to stop
            p1.terminate()
            # read final output
            sleep(1)
            # check if still alive
            if p1.poll() is not None:
                print('had to kill it')
                p1.kill()
            #end new code
            if playcounter < totalsongstoplay:
                playcounter = playcounter + 1
                assistant(command, playcounter, totalsongstoplay)
            else:
                playcounter=1
# next command

谢谢。整个项目位于BTW https://github.com/MikeyBeez/Juliet,该语音激活助手将其所有语音都本地翻译成文本-云中没有任何内容。我在Ubuntu 18.04上,并且将Conda用于我的虚拟环境。 Python是3.6.1。

subprocess python-3.6 speech-recognition ubuntu-18.04 keyboardinterrupt
1个回答
0
投票

已解决:

# next command
    elif 'music' in command:
        if playcounter == 1:
            talktome.talkToMe("Choosing random song . . . ")
        with open('/home/bard/Code/Juliet/mymusiclist.txt') as f:
            if playcounter == 1:
                print("Total songs to play " + str(totalsongstoplay) + ".")
            mymusic = f.read().splitlines()
            random_index = randrange(len(mymusic))
            song = mymusic[random_index]
            print("Playing song number " + str(playcounter) + ".")
            print("Song file:")
            print(song)
            playthis = 'mpg123 -q ' + song
            p1=subprocess.Popen(playthis, shell=True)
            try:
                #while True:
                while p1.poll() is None:
                    pass
                #p1.wait()
            except KeyboardInterrupt:
                # Ctrl-C was pressed (or user knew how to send SIGTERM to the python process)
                pass # not doing anything here, just needed to get out of the loop
            # nicely ask the subprocess to stop
            p1.terminate()
            # read final output
            sleep(1)
            # check if still alive
            if p1.poll() is not None:
                print('had to kill it')
                p1.kill()
            #end new code
            if playcounter < totalsongstoplay:
                playcounter = playcounter + 1
                assistant(command, playcounter, totalsongstoplay)
            #end if 
            playcounter = 1
© www.soinside.com 2019 - 2024. All rights reserved.