In order of recommendation
1、 Use playsound library
fromplaysoundimportplaysound
playsound('xx.mp3')
2、 Use pygame library
frompygameimportmixer
importtime
mixer.init()
mixer.music.load('xx.mp3')
mixer.music.play()
time.sleep(5)
mixer.music.stop()
3、 Use mp3play library (only supports python2, not python3)
importmp3playimporttime
clip=mp3play.load('xx.mp3')
clip.play()
time.sleep(5)
clip.stop()
4、 Open the system's built-in player, and then play MP3, the pop-up window is troublesome and complicated
importos
os.system('xx.mp3')
Content expansion:
Examples of playing music files using pyaudio module
import pyaudio
import wave
import sys
chunk =1024
wf = wave.open('gyh.wav','rb')
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(chunk)whilelen(data)0:
stream.write(data)
data = wf.readframes(CHUNK)
stream.stop_stream()
stream.close()
p.terminate()
Recommended Posts