Differenze tra le versioni di "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.
Riga 7: Riga 7:
 
  Library "libvlc:5.6.1"
 
  Library "libvlc:5.6.1"
 
   
 
   
libvlc_instance_t * libvlc_new (int argc, const char *const *argv)
+
<FONT Color=gray>' ''libvlc_instance_t * libvlc_new (int argc, const char *const *argv)''
' Create And initialize a libvlc instance.
+
' ''Create And initialize a libvlc instance.''</font>
Private Extern libvlc_new(argc As Integer, argv As String[]) As Pointer
+
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)
+
<FONT Color=gray>' ''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.
+
' ''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
+
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)
+
<FONT Color=gray>' ''void libvlc_media_add_option (libvlc_media_t *p_md, const char *psz_options)
 
' Add an option to the media.
 
' Add an option to the media.
 
Private Extern libvlc_media_add_option(p_mi As Pointer, psz_options As String)
 
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)
+
<FONT Color=gray>' ''libvlc_media_player_t * libvlc_media_player_new(libvlc_instance_t *p_libvlc_instance)
 
' Create an empty Media Player object.
 
' Create an empty Media Player object.
 
Private Extern libvlc_media_player_new(p_libvlc_instance As Pointer) As Pointer
 
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)
+
<FONT Color=gray>' ''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.
 
' 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)
 
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)
+
<FONT Color=gray>' ''int libvlc_media_player_play (libvlc_media_player_t * p_mi)
 
' Play the video file.
 
' Play the video file.
 
Private Extern libvlc_media_player_play(p_mi As Pointer) As Integer
 
Private Extern libvlc_media_player_play(p_mi As Pointer) As Integer
  
void libvlc_media_player_release (libvlc_media_player_t * p_mi)
+
<FONT Color=gray>' ''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.
 
' 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)
 
Private Extern libvlc_media_player_release(p_mi As Pointer)
  
void libvlc_media_release (libvlc_media_t *p_md)
+
<FONT Color=gray>' ''void libvlc_media_release (libvlc_media_t *p_md)
 
' Decrement the reference count of a media descriptor object.
 
' Decrement the reference count of a media descriptor object.
 
Private Extern libvlc_media_release(p_md As Pointer)
 
Private Extern libvlc_media_release(p_md As Pointer)
  
libvlc_release (libvlc_instance_t * p_instance)
+
<FONT Color=gray>' ''libvlc_release (libvlc_instance_t * p_instance)
 
' Decrement the reference count of a libvlc instance, and destroy it if it reaches zero.
 
' Decrement the reference count of a libvlc instance, and destroy it if it reaches zero.
 
Private Extern libvlc_release(p_instance As Pointer)
 
Private Extern libvlc_release(p_instance As Pointer)
Riga 46: Riga 46:
 
  Public Sub ToggleButton1_Click()
 
  Public Sub ToggleButton1_Click()
 
   
 
   
Dim inst, mp, m As Pointer
+
  Dim inst, mp, m As Pointer
Dim tm As Date
+
  Dim tm As Date
  
If ToggleButton1.Value Then
+
  If ToggleButton1.Value Then
  
Inizializza la libreria VLC:
+
<FONT Color=gray>' ''Inizializza la libreria VLC:
  inst = libvlc_new(0, Null)
+
    inst = libvlc_new(0, Null)
  
Crea un nuovo oggetto multimedia.
+
<FONT Color=gray>' ''Crea un nuovo oggetto multimedia.
' Nel secondo argomento della funzione va specificato l'indirizzo del flusso di dati audio trasmessi dalla radio web:
+
' 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")
+
    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:
+
<FONT Color=gray>' ''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}}")
+
    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:
+
<FONT Color=gray>' ''Crea il media player:
  mp = libvlc_media_player_new(inst)
+
    mp = libvlc_media_player_new(inst)
  
  libvlc_media_player_set_media(mp, m)
+
    libvlc_media_player_set_media(mp, m)
  
  libvlc_media_release(m)
+
    libvlc_media_release(m)
  
Avvia la registrazione audio da parte del media player:
+
<FONT Color=gray>' ''Avvia la registrazione audio da parte del media player:
  libvlc_media_player_play(mp)
+
    libvlc_media_player_play(mp)
  
  tm = Now
+
    tm = Now
  
  While bo = False
+
    While bo = False
    Me.Title = Str(Time(0, 0, 0, DateDiff(tm, Now, gb.Millisecond)))
+
      Me.Title = Str(Time(0, 0, 0, DateDiff(tm, Now, gb.Millisecond)))
Il Wait consente di agire su altri oggetti posti sul Form:
+
<FONT Color=gray>' ''Il Wait consente di agire su altri oggetti posti sul Form:
    Wait 0.01
+
      Wait 0.01
  Wend
+
    Wend
  
Chiude la libreria VLC ed il programma:
+
<FONT Color=gray>' ''Chiude la libreria VLC ed il programma:
  libvlc_media_player_release(mp)
+
    libvlc_media_player_release(mp)
  libvlc_release(inst)
+
    libvlc_release(inst)
  Me.Close
+
    Me.Close
Else
+
  Else
Causa l'arresto della registrazione, la chiusura della libreria VLC e del programma:
+
<FONT Color=gray>' ''Causa l'arresto della registrazione, la chiusura della libreria VLC e del programma:
  bo = True
+
    bo = True
Endif
+
  Endif
 
+
End
+
End

Versione delle 18:50, 21 ago 2024

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