我编写了流式传输音频的代码,如下所示。
如果没有注册回调(**部分),则此代码可以正常工作。
但我想注册一个播放回调来预处理流数据。
如何注册回调函数可以查看python-vlc文档。
但是我不知道如何编写回调函数。
回调函数的第二个参数samples是指向要播放的数据的指针。
如何在这个指针的(*)部分写回调函数?
任何回复将不胜感激。
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(opaque, samples, count, pts):
"""
@param data: data pointer as passed to L{libvlc_audio_set_callbacks}() [IN].
@param samples: pointer to a table of audio samples to play back [IN].
@param count: number of audio samples to play back.
@param pts: expected play time stamp (see libvlc_delay()).
"""
# HOW DO I RETURN SOMETHING FROM HERE? (*)
pass
return buffer
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()
我编写了播放回调函数,如下所示。 我认为传入的
buffer_array
应该复制到另一个缓冲区来播放样本。
@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
有更新吗?我也遇到同样的问题