Python 中播放 MP3 文件的方法 Python 中播放 MP3 文件的方法
Python 中播放 MP3 文件的方法
方法一:使用pygame库
pygame是一个专门用于游戏开发的库,但也可以用来播放音频。
首先确保你安装了pygame库:
pip install pygame
以下是播放 MP3 的示例代码:
import pygame
pygame.init()
# 加载 MP3 文件
pygame.mixer.music.load('your_mp3_file.mp3')
# 播放音频
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
pygame.quit()
方法二:使用soundfile 库
···
import soundfile as sf
import sounddevice as sd
data, fs = sf.read(‘audio.mp3’)
sd.play(data, fs)
sd.wait()
···
Python 中播放 MP3 文件的方法 Python 中播放 MP3 文件的方法Python 中播放 MP3 文件的方法 Python 中播放 MP3 文件的方法Python 中播放 MP3 文件的方法 Python 中播放 MP3 文件的方法Python 中播放 MP3 文件的方法 Python 中播放 MP3 文件的方法Python 中播放 MP3 文件的方法 Python 中播放 MP3 文件的方法Python 中播放 MP3 文件的方法 Python 中播放 MP3 文件的方法Python 中播放 MP3 文件的方法 Python 中播放 MP3 文件的方法Python 中播放 MP3 文件的方法 Python 中播放 MP3 文件的方法Python 中播放 MP3 文件的方法 Python 中播放 MP3 文件的方法