heidi
heidi

Reputation: 665

Python bindings for libVLC - cannot change audio output device

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. So far, I can see the virtual output appearing in libVLC, but selecting it makes audio play on the default output (i.e. the speakers).

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. However, the output device doesn't actually change. This is a headache for two reasons:

1) audio_output_device_set() doesn't return anything. 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.

So, what I did next was install VLC 3.0 and run the above code with it. Now, audio_output_device_get() works and I can see that the set function is actually changing the output device to the virtual output. 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'. 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.

tl;dr:

1) How do I successfully change the audio output device? 2) How do I set up maximum verbosity logging?

Upvotes: 1

Views: 3192

Answers (2)

someone
someone

Reputation: 132

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!!

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')

Upvotes: 0

heidi
heidi

Reputation: 665

1) For some reason, I had to pause and unpause before VLC would register my change.

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:

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

Upvotes: 3

Related Questions