简体   繁体   English

如何使用pygame播放音乐(Python)

[英]How to play music using pygame (Python)

I am trying to play an .mp3 file using pygame. 我正在尝试使用pygame播放.mp3文件。 Here is my code: 这是我的代码:

import pygame

pygame.init()
pygame.mixer.init()

pygame.mixer.music.load('MSM.mp3')
pygame.mixer.music.play(0)

pygame.event.wait()

This however does not actually play the file. 但是,这实际上不会播放文件。 The audio file is located within the same folder as the .py file. 音频文件与.py文件位于同一文件夹中。

The pyGame docs suggest using the pygame.mixer.pre_init() method before the top-level pygame.init() on some platforms. pyGame文档建议在某些平台上在顶层pygame.init()之前使用pygame.mixer.pre_init()方法。

"Some platforms require the pygame.mixer module for loading and playing sounds module to be initialized after the display modules have initialized. The top level pygame.init() takes care of this automatically, but cannot pass any arguments to the mixer init. To solve this, mixer has a function pygame.mixer.pre_init() to set the proper defaults before the toplevel init is used." “某些平台要求pygame.mixer模块用于在显示模块初始化后初始化加载和播放声音模块。顶层pygame.init()自动处理此问题,但无法将任何参数传递给Mixer初始化。为了解决这个问题,mixer有一个pygame.mixer.pre_init()函数,可以在使用顶层init之前设置适当的默认值。” https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.init https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.init

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM