0

In my project, I need to get sound from a radio stream URL and play it. But it needs to be played in a specific output device called "VB-Cable Input (Virtual Audio Cable)".

I couldn't find a way to do it. This is my current code:

import vlc
import time

url = "https://shout25.crossradio.com.br:18156/1"

# Create VLC instance, media player and media
instance = vlc.Instance()
player = instance.media_player_new()
media = instance.media_new(url)
player.set_media(media)

# Get list of output devices
def get_device():
    mods = player.audio_output_device_enum()
    if mods:
        mod = mods
        while mod:
            mod = mod.contents
            # If VB-Cable is found, return it's module and device id
            if 'CABLE Input (VB-Audio Virtual Cable)' in str(mod.description):
                device = mod.device
                module = mod.description
                return device,module
            mod = mod.next

# Sets the module and device id to VB-Cable           
device,module = get_device()

# Sets VB-Cable as output device
player.audio_output_device_set(device,module)

# Trying the function @Random Davis stated, but results still the same
vlc.libvlc_audio_output_device_set(player, module, device)

# It should return VB-Cable as current audio output, but None is returned...
print(player.audio_output_device_get())

# Audio plays in default output device, which is incorrect
player.play()   
time.sleep(60)

I searched through StackOverflow and the VLC documentation, but couldn't get the result I expected, I believe I'm using the functions in the wrong way.

  • There's a function, `libvlc_audio_output_device_set` which does this. It's clearly documented [here](https://www.olivieraubert.net/vlc/python-ctypes/doc/vlc-module.html#libvlc_audio_output_device_set). Did you just not search the documentation? Or did you find that function but you don't know how to use it? Either way, why did you not mention anything you'd actually tried? Surely you tried something before just posting here? If you really did just post here without making any effort, then why do you expect people to put in effort to help? What's their motivation for helping? – Random Davis Sep 28 '22 at 16:15
  • @RandomDavis Thank you for your reply and your clarification, I am updating my question to give more details of my attempts. About `libvlc_audio_output_device_set` I'm not sure if I used the function correctly, as my edited question shows, VLC is still using the same device. – Pedro Toriality Sep 28 '22 at 17:54
  • Okay that's much better. So, in `get_device()`, are you sure it's finding anything? Did you step through the code in a debugger and ensure that it's behaving how you expect? Did you make sure it's returning something valid? Are `device` and `module` set to sensible values when you run `device,module = get_device()`? – Random Davis Sep 28 '22 at 18:00
  • @RandomDavis Yes, get_device() finds VB-Cable device and returns correctly the variables, when doing print(device, module) after get_device() it shows me the correct informations about VB device. About the last question, I didn't quite understand what would be setting to sensible values. – Pedro Toriality Sep 28 '22 at 18:12
  • Oh I just was wondering if the function correctly returns the variables, and you answered that. So, maybe `audio_output_device_set` is the issue, and it is not getting used correctly. Hard to say. Probably that's what you should focus on. – Random Davis Sep 28 '22 at 18:20

1 Answers1

1

After some research, I found this implementation of audio_output_device_set() in GitHub

As @RandomDavis said, the problem was indeed in this function. All I had to do was switching the first argument to None and leave the rest as it is.

This is the correct code:

# Sets VB-Cable as output device
player.audio_output_device_set(None, device)

Still not quite sure why inserting any values other than None makes the function invalid. But the problem is now solved.