按推荐顺序排列
1、使用playsound库
from playsound import playsound playsound('xx.mp3')
2、使用pygame库
from pygame import mixer import time mixer.init() mixer.music.load('xx.mp3') mixer.music.play() time.sleep(5) mixer.music.stop()
3、使用mp3play库(仅支持python2,对python3不支持)
import mp3playimport time clip = mp3play.load('xx.mp3') clip.play() time.sleep(5) clip.stop()
4、打开系统自带播放器,然后播放MP3,弹窗麻烦复杂
import os os.system('xx.mp3')
内容扩展:
使用pyaudio模块播放音乐文件的实例
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) while len(data) > 0: stream.write(data) data = wf.readframes(CHUNK) stream.stop_stream() stream.close() p.terminate()