Page 1 of 1

I want to know how to write audio play callback in python-vlc

Posted: 03 Jun 2021 15:55
by s705d
I wrote code to stream audio as simple as the following.

If no callback is registered (part **) this code works fine.

But there is no sound when the callback function is registered.

I would like to register a play callback to preprocess the streamed data.

How to register a callback function can be found by looking at the python-vlc documentation.

But I can't figure out how to write the callback function.

The second argument to the callback function, samples, is a pointer to the data to be played.

How should I write a callback function at the (*) part with this pointer?

Any reply would be appreciated.

And log file is here. https://gist.github.com/metamath1/7c4f6 ... a8cca9c653

Code: Select all

import vlc import re import requests import ctypes url = "http://serpent0.duckdns.org:8088/kbsfm.pls" res = requests.get(url) res.raise_for_status() # retrieve url p = re.compile("https://.+") m = p.search(res.text) url = m.group() # AudioPlayCb = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint, ctypes.c_int64) @vlc.CallbackDecorators.AudioPlayCb def play_callback(data, samples, count, pts): bytes_read = count * 4 * 2 buffer_array = ctypes.cast(samples, ctypes.POINTER(ctypes.c_char * bytes_read)) ################################################################################## # THIS CODE IS INVALID. TO DO IT RIGHT, WHERE SHOULD BUFFER_ARRAY BE COPIED TO? (*) buffer = bytearray(bytes_read) buffer[:] = buffer_array.contents ################################################################################## instance = vlc.Instance(["--prefetch-buffer-size=2000 --prefetch-read-size=5000 --network-caching=1000"]) #define VLC instance media = instance.media_new(url) player = media.player_new_from_media() player.audio_set_format("f32l", 48000, 2) # PLAYS WELL WITHOUT THIS LINE (**) player.audio_set_callbacks(play=play_callback, pause=None, resume=None, flush=None, drain=None, opaque=None) player.play() #Play the media c = input()