针对那些像我一样在实现这个过程中遇到困难的人,以下是关于@PyPylia回答的最新更新。
Current Package Version: pygame 2.1.2 (SDL 2.0.18, Python 3.10.6)
Tested Systems: Windows 10 (21H2 - 19044.1288), (Should be the same process on Mac but this is untested as of now...)
首先,您需要下载并安装适用于您平台的
VB-Cable虚拟麦克风驱动程序。这将为我们提供一个虚拟麦克风,使我们能够在使用视频通话软件(如Google Meet、Microsoft Teams、Zoom)时将我们机器上播放的音频作为麦克风输入传递。之后,所有操作都通过
pygame模块的音频包处理。
要获取音频设备列表:
from pygame import mixer, _sdl2 as devicer
mixer.init()
print("Inputs:", devicer.audio.get_audio_device_names(True))
print("Outputs:", devicer.audio.get_audio_device_names(False))
mixer.quit()
例如,我的设备返回:
Inputs: ['Microphone (High Definition Audio Device)', 'CABLE Output (VB-Audio Virtual Cable)']
Outputs: ['Speakers (High Definition Audio Device)', 'CABLE Input (VB-Audio Virtual Cable)']
然后,要播放音频:
import time
from pygame import mixer
mixer.init(devicename = 'CABLE Input (VB-Audio Virtual Cable)') # Initialize it with the correct device
mixer.music.load("Toby Fox - Megalovania.mp3") # Load the mp3
mixer.music.play() # Play it
while mixer.music.get_busy(): # wait for music to finish playing
time.sleep(1)
如果您希望连续播放多个音轨,请在上述while循环中添加以下代码段:
...
else:
mixer.music.unload() # Unload the mp3 to free up system resources
mixer.music.load("Sleeping at Last - Saturn.wav") # Load the wav
...
然后,在相关软件内的另一端,只需将麦克风输入从默认更改为
CABLE Output (VB-Audio Virtual Cable)
,即可让另一端听到源的音频。如果您使用的是较新版本的软件包,而某些列出的方法似乎因为
AttributeError: module 'pygame' has no attribute {method_name}
而无法使用,请使用
pyup 并搜索相关方法,以查看该方法的调用方式是否有任何更改。这是为什么
@PyPylia 的代码片段不再起作用,除非您使用旧版的
pygame。