python - 如何使用 pygame 混音器在两个设备中并行播放声音?
问题描述
我想在两个设备中使用pygame.mixer
. 我目前仅将其配置为一台设备,我希望将其配置为两台设备。
from flask import Flask
from markupsafe import escape
from pygame._sdl2 import get_num_audio_devices, get_audio_device_name
from pygame import mixer
app = Flask(__name__)
@app.route("/<name>")
def hello_world(name):
try:
mixer.init(devicename="Line (VB-Audio Virtual Cable)") # device 1, I have one more device with the name Speaker/Headphone (Realtek High Definition Audio)
mixer.music.load("C:\\Users\\saif\\Desktop\\soundboard\\" + escape(name)+ ".mp3")
mixer.music.play() #Play it
except:
print("some error")
return "<p>Hello, World!</p>"
if __name__ == '__main__':
app.debug = True
app.run(host="0.0.0.0")
设备列表:
(base) C:\Users\saif>python
Python 3.8.3 (default, Jul 2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pygame._sdl2 import get_num_audio_devices, get_audio_device_name #Get playback device names
pygame 2.0.0.dev8 (SDL 2.0.12, python 3.8.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> from pygame import mixer #Playing sound
>>> mixer.init() #Initialize the mixer, this will allow the next command to work
>>> [get_audio_device_name(x, 0).decode() for x in range(get_num_audio_devices(0))] #Returns playback devices
['Line (VB-Audio Virtual Cable)', 'Speaker/Headphone (Realtek High Definition Audio)', 'BenQ GW2480 (Intel(R) Display Audio)']
>>>
我要并行播放文件的两个设备是:
Line (VB-Audio Virtual Cable)
Speaker/Headphone (Realtek High Definition Audio)
解决方案
推荐阅读
- amazon-web-services - AccessDeniedException:用户:arn:aws:iam::xxxxxxx:root 无权执行:lambda:UpdateFunctionCode
- javascript - 记忆游戏中最后一张卡片翻转的问题
- apache-spark - 真空不删除旧的镶木地板文件
- android - Android Studio Network Profiler 西里尔文
- c# - 忽略按下按钮
- python - 分数未更新 Pong Pygame
- reactjs - 如何使用 React 中的 Object.entries 方法使我的函数更易于维护
- java - 缺少选项卡名称和选项卡名称在 android studio 上不会更改
- pandas - 熊猫系列申请
- mysql - 我的金融证券数据库架构是否正确?