python 音频播放选择

pygame

pygame.mixer

用于加载和播放声音的pygame模块

初始化混音器模块
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096) -> None

预设混音器初始化参数
pygame.mixer.pre_init(frequency=22050, size=-16, channels=2, buffersize=4096) -> None

设置播放频道的总数
pygame.mixer.set_num_channels(count: int = 8) -> None

获取播放频道的总数
pygame.mixer.get_num_channels() -> int

预留频道自动使用
pygame.mixer.set_reserved(count: int) -> None

找到一个未使用的频道
pygame.mixer.find_channel(force: bool = False) -> Channel

测试mixer 是否正忙
pygame.mixer.get_busy() -> bool

测试混音器是否初始化
pygame.mixer.get_init(frequency, format, channels)

停止播放所有声道
pygame.mixer.stop()

暂停播放所有声道
pygame.mixer.pause()

恢复播放声道
pygame.mixer.unpause()

停止前淡出所有声音的音量
pygame.mixer.fadeout(time_ms: int)

卸载混音器模块
pygame.mixer.quit()

从文件或缓冲区对象创建新的Sound对象
pygame.mixer.Sound

Sound(filename) -> Sound
Sound(file=filename) -> Sound
Sound(buffer) -> Sound
Sound(buffer=buffer) -> Sound
Sound(object) -> Sound
Sound(file=object) -> Sound
Sound(array=object) -> Sound

pygame.mixer.Sound.play - 开始播放声音
pygame.mixer.Sound.stop - 停止声音播放
pygame.mixer.Sound.fadeout - 淡出后停止声音播放
pygame.mixer.Sound.set_volume - 设置此声音的播放音量
pygame.mixer.Sound.get_volume - 获取播放音量
pygame.mixer.Sound.get_num_channels - 计算此声音播放的次数
pygame.mixer.Sound.get_length - 得到声音的长度
pygame.mixer.Sound.get_raw - 返回Sound样本的bytestring副本

流化音频控制模块
pygame.mixer.music

pygame.mixer.music.load(filename) - 加载音乐文件
pygame.mixer.music.unload() - 卸载当前加载的音乐, 释放资源
pygame.mixer.music.fadeout(time_ms: int) - 淡出音乐
pygame.mixer.music.get_volume() - 获取音量
pygame.mixer.music.set_volume(count: int) - 设置音量
pygame.mixer.music.set_endevent() - 播放结束发送事件
pygame.mixer.music.play() - 开始播放
pygame.mixer.music.pause() - 暂停播放
pygame.mixer.music.unpause() - 从暂停中恢复播放
pygame.mixer.music.stop() - 停止播放
pygame.mixer.music.queue(filename) - 加入播放队列
pygame.mixer.music.rewind() - 重启音乐
pygame.mixer.music.get_pos() - 获取播放时间
pygame.mixer.music.set_pos(pos) - 设置播放起点位置
pygame.mixer.music.get_budy() - 检查音乐流是否播放中

创建一个Channel对象来控制播放
pygame.mixer.Channel

Channel(id) -> Channel

pygame.mixer.Channel.play - 在特定频道播放声音
pygame.mixer.Channel.stop - 停止在频道上播放
pygame.mixer.Channel.pause - 暂时停止播放频道
pygame.mixer.Channel.unpause - 恢复暂停播放频道
pygame.mixer.Channel.fadeout - 淡出通道后停止播放
pygame.mixer.Channel.set_volume - 设置播放频道的音量
pygame.mixer.Channel.get_volume - 获得播放频道的音量
pygame.mixer.Channel.get_busy - 检查通道是否处于活动状态
pygame.mixer.Channel.get_sound - 得到当前播放的声音
pygame.mixer.Channel.queue - 排队Sound对象以跟随当前
pygame.mixer.Channel.get_queue - 返回排队的任何声音
pygame.mixer.Channel.set_endevent - 播放停止时让频道发送事件
pygame.mixer.Channel.get_endevent - 获取播放停止时频道发送的事件

playsound

支持wav/mp3,功能简单,包体积小。播放时会阻塞线程,无法中断播放。

from playsound import playsound
playsound('/path/to/a/sound/file/you/want/to/play.mp3')

simpleaudio

支持wav, 支持中断播放且不阻塞线程。

import simpleaudio as sa
wave_obj = sa.WaveObject.from_wave_file("path/to/file.wav")
play_obj = wave_obj.play()
play_obj.wait_done()

pydub

支持多种音频格式, 功能强大, 但依赖ffmpeg来处理音频文件。

from pydub.playback import play
from pydub.audio_segment import AudioSegment
#加载mp3文件
sound_mp3 = AudioSegment.from_mp3('demo.mp3')
#加载wav文件
sound_wav = AudioSegment.from_wav('demo.wav')
# 播放音频
play(sound_mp3)
play(sound_wav)

xxxx