简体   繁体   中英

How to play music using pygame (Python)

I am trying to play an .mp3 file using pygame. 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.

The pyGame docs suggest using the pygame.mixer.pre_init() method before the top-level pygame.init() on some platforms.

"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." https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.init

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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