Registrare in tempo reale in un file l'audio di una radio WEB con le funzioni esterne del API di VLC

Da Gambas-it.org - Wikipedia.

Con le riorse di VLC è possibile ascoltare e contemporaneamente registrare in un file audio in tempo reale i dati audio trasmessi da una radio WEB.

Mostriamo il seguente esempio partico in ambiente grafico:

Private bo As Boolean


Library "libvlc:5.6.1"

libvlc_instance_t * libvlc_new (int argc, const char *const *argv) ' Create And initialize a libvlc instance. Private Extern libvlc_new(argc As Integer, argv As String[]) As Pointer

libvlc_media_t *libvlc_media_new_location(libvlc_instance_t *p_instance, const char * psz_mrl) ' Create a media with a certain given media resource location, for instance a valid URL. Private Extern libvlc_media_new_location(p_instance As Pointer, path As String) As Pointer

void libvlc_media_add_option (libvlc_media_t *p_md, const char *psz_options) ' Add an option to the media. Private Extern libvlc_media_add_option(p_mi As Pointer, psz_options As String)

libvlc_media_player_t * libvlc_media_player_new(libvlc_instance_t *p_libvlc_instance) ' Create an empty Media Player object. Private Extern libvlc_media_player_new(p_libvlc_instance As Pointer) As Pointer

void libvlc_media_player_set_media(libvlc_media_player_t *p_mi, libvlc_media_t *p_md) ' Set the media that will be used by the media_player. Private Extern libvlc_media_player_set_media(p_md As Pointer, mp As Pointer)

int libvlc_media_player_play (libvlc_media_player_t * p_mi) ' Play the video file. Private Extern libvlc_media_player_play(p_mi As Pointer) As Integer

void libvlc_media_player_release (libvlc_media_player_t * p_mi) ' Release a media_player after use Decrement the reference count of a media player object. Private Extern libvlc_media_player_release(p_mi As Pointer)

void libvlc_media_release (libvlc_media_t *p_md) ' Decrement the reference count of a media descriptor object. Private Extern libvlc_media_release(p_md As Pointer)

libvlc_release (libvlc_instance_t * p_instance) ' Decrement the reference count of a libvlc instance, and destroy it if it reaches zero. Private Extern libvlc_release(p_instance As Pointer)


Public Sub ToggleButton1_Click()

Dim inst, mp, m As Pointer
Dim tm As Date
If ToggleButton1.Value Then

Inizializza la libreria VLC:

  inst = libvlc_new(0, Null)

Crea un nuovo oggetto multimedia. ' Nel secondo argomento della funzione va specificato l'indirizzo del flusso di dati audio trasmessi dalla radio web: m = libvlc_media_new_location(inst, "https://stream.rockantenne.de/70er-rock/stream/mp3?aw_0_1st.playerid=radio.de")

Imposta i parametri per il formato audio e per creare il file audio:

  libvlc_media_add_option(m, ":sout=#transcode{acodec=mp3,ab=128,samplerate=44100,channels=2}:duplicate{dst=display,dst=std{access=file,mux=raw,dst=/tmp/audio.mp3}}")

Crea il media player:

  mp = libvlc_media_player_new(inst)
  libvlc_media_player_set_media(mp, m)
  libvlc_media_release(m)

Avvia la registrazione audio da parte del media player:

  libvlc_media_player_play(mp)
  tm = Now
  While bo = False
    Me.Title = Str(Time(0, 0, 0, DateDiff(tm, Now, gb.Millisecond)))

Il Wait consente di agire su altri oggetti posti sul Form:

    Wait 0.01
  Wend

Chiude la libreria VLC ed il programma:

  libvlc_media_player_release(mp)
  libvlc_release(inst)
  Me.Close
Else

Causa l'arresto della registrazione, la chiusura della libreria VLC e del programma:

  bo = True
Endif

End