使用pygame播放音乐但没有声音 [英] play music using pygame but no sound

查看:676
本文介绍了使用pygame播放音乐但没有声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import pygame
pygame.mixer.init()
pygame.mixer.music.load("only one.mp3")
pygame.mixer.music.play(0)
while pygame.mixer.music.get_busy():
    pygame.time.Clock().tick(10)

当我运行代码时,没有声音,程序以一秒钟结束.最初,我没有while循环,直到看到类似问题的答案中的建议为止.该程序确实在我朋友的Windows系统上进入了while循环,但在我的Mac上却没有,并且即使在我朋友的Windows系统上也没有任何声音.有人知道如何解决吗?

When I run the code, there's no sound and the program ends in like a second. Initially I didn't have the while loop until I saw the suggestions in the answers to similar questions. The program does enter the while loop on my friend's windows system, but not on my mac, and it doesn't have any sound either even on my friend's windows system. Does anybody know how to solve it?

推荐答案

在Ubuntu 10.04和Pygame 1.9.1上运行良好.

Works well on Ubuntu 10.04 and Pygame 1.9.1.

您可以尝试一些操作:

  • 初始化整个pygame pygame.init()
  • i_4_got的建议(创建显示)pygame.display.set_mode((200,100))
  • 在播放get_busy之间停顿一下(滴答声)
  • 循环pygame.event.get()
  • 中的轮询事件
  • initialize whole pygame pygame.init()
  • i_4_got's suggestion (create a display) pygame.display.set_mode((200,100))
  • put a pause (tick) between play an get_busy
  • poll events inside the loop pygame.event.get()

示例:

import pygame
pygame.init()
pygame.display.set_mode((200,100))
pygame.mixer.music.load("only one.mp3")
pygame.mixer.music.play(0)

clock = pygame.time.Clock()
clock.tick(10)
while pygame.mixer.music.get_busy():
    pygame.event.poll()
    clock.tick(10)

这篇关于使用pygame播放音乐但没有声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆