【问题标题】:pygame is not responding after playing an audio filepygame 播放音频文件后没有响应
【发布时间】:2017-05-02 23:44:54
【问题描述】:

我是pygame的新手,不是一个非常有经验的python程序员,所以希望你能帮助。我正在尝试使用 pygame 和 tkinter 在 python 上制作自己的音频播放器。所以有我的代码:

from tkinter import *
from tkinter.filedialog import *
import pygame
import sys
from pygame import*
mixer.init()
pygame.mixer.pre_init(44100, -16, 2, 2048)
def play(event):
    mixer.music.load("Chillingmusic.wav")
    mixer.music.play()
    while mixer.music.get_busy():
        time.Clock().tick(10)

def pause(event):
    pygame.mixer.music.pause()
    song.pause()

root=Tk()

txt=Text(root)

m=Menu(root)
root.config(menu=m)

fm=Menu(m)
m.add_cascade(label="File",menu=fm)
fm.add_command(label="Open",command_=open)

but=Button(text=">",bg="lightgreen")
but.grid(row=0,column=0)
but.bind("<Button-1>",play)

but1=Button(text="p",bg="lightblue")
but1.grid(row=0,column=1)
but1.bind("<Button-1>",pause)

root.mainloop()
pygame.quit()

问题是当我使用播放功能开始播放音乐时,audiofile 正在播放,但在该界面停止响应之后,我无法使用暂停功能。 我在 Windows 10(64 位)上使用 python 3.6 和 pygame 1.9.3。

【问题讨论】:

  • play 函数中的 while 循环将使您的主线程在歌曲播放期间保持忙碌。换句话说,在 while 循环完成之前,您不能做任何事情。如果您删除 while 循环,我认为它应该可以按预期工作。
  • @TedKleinBergman 您的解决方案有效。您为什么不将其发布为答案?
  • @skrx 自己没有时间验证它,所以这涉及到一些猜测。这更像是一个“试试这个,它可能会起作用”的评论,而不是一个答案。我现在将发布一个简短的答案

标签: python audio tkinter pygame


【解决方案1】:

您的play 函数将在歌曲完成之前完成。这意味着您的程序必须等待它完成并且在此期间无法执行任何其他操作,从而使其他所有操作都没有响应。不要使用while 循环。

def play(event):
    mixer.music.load("Chillingmusic.wav")
    mixer.music.play()

mixer 正在使用除您的主线程之外的另一个线程,因此音乐将播放并且您自己的代码可以同时执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多