首页 > 解决方案 > 如何使用 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)

标签: pythonaudiopygame

解决方案


推荐阅读