我们在做一些python项目时,经常需要用到语音提示功能,我们可以通过调用pygame库的music.play播放mp3文件功能来实现。
step.1 安装pygame库
(略)
step.2 引用pygame
import pygame # used pygame's music play function
step.3 初始化pygame播放功能
pygame.init()
pygame.mixer.init()
step.4 使用pygame的播放功能
pygame.mixer.music.load(voice_file)
pygame.mixer.music.play()
step. 5 等待mp3播放结束
while pygame.mixer.music.get_busy():
pass
# end while
大功告成!:)