简体   繁体   English

libVLC 的 Python 绑定 - 无法更改音频输出设备

[英]Python bindings for libVLC - cannot change audio output device

VLC 2.2.3, python-vlc 1.1.2. VLC 2.2.3,python-vlc 1.1.2。

I have a virtual audio output and am trying to get libVLC to output on it.我有一个虚拟音频输出,并试图让 libVLC 在其上输出。 So far, I can see the virtual output appearing in libVLC, but selecting it makes audio play on the default output (ie the speakers).到目前为止,我可以看到出现在 libVLC 中的虚拟输出,但选择它会使音频在默认输出(即扬声器)上播放。

This is the relevant part of what I have:这是我所拥有的相关部分:

self.Instance = vlc.Instance()
self.player = self.Instance.media_player_new()

devices = []
mods = self.player.audio_output_device_enum()

if mods:
    mod = mods
    while mod:
        mod = mod.contents
        devices.append(mod.device)
        mod = mod.next

vlc.libvlc_audio_output_device_list_release(mods)

# this is the part I change on each run of the code.
self.player.audio_output_device_set(None, devices[0]) 

I've run the code multiple times, changing the device ID as per the code comment.我已经多次运行代码,根据代码注释更改设备 ID。 However, the output device doesn't actually change.但是,输出设备实际上并没有改变。 This is a headache for two reasons:这是一个令人头疼的问题,原因有二:

1) audio_output_device_set() doesn't return anything. 1) audio_output_device_set() 不返回任何内容。 I can't tell if I'm actually accomplishing anything when I run this function.当我运行这个函数时,我不知道我是否真的完成了任何事情。 2) I can't even run audio_output_device_get() to check if the set function is doing anything as this is only for libvlc 3. I would prefer for my program to work with 2.2.3. 2) 我什至无法运行 audio_output_device_get() 来检查 set 函数是否正在执行任何操作,因为这仅适用于 libvlc 3。我希望我的程序可以与 2.2.3 一起使用。

So, what I did next was install VLC 3.0 and run the above code with it.所以,我接下来要做的是安装 VLC 3.0 并用它运行上面的代码。 Now, audio_output_device_get() works and I can see that the set function is actually changing the output device to the virtual output.现在,audio_output_device_get() 起作用了,我可以看到 set 函数实际上是将输出设备更改为虚拟输出。 But sound STILL plays on the speakers.但声音仍然在扬声器上播放。

What's going on?这是怎么回事? How do I fix this?我该如何解决?

I asked at the VLC forums and got a singularly unhelpful reply telling me to 'check logs and documentation'.我在 VLC 论坛上询问并得到一个非常无益的回复,告诉我“检查日志和文档”。 That's it.而已。 I've been superglued to the rather lacking documentation to get this far.为了做到这一点,我一直非常依赖相当缺乏的文档。 Even though I doubt it can help, I've decided to try logging.即使我怀疑它是否有帮助,我还是决定尝试记录。 I thought it would be as simple as calling libvlc_log_set_file but it needs a libVLC file pointer, and I don't know how to create one with a name and mode as in Python.我认为这就像调用libvlc_log_set_file一样简单,但它需要一个 libVLC 文件指针,而且我不知道如何像在 Python 中那样创建一个具有名称和模式的文件指针。

tl;dr: tl;博士:

1) How do I successfully change the audio output device? 1) 如何成功更改音频输出设备? 2) How do I set up maximum verbosity logging? 2)如何设置最大详细日志记录?

1) For some reason, I had to pause and unpause before VLC would register my change. 1) 出于某种原因,我不得不在 VLC 注册我的更改之前暂停和取消暂停。

This code fixes things:这段代码修复了一些事情:

[... rest of GUI class ...]
self.p.play()
self.root.after(350, self.DeviceSet)

def DeviceSet(self):
    self.p.audio_output_device_set(None, self.audiodevice)
    self.p.pause()
    self.root.after(10)
    self.p.pause()

2) Initialise VLC as follows: 2)初始化VLC如下:

self.instance = vlc.Instance('--verbose 9')

Here is a full example of how to switch to different audio device.以下是如何切换到不同音频设备的完整示例。

Remember: don't call player.stop() after player.audio_output_device_set() , otherwise the set operation won't work!!切记:不要在player.audio_output_device_set() player.stop() ) ,否则设置操作无效!!

import time
from typing import List

import vlc


def vlc_set_device_test(filename: str):
    # creating a vlc instance
    vlc_instance: vlc.Instance = vlc.Instance()
    player: vlc.MediaPlayer = vlc_instance.media_player_new()
    media: vlc.Media = vlc_instance.media_new(filename)
    player.set_media(media)

    # list devices
    device_ids: List[bytes] = []
    mods = player.audio_output_device_enum()
    if mods:
        index = 0
        mod = mods
        while mod:
            mod = mod.contents
            desc = mod.description.decode('utf-8', 'ignore')
            print(f'index = {index}, desc = {desc}')
            device_ids.append(mod.device)

            mod = mod.next
            index += 1

    # free devices
    vlc.libvlc_audio_output_device_list_release(mods)

    # hard code device
    pc_speaker = device_ids[1]
    headset = device_ids[3]

    # play music to default device
    player.play()
    time.sleep(3)

    # set output device
    player.audio_output_device_set(None, headset)
    # don't call player.stop()!!
    player.pause()

    # now music is playing from headset
    player.play()
    time.sleep(10)

    player.stop()


if __name__ == '__main__':
    vlc_set_device_test(r'D:\cheer up.mp3')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM