Differenze tra le versioni di "Eseguire file audio mediante le funzioni esterne del API di VLC"

Da Gambas-it.org - Wikipedia.
Riga 99: Riga 99:
 
   Until libvlc_media_player_get_state(mp) > libvlc_Paused
 
   Until libvlc_media_player_get_state(mp) > libvlc_Paused
 
   
 
   
   Chiude()
+
   Chiude(m)
 
   
 
   
 
  '''End'''
 
  '''End'''
Riga 112: Riga 112:
 
   
 
   
 
   
 
   
  '''Private''' Procedure Chiude()
+
  '''Private''' Procedure Chiude(md As Pointer)
 
      
 
      
 
  <FONT color=gray>' ''Rilascia e chiude il media player:''</font>
 
  <FONT color=gray>' ''Rilascia e chiude il media player:''</font>
Riga 118: Riga 118:
 
      
 
      
 
  <FONT color=gray>' ''Chiude il descrittore dell'oggetto media:''</font>
 
  <FONT color=gray>' ''Chiude il descrittore dell'oggetto media:''</font>
   libvlc_media_release(m)
+
   libvlc_media_release(md)
 
    
 
    
 
  <FONT color=gray>' ''Chiude la libreria VLC:''</font>
 
  <FONT color=gray>' ''Chiude la libreria VLC:''</font>

Versione delle 20:04, 23 ago 2020

La libreria LibVLC mette a disposizione funzioni e risorse anche per eseguire file audio.

I file audio eseguibili sono i seguenti:

  • MP3;
  • OGG;
  • WAV;
  • WMA.

E' possibile eseguire dati audio anche direttamente da internet.


Per creare in Gambas un'applicazione con la presente risorsa, si dovrà installare e richiamare la libreria dinamica e condivisa libvlc.so.5.6.0.

Esempio con applicazione grafica

Mostriamo di seguito un semplice esempio, nel quale si potrà avviare, arrestare, porre in pausa e riprendere l'esecuzione del file audio:

Private inst As Pointer
Private mp As Pointer
 

Library "libvlc:5.6.0"

Private Enum libvlc_NothingSpecial = 0,
        libvlc_Opening,
        libvlc_Buffering,
        libvlc_Playing,
        libvlc_Paused,
        libvlc_Stopped,
        libvlc_Ended,
        libvlc_Error

' 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_path (libvlc_instance_t *p_instance, const char *path)
' Create a media for a certain file path.
Private Extern libvlc_media_new_path(p_instance As Pointer, path As String) As Pointer

' libvlc_media_player_t * libvlc_media_player_new_from_media (libvlc_media_t *p_md)
' Create a Media Player object from a Media.
Private Extern libvlc_media_player_new_from_media(p_md As Pointer) As Pointer

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

' void libvlc_media_player_stop (libvlc_media_player_t * p_mi)
' Stop the audio file.
Private Extern libvlc_media_player_stop(p_mi As Pointer)

' libvlc_time_t libvlc_media_player_get_length(libvlc_media_player_t *, libvlc_exception_t *)
' Get the current movie length (in ms).
Private Extern libvlc_media_player_get_length(p_mi As Pointer, l_ex As Pointer) As Long

' libvlc_time_t libvlc_media_player_get_time(libvlc_media_player_t * p_mi)
' Get the current movie time (in ms).
Private Extern libvlc_media_player_get_time(p_mi As Pointer) As Long

' libvlc_state_t libvlc_media_player_get_state(libvlc_media_player_t *p_mi)
' Get current movie state.
Private Extern libvlc_media_player_get_state(p_mi As Pointer) As Integer

' void   libvlc_media_player_pause (libvlc_media_player_t *p_mi)
' Toggle pause.
Private Extern libvlc_media_player_pause(p_mi As Pointer)

' 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 Button1_Click()

 Dim m As Pointer
   
' Inizializza la libreria VLC:
  inst = libvlc_new(0, Null)
   
' Crea un nuovo oggetto multimedia:
  m = libvlc_media_new_path(inst, "/percorso/del/file/audio")
   
' Crea un media player:
  mp = libvlc_media_player_new_from_media(m)
   
 ' Avvia l'esecuzione del file audio da parte del media player:
  libvlc_media_player_play(mp)

  Repeat
    TextLabel1.Text = "Durata: " & Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_length(mp, 0)))
    TextLabel2.Text = "<FONT Color=red>" & Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_time(mp)))
    Wait 0.01
  Until libvlc_media_player_get_state(mp) > libvlc_Paused

  Chiude(m)

End


Public Sub Button2_Click()

' Arresta l'esecuzione del file audio:
  libvlc_media_player_stop(mp)

End


Private Procedure Chiude(md As Pointer)
   
' Rilascia e chiude il media player:
  libvlc_media_player_release(mp)
    
' Chiude il descrittore dell'oggetto media:
  libvlc_media_release(md)
  
' Chiude la libreria VLC:
  libvlc_release(inst)
   
End


Public Sub ToggleButton1_Click()

' Pone in pausa o riprende (se già in pausa) l'esecuzione del file audio:
  libvlc_media_player_pause(mp)

End


In quest'altro esempio mostreremo un effetto video (Spectrum analyzer) di VLC, che rappresenta un "Analizzatore di spettro", all'interno di una DrawingArea

Private inst As Pointer
Private mp As Pointer
Private m As Pointer
 

Library "libvlc:5.6.0"

Private Enum libvlc_NothingSpecial = 0,
        libvlc_Opening,
        libvlc_Buffering,
        libvlc_Playing,
        libvlc_Paused,
        libvlc_Stopped,
        libvlc_Ended,
        libvlc_Error

' 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_path (libvlc_instance_t *p_instance, const char *path)
' Create a media for a certain file path.
Private Extern libvlc_media_new_path(p_instance As Pointer, path As String) As Pointer

' libvlc_media_player_t * libvlc_media_player_new_from_media (libvlc_media_t *p_md)
' Create a Media Player object from a Media.
Private Extern libvlc_media_player_new_from_media(p_md As Pointer) As Pointer

' void libvlc_media_player_set_xwindow (libvlc_media_player_t *p_mi, uint32_t drawable)
' Set an X Window System drawable where the media player should render its video output.
Private Extern libvlc_media_player_set_xwindow(p_mi As Pointer, drawable As Integer)

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

' void libvlc_media_player_stop (libvlc_media_player_t * p_mi)
' Stop the audio file.
Private Extern libvlc_media_player_stop(p_mi As Pointer)

' libvlc_time_t libvlc_media_player_get_length(libvlc_media_player_t *, libvlc_exception_t *)
' Get the current movie length (in ms).
Private Extern libvlc_media_player_get_length(p_mi As Pointer, l_ex As Pointer) As Long

' libvlc_time_t libvlc_media_player_get_time(libvlc_media_player_t * p_mi)
' Get the current movie time (in ms).
Private Extern libvlc_media_player_get_time(p_mi As Pointer) As Long

' libvlc_state_t libvlc_media_player_get_state(libvlc_media_player_t *p_mi)
' Get current movie state.
Private Extern libvlc_media_player_get_state(p_mi As Pointer) As Integer

' void   libvlc_media_player_pause (libvlc_media_player_t *p_mi)
' Toggle pause.
Private Extern libvlc_media_player_pause(p_mi As Pointer)

' 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 Button1_Click()

 Dim ss As String[]
 Dim id As Integer
  
  ss = ["--audio-visual=visual", "--effect-list=spectrum"]
  
' Inizializza la libreria VLC:
  inst = libvlc_new(ss.Count, ss)
   
' Crea un nuovo oggetto multimedia:
  m = libvlc_media_new_path(inst, "/percorso/del/file/audio")
   
' Crea un media player:
  mp = libvlc_media_player_new_from_media(m)
  
' Per far mostrare l'effetto video "Analizzatore di spettro" nella "DrawingArea", ricaviamo il suo identificativo:
  id = DrawingArea1.Id
  
' Passiamo l'identificativo della finestra, nella quale dovrà essere mostrato l'effetto "Spectrum analyzer":
  libvlc_media_player_set_xwindow(mp, id)
   
 ' Avvia l'esecuzione del file audio da parte del media player:
  libvlc_media_player_play(mp)

  Repeat
    TextLabel1.Text = "Durata: " & Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_length(mp, 0)))
    TextLabel2.Text = "<FONT Color=red>" & Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_time(mp)))
    Wait 0.01
  Until libvlc_media_player_get_state(mp) > libvlc_Paused

  Chiude()

End


Public Sub Button2_Click()

' Arresta l'esecuzione del file audio:
  libvlc_media_player_stop(mp)

End


Private Procedure Chiude()
   
' Rilascia e chiude il media player:
  libvlc_media_player_release(mp)
    
' Chiude il descrittore dell'oggetto media:
  libvlc_media_release(m)
  
' Chiude la libreria VLC:
  libvlc_release(inst)
   
End


Public Sub ToggleButton1_Click()

' Pone in pausa o riprende (se già in pausa) l'esecuzione del file audio:
  libvlc_media_player_pause(mp)

End


Quest'altro esempio è più completo e complesso del precedente. La particolarità del codice seguente è che è possibile selezionare nel codice fino a 5 effetti grafici VLC.

Private bt1 As Button
Private bt2 As Button
Private tb As ToggleButton
Private tl0 As TextLabel
Private tl1 As TextLabel
Private tl2 As TextLabel
Private menu As Menu
Private fileaudio As String
Private inst As Pointer
Private m As Pointer
Private mp As Pointer


Library "libvlc:5.6.0"

Private Enum libvlc_NothingSpecial = 0, libvlc_Opening, libvlc_Buffering, libvlc_Playing,
             libvlc_Paused, libvlc_Stopped, libvlc_Ended, libvlc_Error

' 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_path (libvlc_instance_t *p_instance, const char *path)
' Create a media for a certain file path.
Private Extern libvlc_media_new_path(p_instance As Pointer, path As String) As Pointer

' libvlc_media_player_t * libvlc_media_player_new_from_media (libvlc_media_t *p_md)
' Create a Media Player object from a Media.
Private Extern libvlc_media_player_new_from_media(p_md As Pointer) As Pointer

' void libvlc_media_player_set_xwindow (libvlc_media_player_t *p_mi, uint32_t drawable)
' Set an X Window System drawable where the media player should render its video output.
Private Extern libvlc_media_player_set_xwindow(p_mi As Pointer, drawable As Integer)

' 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_stop (libvlc_media_player_t * p_mi)
' Stop the video file.
Private Extern libvlc_media_player_stop(p_mi As Pointer)

' libvlc_time_t libvlc_media_player_get_length(libvlc_media_player_t *, libvlc_exception_t *)
' Get the current movie length (in ms).
Private Extern libvlc_media_player_get_length(p_mi As Pointer, l_ex As Pointer) As Long

' libvlc_time_t libvlc_media_player_get_time(libvlc_media_player_t * p_mi)
' Get the current movie time (in ms).
Private Extern libvlc_media_player_get_time(p_mi As Pointer) As Long

' libvlc_state_t libvlc_media_player_get_state(libvlc_media_player_t *p_mi)
' Get current movie state.
Private Extern libvlc_media_player_get_state(p_mi As Pointer) As Integer

' void libvlc_media_player_pause (libvlc_media_player_t *p_mi)
' Toggle pause.
Private Extern libvlc_media_player_pause(p_mi As Pointer)

' 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 Form_Open()
 
 Dim effetti, ss As String[]
 Dim argc As Integer
 
  GestioneOggetti()
  effetti = ["dummy", "scope", "spectrum", "spectrometer", "vuMeter"]
  
  ss = ["--audio", "--audio-visual=visual", "--effect-list=" & effetti[4], "--visual-80-bands", "--visual-peaks", 
        "--width= 350", "--height=250", "--gain=1.000", "--no-xli"]
  
' Indica quanti, e dunque, quali effetti, compresi nel vettore "ss" si vogliono utilizzare:
  argc = ss.Count
  
' Inizializza la libreria VLC:
  inst = libvlc_new(argc, ss)
  
End


Public Sub Button1_Click()
 
 Dim id As Integer
 Dim DrAr As DrawingArea
  
  With DrAr = New DrawingArea(Me)
    .X = 10
    .Y = 10
    .W = 350
    .H = 250
  End With
  
  bt1.Enabled = False
  tb.Enabled = True
  tb.Value = False
   
' Crea un nuovo oggetto multimedia.
' Nel secondo argomento della funzione va specificato il percorso del file audio.
  m = libvlc_media_new_path(inst, fileaudio)
  
' Crea un media player:
  mp = libvlc_media_player_new_from_media(m)
  
' Per far mostrare l'effetto VLC nella "DrawingArea", ricaviamo il suo identificativo:
  id = DrAr.Id
  
' Passiamo l'identificativo della finestra, nella quale dovrà essere mostrato l'effetto VLC:
  libvlc_media_player_set_xwindow(mp, id)
  
' Avvia l'esecuzione del file audio da parte del media player:
  libvlc_media_player_play(mp)
  
  Repeat
    tl1.Text = "Durata: " & Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_length(mp, 0)))
    tl2.Text = "<FONT Color=red>" & Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_time(mp)))
    Wait 0.01
  Until libvlc_media_player_get_state(mp) > libvlc_Paused
  
  mp = 0
  bt1.Enabled = True
  
End


Public Sub Button2_Click()
 
 Stop()
 
End


Public Sub ToggleButton1_Click()
 
' Pone in pausa o riprende (se già in pausa) l'esecuzione del file audio:
 libvlc_media_player_pause(mp)
  
End


Public Sub Form_Close()
 
 If Object.IsValid(Me) Then Chiude()
 
End


Public Sub Menu_Click()
 
 Select Case Last.Name
   Case "Apri"
     With Dialog
       .Title = "Apre file..."
       .Filter = ["*.mp3; *.ogg; *.wav; *.wma", "File audio"]
       If .OpenFile() Then Return
       fileaudio = .Path
     End With
     If mp > 0 Then Stop()
     tl0.Text = File.Name(fileaudio)
     tl1.Text = Null
     tl2.Text = Null
     bt1.Enabled = True
   Case "Esci"
     Chiude()
 End Select
  
End


Private Procedure Chiude()
 
 If mp > 0 Then
' Arresta l'esecuzione del file audio:
   libvlc_media_player_stop(mp)
' Rilascia e chiude il media player:
   libvlc_media_player_release(mp)
' Chiude l'oggetto media:
   libvlc_media_release(m)
 Endif
 
' Chiude la libreria VLC:
 libvlc_release(inst)
 
 If Object.IsValid(Me) Then
   Object.Lock(Me)
   Me.Close
 Endif
  
End


Private Procedure Stop()
 
 If mp > 0 Then
' Arresta l'esecuzione del file video:
   libvlc_media_player_stop(mp)
' Rilascia e chiude il media player:
   libvlc_media_player_release(mp)
 Endif
 
 tb.Enabled = False
 tb.Value = False
  
End


Private Procedure GestioneOggetti()
 
 Dim mn1, mn2 As Menu
 
  With Me
    .W = 400
    .H = 370
  End With
  With tl0 = New TextLabel(Me)
    .X = 10
    .Y = 265
    .W = 350
    .H = 20
  End With
  With tl1 = New TextLabel(Me)
    .X = 10
    .Y = tl0.Y + tl0.H
    .W = 120
    .H = 18
    .Alignment = Align.Top
  End With
  With tl2 = New TextLabel(Me)
    .X = tl1.X + tl1.W + 30
    .Y = tl0.Y + tl0.H
    .W = 120
    .H = 18
    .Alignment = Align.TopLeft
  End With
  With bt1 = New Button(Me) As "Button1"
    .X = 10
    .Y = 300
    .W = 300
    .H = 40
    .Foreground = Color.DarkGreen
    .Text = "Esegui"
    .Enabled = False
  End With
  With bt2 = New Button(Me) As "Button2"
    .X = bt1.X + bt1.W + 20
    .Y = 300
    .W = 50
    .H = 40
    .Foreground = Color.DarkRed
    .Text = "Stop"
  End With
  With tb = New ToggleButton(Me) As "ToggleButton1"
    .X = bt2.X
    .Y = 220
    .W = 50
    .H = 40
    .Text = "Pausa"
    .Enabled = False
  End With
  With menu = New Menu(Me) As "Menu"
    .Caption = "File"
  End With
  With mn1 = New Menu(menu, False) As "Menu"
   .Caption = "Apri file..."
   .Name = "Apri"
  End With
  With mn2 = New Menu(menu, False) As "Menu"
   .Caption = "Esci"
   .Name = "Esci"
  End With
  
End


Esempio con applicazione a riga di comando

Library "libvlc:5.6.0"

Private Enum libvlc_NothingSpecial = 0,
        libvlc_Opening,
        libvlc_Buffering,
        libvlc_Playing,
        libvlc_Paused,
        libvlc_Stopped,
        libvlc_Ended,
        libvlc_Error

' 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_path (libvlc_instance_t *p_instance, const char *path)
' Create a media for a certain file path.
Private Extern libvlc_media_new_path(p_instance As Pointer, path As String) As Pointer

' libvlc_media_player_t * libvlc_media_player_new_from_media (libvlc_media_t *p_md)
' Create a Media Player object from a Media.
Private Extern libvlc_media_player_new_from_media(p_md As Pointer) As Pointer

' int libvlc_media_player_play (libvlc_media_player_t * p_mi)
' Play the audio file.
Private Extern libvlc_media_player_play(p_mi As Pointer) As Integer
 
' libvlc_time_t libvlc_media_player_get_time(libvlc_media_player_t * p_mi)
' Get the current movie time (in ms).
Private Extern libvlc_media_player_get_time(p_mi As Pointer) As Long

' libvlc_state_t libvlc_media_player_get_state(libvlc_media_player_t *p_mi)
' Get current movie state.
Private Extern libvlc_media_player_get_state(p_mi As Pointer) As Long
 
' 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 Main()

 Dim inst, m, mp As Pointer
      
' Inizializza la libreria VLC:
   inst = libvlc_new(0, Null)
   
' Crea un nuovo oggetto multimedia:
   m = libvlc_media_new_path(inst, "/percorso/del/file/audio")
   
' Crea un media player:
   mp = libvlc_media_player_new_from_media(m)
   
 ' Avvia l'esecuzione del file audio da parte del media player:
   libvlc_media_player_play(mp)

   While libvlc_media_player_get_state(mp) < libvlc_Stopped
     Write Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_time(mp))) & "\r"
   Wend


' Va in chiusura:
   libvlc_media_player_release(mp)
   libvlc_media_release(m)
   libvlc_release(inst)

End




Riferimenti