【问题标题】:python - can't get audio player workingpython - 无法让音频播放器工作
【发布时间】:2018-08-21 22:55:25
【问题描述】:

除了下一首歌曲完成后,其他所有歌曲都无法播放。

import os, random
from pygame import mixer
from pynput import keyboard
startup = 0
pause = 0
volume = 0.5
def Picker():
    global startup
    global volume
    startup += 1
    if startup > 1:
        ThisSong = random.choice(os.listdir("C:\\Users\\...\\Music"))
        NextSong = random.choice(os.listdir("C:\\Users\\...\\Music"))
        ThisSong = NextSong
        if ThisSong != NextSong:
            mixer.init()
            mixer.music.load("C:\\Users\\...\\Music" + ThisSong)
            mixer.music.play(0)
            mixer.music.set_volume(volume)
            while mixer.music.get_busy():
                def on_press(key):
                    global pause
                    global volume
                    if key == keyboard.KeyCode(char='-'):
                        volume -= 0.1
                        if volume < 0.1:
                            volume = 0.1
                        mixer.music.set_volume(volume)
                    if key == keyboard.KeyCode(char='='):
                        volume += 0.1
                        if volume > 1:
                            volume = 1
                        mixer.music.set_volume(volume)
                    if key == keyboard.KeyCode(char='['):
                        pause += 1
                        if pause == 1:
                            mixer.music.pause()
                            pause = 2
                        if pause == 3:
                            mixer.music.unpause()
                            pause = 0
                with keyboard.Listener(on_press=on_press) as listener: listener.join()
            else:
                Picker()
        else:
            pass
Picker()
Picker()

screenshot of code

我无法让它工作,我对 python 很陌生,所以我可能遗漏了一些东西 很明显

【问题讨论】:

标签: python python-3.x


【解决方案1】:

开始之前:感谢@JGreenwell 复制代码。

好的,首先,我会帮你清理代码。

错误的地方

  1. 拥有所有 ThisSong 和 NextSong 的东西:当您重新启动时不会保存它Picker()。要么只使用 ThisSong,要么将 ThisSong 和 NextSong 赋值与音量和暂停变量一起放置:

.

pause = 0 # this is a problem! Next point
volume = 0.5
ThisSong = random.choice(...)
NextSong = random.choice(...)

  1. 暂停变量应为布尔值 (True/False),暂停代码应如下所示:

.

pause = not pause
if pause:
    # pause
else:
    # unpause

另外,理想情况下它会被称为paused


  1. on_press 和 Listener 声明应该在 while 循环之外,否则它们只会在每次循环时都被声明。然后你应该 import time 并将 time.sleep(500) 或类似的东西放在 while 循环中,这样它就不会检查每一秒。

至于下一首不播放的歌曲,我真的不知道,但我建议在程序运行后在 shell 中输入Picker()(来自 CMD 的python -i script.py,IDLE 默认将您留在 shell 中)。我还建议真正遵循建议#3,因为将它们放在循环中可能会破坏循环并阻止它完成。最重要的是,我会要求您通过在代码的每个步骤中添加打印来调试代码:

print(1)
if something:
     print(2)
etc...

看看它在哪里阻塞

P.S.:随机点是因为列表中不能有代码,所以我不得不退出列表。

【讨论】:

  • 感谢您的回答!另外,感谢 JGreenwell 将我的代码写出来,昨晚它对我不起作用,这就是我发布屏幕截图的原因
【解决方案2】:

可行的解决方案,以防其他人将来遇到与我相同的问题:)

from pygame import mixer
from pynput import keyboard
import threading
import random
import os

paused = 0

def player():
    song = random.choice(os.listdir("C:\\users\\...\\desktop\\music"))
    mixer.init()
    mixer.music.load("C:\\users\\...\\desktop\\music\\" + song)
    mixer.music.play(0)
    while mixer.music.get_busy():
        pass
    else:
        player()

def main():
    t = threading.Thread(target = player, name = 'thread1', args = ())
    t.start()

main()

def on_press(key):
    global paused
    if key == keyboard.KeyCode(char='['):
                paused = not paused
                if paused:
                    mixer.music.pause()
                else:
                    mixer.music.unpause()
with keyboard.Listener(on_press=on_press) as listener: listener.join()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-28
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多