【问题标题】:How to pause/interupt with keyboard如何使用键盘暂停/中断
【发布时间】:2018-03-12 04:31:23
【问题描述】:

我正在使用以下脚本来播放当前路径中的所有 WAV 文件。我将对其进行修改以打印一些文本文件的输出。这部分很简单。

需要知道在播放 WAV 文件的循环中如何/在哪里,在哪里添加一些代码来暂停/中断使用键盘执行代码。

#!/usr/bin/python3

import vlc
import time
import glob

wav_files = glob.glob("*.wav")
instance=vlc.Instance(["--no-sub-autodetect-file"])

# You should not recreate a player for each file, just reuse the same 
# player
player=instance.media_player_new()

for wav in wav_files:
    player.set_mrl(wav)
    player.play()
    playing = set([1,2,3,4])
    time.sleep(5) #Give time to get going
    duration = player.get_length() / 1000
    mm, ss = divmod(duration, 60)
    print("Playing", wav, "Length:", "%02d:%02d" % (mm,ss))
    while True:
        state = player.get_state()
        if state not in playing:
            break
        continue

【问题讨论】:

    标签: python-3.x audio wav vlc


    【解决方案1】:

    vlc.py中窃取getch
    我添加了 windows 选项,因为您没有指定操作系统。

    #!/usr/bin/python3
    
    import vlc
    import time
    import glob
    import sys
    import termios, tty
    
    try:
        from msvcrt import getch  # try to import Windows version
    except ImportError:
        def getch():  # getchar(), getc(stdin)  #PYCHOK flake
            fd = sys.stdin.fileno()
            old = termios.tcgetattr(fd)
            try:
                tty.setraw(fd)
                ch = sys.stdin.read(1)
            finally:
                termios.tcsetattr(fd, termios.TCSADRAIN, old)
            return ch
    
    wav_files = glob.glob("*.wav")
    print("Play List")
    for f in wav_files:
        print(f)
    instance=vlc.Instance(["--no-sub-autodetect-file"])
    
    # You should not recreate a player for each file, just reuse the same
    # player
    player=instance.media_player_new()
    
    for wav in wav_files:
        player.set_mrl(wav)
        player.play()
        playing = set([1,2,3,4])
        time.sleep(1) #Give time to get going
        duration = player.get_length() / 1000
        mm, ss = divmod(duration, 60)
        print("Playing", wav, "Length:", "%02d:%02d" % (mm,ss))
        while True:
            state = player.get_state()
            if state not in playing:
                break
            k = getch()
            if k in ["N","n"]:#Next
                player.stop()
                break
            elif k in ["Q","q"]:#Quit
                player.stop()
                sys.exit()
                break
            elif k == " ":#Toggle Pause
                player.pause()
            else:
                print("[Q - Quit, N - Next, Space - Pause]")
            continue
    

    【讨论】:

    • 谢谢,工作得很好。我使用 Linux/Kubuntu 并没有更改您提供的代码。只是复制它并测试它。用空格键暂停/继续,.. 太棒了,正是我想要的,谢谢。
    • stackoverflow.com/users/4637585/rolf-of-saxony - 我确实使用“向上箭头”表示这个答案很有用,但我没有足够的分数来显示它。只是说,..知道你很感激。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    相关资源
    最近更新 更多