python源码:指定麦克风/音响播放歌曲
前言
我使用pygame实现了指定麦克风/音响播放歌曲的功能,主要目的是解决直播过程的多源声道控制问题。
代码
# 查看自己的音频设备
# 请记住目标音频设备的具体名称
import pygame as mixer
import pygame._sdl2 as sdl2
mixer.init() # Initialize the mixer, this will allow the next command to work
print(sdl2.audio.get_audio_device_names(False)) # set to False to return playback(output) devices, set to True to return record(input) devices.
mixer.quit() # Quit the mixer as it's initialized on your main playback device
from pygame import mixer
import time
# 使用设备:Voicemeeter In 1 (VB-Audio Voicemeeter VAIO)
# 来负责播放
mixer.init(devicename = 'Voicemeeter In 1 (VB-Audio Voicemeeter VAIO)')
# 播放本地歌曲
mixer.music.load("Reol.MP3") # Load the mp3
# 播放接口流式歌曲
# from io import BytesIO
# response = requests.get('http://xxxxxx/wav2wav1')
# audio_data = BytesIO(response.content)
# mixer.music.load(audio_data)
mixer.music.play() # Play it
# 监测音频是否播放完毕
while mixer.music.get_busy(): # wait for music to finish playing
time.sleep(1)