简体   繁体   中英

Python play mp3/wav file audio(Pygame mixer)

What is the best way to play mp3/wav file audio with Python?

I tried to use pygame, it play the sound, but don't close propertly the file. So when i try to remove the file i get errors.(Yes, i call pygame.mixer.quit() , but the file is still used in another process)

If you are open to using libraries other than pygame then you can use pydub which is clean and easy. Below is an working example.

For wav files

from pydub import AudioSegment
from pydub.playback import play

song = AudioSegment.from_wav("your_wav_file.wav")
play(song)

For MP3 files

song = AudioSegment.from_mp3("your_mp3_file.mp3")
play(song)

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