Differenze tra le versioni di "Eseguire file audio mediante le funzioni esterne del API di VLC"
(28 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 8: | Riga 8: | ||
E' possibile eseguire dati audio anche direttamente da internet. | 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 condivisa "''libvlc.so.5.6.1'' ". | |
− | Per creare in Gambas un'applicazione con la presente risorsa, si dovrà installare e richiamare la libreria | ||
===Esempio con applicazione grafica=== | ===Esempio con applicazione grafica=== | ||
Riga 17: | Riga 16: | ||
− | Library "libvlc:5.6. | + | Library "libvlc:5.6.1" |
Private Enum libvlc_NothingSpecial = 0, | Private Enum libvlc_NothingSpecial = 0, | ||
− | + | libvlc_Opening, | |
− | + | libvlc_Buffering, | |
− | + | libvlc_Playing, | |
− | + | libvlc_Paused, | |
− | + | libvlc_Stopped, | |
− | + | libvlc_Ended, | |
− | + | libvlc_Error | |
<FONT color=gray>' ''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)'' | ||
Riga 63: | Riga 62: | ||
' ''Toggle pause.''</font> | ' ''Toggle pause.''</font> | ||
Private Extern libvlc_media_player_pause(p_mi As Pointer) | Private Extern libvlc_media_player_pause(p_mi As Pointer) | ||
+ | |||
+ | <FONT color=gray>' ''int libvlc_audio_set_volume(libvlc_media_player_t *p_mi, int i_volume)'' | ||
+ | ' ''Set current software audio volume.''</font> | ||
+ | Private Extern libvlc_audio_set_volume(p_mi As Pointer, i_volume As Integer) As Integer | ||
<FONT color=gray>' ''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)'' | ||
Riga 72: | Riga 75: | ||
Private Extern libvlc_media_release(p_md As Pointer) | Private Extern libvlc_media_release(p_md As Pointer) | ||
− | <FONT color=gray>' ''libvlc_release (libvlc_instance_t * p_instance)'' | + | <FONT color=gray>' ''void libvlc_release (libvlc_instance_t * p_instance)'' |
' ''Decrement the reference count of a libvlc instance, and destroy it if it reaches zero.''</font> | ' ''Decrement the reference count of a libvlc instance, and destroy it if it reaches zero.''</font> | ||
Private Extern libvlc_release(p_instance As Pointer) | Private Extern libvlc_release(p_instance As Pointer) | ||
− | + | Public Sub Form_Open() | |
+ | |||
+ | With Slider1 | ||
+ | .MaxValue = 100 | ||
+ | .MinValue = 0 | ||
+ | .Value = 50 | ||
+ | End With | ||
+ | |||
+ | End | ||
+ | |||
+ | |||
+ | Public Sub Button1_Click() | ||
Dim m As Pointer | Dim m As Pointer | ||
<FONT color=gray>' ''Inizializza la libreria VLC:''</font> | <FONT color=gray>' ''Inizializza la libreria VLC:''</font> | ||
− | + | inst = libvlc_new(0, Null) | |
<FONT color=gray>' ''Crea un nuovo oggetto multimedia:''</font> | <FONT color=gray>' ''Crea un nuovo oggetto multimedia:''</font> | ||
− | + | m = libvlc_media_new_path(inst, "<FONT color=darkgreen>''/percorso/del/file/audio''</font>") | |
<FONT color=gray>' ''Crea un media player:''</font> | <FONT color=gray>' ''Crea un media player:''</font> | ||
− | + | mp = libvlc_media_player_new_from_media(m) | |
− | + | ||
+ | <FONT color=gray>' ''Chiude il descrittore dell'oggetto media:''</font> | ||
+ | libvlc_media_release(m) | ||
+ | |||
+ | <FONT color=gray>' ''Imposta il valore iniziale del volume dell'audio in base al valore iniziale dello "Slider":''</font> | ||
+ | libvlc_audio_set_volume(mp, Slider1.Value) | ||
+ | |||
<FONT color=gray>' ''Avvia l'esecuzione del file audio da parte del media player:''</font> | <FONT color=gray>' ''Avvia l'esecuzione del file audio da parte del media player:''</font> | ||
− | + | libvlc_media_player_play(mp) | |
− | + | Repeat | |
− | + | Wait 0.01 | |
− | + | Until libvlc_media_player_get_length(mp, 0) > 0.0 | |
− | |||
− | |||
− | + | Repeat | |
+ | TextLabel1.Text = "Durata: " & Str(Time(0, 0, 0, libvlc_media_player_get_length(mp, 0))) | ||
+ | TextLabel2.Text = "<FONT Color=red>" & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) | ||
+ | ProgressBar1.Value = libvlc_media_player_get_time(mp) / libvlc_media_player_get_length(mp, 0) | ||
+ | Wait 0.01 | ||
+ | Until libvlc_media_player_get_state(mp) > libvlc_Paused | ||
+ | |||
+ | <FONT color=gray>' ''Rilascia e chiude il media player:''</font> | ||
+ | libvlc_media_player_release(mp) | ||
+ | |||
+ | <FONT color=gray>' ''Chiude la libreria VLC:''</font> | ||
+ | libvlc_release(inst) | ||
− | + | End | |
− | + | Public Sub Button2_Click() | |
<FONT color=gray>' ''Arresta l'esecuzione del file audio:''</font> | <FONT color=gray>' ''Arresta l'esecuzione del file audio:''</font> | ||
− | + | libvlc_media_player_stop(mp) | |
− | + | End | |
− | + | Public Sub ToggleButton1_Click() | |
− | + | ||
− | <FONT color=gray>' '' | + | <FONT color=gray>' ''Pone in pausa o riprende (se già in pausa) l'esecuzione del file audio:''</font> |
− | + | libvlc_media_player_pause(mp) | |
− | + | ||
− | + | End | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | Public Sub Slider1_Change() | |
− | <FONT color=gray>' '' | + | <FONT color=gray>' ''Modifica il volume audio:''</font> |
− | + | If mp > 0 Then libvlc_audio_set_volume(mp, Slider1.Value) | |
− | + | End | |
+ | ===Spostare l'esecuzione del file audio nell'ambito della sua durata=== | ||
+ | Si può avanzare o retrocedere all'interno dell'esecuzione di un <U>file</u> audio mediante la funzione esterna "libvlc_media_player_set_time()". | ||
+ | |||
+ | Innanzitutto si dichiarerà formalmente la funzione esterna "libvlc_media_player_set_time()" di libvlc: | ||
+ | <FONT color=gray>' ''void libvlc_media_player_set_time (libvlc_media_player_t *p_mi, libvlc_time_t i_time)'' | ||
+ | ' ''Set the movie time (in ms).''</font> | ||
+ | Private Extern libvlc_media_player_set_time(player As Pointer, i_time As Long) | ||
+ | |||
+ | Volendo usare uno ''Slider'', imposteremo i valori iniziali dello ''Slider'', dedicato allo spostamento dell'esecuzione del file audio, nella routine: | ||
+ | Public Sub Form_Open() | ||
+ | |||
+ | ...... | ||
+ | |||
+ | With Slider2 | ||
+ | .MaxValue = 1000 | ||
+ | .MinValue = 0 | ||
+ | .Value = 0 | ||
+ | End With | ||
+ | |||
+ | End | ||
+ | Inoltre imposteremo una specifica routine per lo ''Slider'' dedicato allo spostamento dell'esecuzione del file audio: | ||
+ | Public Sub Slider2_Change() | ||
+ | |||
+ | <FONT color=gray>' ''Sposta l'esecuzione del file audio nell'ambito della sua durata:''</font> | ||
+ | libvlc_media_player_set_time(mp, (Slider2.Value * libvlc_media_player_get_length(mp, 0)) / 1000) | ||
+ | |||
+ | End | ||
+ | ===Far variare la posizione del cursore dello ''Slider'' in base all'esecuzione=== | ||
+ | Se si preferisce far variare la posizione del cursore dello ''Slider'' in base al punto, all'evolversi dell'esecuzione del file audio, e poter arbitrariamente far avanzare e retrocedere il punto dell'esecuzione muovendo il cursore dello ''Slider'', allora potrà adottarsi un codice come il seguente: | ||
+ | Private inst As Pointer | ||
+ | Private mp As Pointer | ||
+ | Private m As Pointer | ||
+ | Private bo As Boolean | ||
+ | |||
+ | |||
+ | Library "libvlc:5.6.1" | ||
+ | |||
+ | Private Enum libvlc_NothingSpecial = 0, libvlc_Opening, libvlc_Buffering, libvlc_Playing, | ||
+ | libvlc_Paused, libvlc_Stopped, libvlc_Ended, libvlc_Error | ||
+ | |||
+ | <Font Color=gray>' ''libvlc_instance_t * libvlc_new (int argc, const char *const *argv)'' | ||
+ | ' ''Create And initialize a libvlc instance.''</font> | ||
+ | Private Extern libvlc_new(argc As Integer, argv As String[]) As Pointer | ||
+ | |||
+ | <Font Color=gray>' ''libvlc_media_t * libvlc_media_new_path (libvlc_instance_t *p_instance, const char *path)'' | ||
+ | ' ''Create a media for a certain file path.''</font> | ||
+ | Private Extern libvlc_media_new_path(p_instance As Pointer, path As String) As Pointer | ||
+ | |||
+ | <Font Color=gray>' ''libvlc_media_player_t * libvlc_media_player_new_from_media (libvlc_media_t *p_md)'' | ||
+ | ' ''Create a Media Player object from a Media.''</font> | ||
+ | Private Extern libvlc_media_player_new_from_media(p_md As Pointer) As Pointer | ||
+ | |||
+ | <Font Color=gray>' ''int libvlc_media_player_play (libvlc_media_player_t * p_mi)'' | ||
+ | ' ''Play the video file.''</font> | ||
+ | Private Extern libvlc_media_player_play(p_mi As Pointer) As Integer | ||
+ | |||
+ | <Font Color=gray>' ''void libvlc_media_player_stop (libvlc_media_player_t * p_mi)'' | ||
+ | ' ''Stop the video file.''</font> | ||
+ | Private Extern libvlc_media_player_stop(p_mi As Pointer) | ||
+ | |||
+ | <Font Color=gray>' ''libvlc_time_t libvlc_media_player_get_length(libvlc_media_player_t *, libvlc_exception_t *)'' | ||
+ | ' ''Get the current movie length (in ms).''</font> | ||
+ | Private Extern libvlc_media_player_get_length(p_mi As Pointer, l_ex As Pointer) As Integer | ||
+ | |||
+ | <Font Color=gray>' ''libvlc_time_t libvlc_media_player_get_time(libvlc_media_player_t * p_mi)'' | ||
+ | ' ''Get the current movie time (in ms).''</font> | ||
+ | Private Extern libvlc_media_player_get_time(p_mi As Pointer) As Integer | ||
+ | |||
+ | <Font Color=gray>' ''libvlc_state_t libvlc_media_player_get_state(libvlc_media_player_t *p_mi)'' | ||
+ | ' ''Get current movie state.''</font> | ||
+ | Private Extern libvlc_media_player_get_state(p_mi As Pointer) As Integer | ||
+ | |||
+ | <Font Color=gray>' ''void libvlc_media_player_pause (libvlc_media_player_t *p_mi)'' | ||
+ | ' ''Toggle pause.''</font> | ||
+ | Private Extern libvlc_media_player_pause(p_mi As Pointer) | ||
+ | |||
+ | <Font Color=gray>' ''void libvlc_media_player_set_time (libvlc_media_player_t *p_mi, libvlc_time_t i_time)'' | ||
+ | ' ''Set the movie time (in ms).''</font> | ||
+ | Private Extern libvlc_media_player_set_time(player As Pointer, i_time As Long) | ||
+ | |||
+ | <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.''</font> | ||
+ | Private Extern libvlc_media_player_release(p_mi As Pointer) | ||
+ | |||
+ | <Font Color=gray>' ''void libvlc_media_release (libvlc_media_t *p_md)'' | ||
+ | ' ''Decrement the reference count of a media descriptor object.''</font> | ||
+ | Private Extern libvlc_media_release(p_md As Pointer) | ||
+ | |||
+ | <Font Color=gray>' ''void libvlc_release (libvlc_instance_t * p_instance)'' | ||
+ | ' ''Decrement the reference count of a libvlc instance, and destroy it if it reaches zero.''</font> | ||
+ | Private Extern libvlc_release(p_instance As Pointer) | ||
+ | |||
+ | |||
+ | Public Sub Form_Open() | ||
+ | |||
+ | With Slider1 | ||
+ | .MaxValue = 100 | ||
+ | .MinValue = 0 | ||
+ | .Value = 0 | ||
+ | End With | ||
+ | |||
+ | End | ||
+ | |||
+ | |||
+ | Public Sub Button1_Click() | ||
+ | |||
+ | Slider1.Value = 0 | ||
+ | |||
+ | inst = libvlc_new(0, Null) | ||
+ | |||
+ | m = libvlc_media_new_path(inst, "<FONT color=darkgreen>''/percorso/del/file/audio''</font>") | ||
+ | |||
+ | mp = libvlc_media_player_new_from_media(m) | ||
+ | |||
+ | libvlc_media_player_play(mp) | ||
+ | |||
+ | Repeat | ||
+ | Wait 0.01 | ||
+ | Until libvlc_media_player_get_length(mp, 0) > 0.0 | ||
+ | |||
+ | Repeat | ||
+ | Me.Title = "Durata: " & Str(Time(0, 0, 0, libvlc_media_player_get_length(mp, 0))) & | ||
+ | " - " & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) | ||
+ | ProgressBar1.Value = libvlc_media_player_get_time(mp) / libvlc_media_player_get_length(mp, 0) | ||
+ | If Not bo Then Slider1.Value = ProgressBar1.Value * 100 | ||
+ | Wait 0.01 | ||
+ | Until libvlc_media_player_get_state(mp) > libvlc_Paused | ||
+ | |||
+ | Chiude() | ||
+ | |||
+ | End | ||
+ | |||
+ | |||
+ | Public Sub Button2_Click() | ||
+ | |||
+ | libvlc_media_player_stop(mp) | ||
+ | |||
+ | DrawingArea1.Clear | ||
+ | |||
+ | End | ||
+ | |||
+ | |||
+ | Private Procedure Chiude() | ||
+ | |||
+ | libvlc_media_player_release(mp) | ||
+ | libvlc_media_release(m) | ||
+ | libvlc_release(inst) | ||
+ | |||
+ | End | ||
+ | |||
+ | |||
+ | Public Sub ToggleButton1_Click() | ||
+ | |||
+ | libvlc_media_player_pause(mp) | ||
+ | |||
+ | End | ||
+ | |||
+ | |||
+ | Public Sub Slider1_MouseDown() | ||
+ | |||
+ | bo = True | ||
+ | |||
+ | End | ||
+ | |||
+ | |||
+ | Public Sub Slider1_MouseUp() | ||
+ | |||
+ | bo = False | ||
+ | |||
+ | libvlc_media_player_set_time(mp, (Slider1.Value * libvlc_media_player_get_length(mp, 0)) / 100) | ||
+ | |||
+ | End | ||
+ | |||
+ | ===Impostare anche effetti video=== | ||
In quest'altro esempio mostreremo un effetto video (''Spectrum analyzer'') di VLC, che rappresenta un "Analizzatore di spettro", all'interno di una ''DrawingArea'' | 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 inst As Pointer | ||
Riga 139: | Riga 336: | ||
− | Library "libvlc:5.6. | + | Library "libvlc:5.6.1" |
Private Enum libvlc_NothingSpecial = 0, | Private Enum libvlc_NothingSpecial = 0, | ||
− | + | libvlc_Opening, | |
− | + | libvlc_Buffering, | |
− | + | libvlc_Playing, | |
− | + | libvlc_Paused, | |
− | + | libvlc_Stopped, | |
− | + | libvlc_Ended, | |
− | + | libvlc_Error | |
<FONT color=gray>' ''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)'' | ||
Riga 203: | Riga 400: | ||
− | + | Public Sub Button1_Click() | |
Dim ss As String[] | Dim ss As String[] | ||
Riga 209: | Riga 406: | ||
Dim id As Integer | Dim id As Integer | ||
− | + | ss = ["--audio-visual=visual", "--effect-list=spectrum"] | |
<FONT color=gray>' ''Inizializza la libreria VLC:''</font> | <FONT color=gray>' ''Inizializza la libreria VLC:''</font> | ||
− | + | inst = libvlc_new(ss.Count, ss) | |
<FONT color=gray>' ''Crea un nuovo oggetto multimedia:''</font> | <FONT color=gray>' ''Crea un nuovo oggetto multimedia:''</font> | ||
− | + | m = libvlc_media_new_path(inst, "<FONT color=darkgreen>''/percorso/del/file/audio''</font>") | |
<FONT color=gray>' ''Crea un media player:''</font> | <FONT color=gray>' ''Crea un media player:''</font> | ||
− | + | mp = libvlc_media_player_new_from_media(m) | |
+ | |||
+ | <FONT color=gray>' ''Chiude il descrittore dell'oggetto media:''</font> | ||
+ | libvlc_media_release(m) | ||
<FONT color=gray>' ''Per far mostrare l'effetto video "Analizzatore di spettro" nella "DrawingArea", ricaviamo il suo identificativo:''</font> | <FONT color=gray>' ''Per far mostrare l'effetto video "Analizzatore di spettro" nella "DrawingArea", ricaviamo il suo identificativo:''</font> | ||
− | + | id = DrawingArea1.Id | |
<FONT color=gray>' ''Passiamo l'identificativo della finestra, nella quale dovrà essere mostrato l'effetto "Spectrum analyzer":''</font> | <FONT color=gray>' ''Passiamo l'identificativo della finestra, nella quale dovrà essere mostrato l'effetto "Spectrum analyzer":''</font> | ||
− | + | libvlc_media_player_set_xwindow(mp, id) | |
<FONT color=gray>' ''Avvia l'esecuzione del file audio da parte del media player:''</font> | <FONT color=gray>' ''Avvia l'esecuzione del file audio da parte del media player:''</font> | ||
− | + | libvlc_media_player_play(mp) | |
− | + | Repeat | |
− | + | TextLabel1.Text = "Durata: " & Str(Time(0, 0, 0, libvlc_media_player_get_length(mp, 0))) | |
− | + | TextLabel2.Text = "<FONT Color=red>" & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) | |
− | + | Wait 0.01 | |
− | + | Until libvlc_media_player_get_state(mp) > libvlc_Paused | |
− | + | <FONT color=gray>' ''Rilascia e chiude il media player:''</font> | |
+ | libvlc_media_player_release(mp) | ||
+ | |||
+ | <FONT color=gray>' ''Chiude la libreria VLC:''</font> | ||
+ | libvlc_release(inst) | ||
− | + | End | |
− | + | Public Sub Button2_Click() | |
<FONT color=gray>' ''Arresta l'esecuzione del file audio:''</font> | <FONT color=gray>' ''Arresta l'esecuzione del file audio:''</font> | ||
− | + | libvlc_media_player_stop(mp) | |
− | + | End | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | Public Sub ToggleButton1_Click() | |
<FONT color=gray>' ''Pone in pausa o riprende (se già in pausa) l'esecuzione del file audio:''</font> | <FONT color=gray>' ''Pone in pausa o riprende (se già in pausa) l'esecuzione del file audio:''</font> | ||
− | + | libvlc_media_player_pause(mp) | |
− | + | End | |
− | + | Quest'altro esempio è più completo e complesso del precedente: è possibile selezionare nel codice fino a 5 effetti grafici VLC. | |
− | |||
− | Quest'altro esempio è più completo e complesso del precedente | ||
Private bt1 As Button | Private bt1 As Button | ||
Private bt2 As Button | Private bt2 As Button | ||
Riga 284: | Riga 472: | ||
− | Library "libvlc:5.6. | + | Library "libvlc:5.6.1" |
Private Enum libvlc_NothingSpecial = 0, libvlc_Opening, libvlc_Buffering, libvlc_Playing, | Private Enum libvlc_NothingSpecial = 0, libvlc_Opening, libvlc_Buffering, libvlc_Playing, | ||
Riga 342: | Riga 530: | ||
− | + | Public Sub Form_Open() | |
Dim effetti, ss As String[] | Dim effetti, ss As String[] | ||
Dim argc As Integer | 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"] | |
<FONT Color=gray>' ''Indica quanti, e dunque, quali effetti, compresi nel vettore "ss" si vogliono utilizzare:''</font> | <FONT Color=gray>' ''Indica quanti, e dunque, quali effetti, compresi nel vettore "ss" si vogliono utilizzare:''</font> | ||
− | + | argc = ss.Count | |
<FONT Color=gray>' ''Inizializza la libreria VLC:''</font> | <FONT Color=gray>' ''Inizializza la libreria VLC:''</font> | ||
− | + | inst = libvlc_new(argc, ss) | |
− | + | End | |
− | + | Public Sub Button1_Click() | |
Dim id As Integer | Dim id As Integer | ||
Dim DrAr As DrawingArea | 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 | |
<FONT Color=gray>' ''Crea un nuovo oggetto multimedia.'' | <FONT Color=gray>' ''Crea un nuovo oggetto multimedia.'' | ||
' ''Nel secondo argomento della funzione va specificato il percorso del file audio.''</font> | ' ''Nel secondo argomento della funzione va specificato il percorso del file audio.''</font> | ||
− | + | m = libvlc_media_new_path(inst, fileaudio) | |
<FONT Color=gray>' ''Crea un media player:''</font> | <FONT Color=gray>' ''Crea un media player:''</font> | ||
− | + | mp = libvlc_media_player_new_from_media(m) | |
+ | |||
+ | <FONT color=gray>' ''Chiude l'oggetto media:''</font> | ||
+ | libvlc_media_release(m) | ||
<FONT Color=gray>' ''Per far mostrare l'effetto VLC nella "DrawingArea", ricaviamo il suo identificativo:''</font> | <FONT Color=gray>' ''Per far mostrare l'effetto VLC nella "DrawingArea", ricaviamo il suo identificativo:''</font> | ||
− | + | id = DrAr.Id | |
<FONT Color=gray>' ''Passiamo l'identificativo della finestra, nella quale dovrà essere mostrato l'effetto VLC:''</font> | <FONT Color=gray>' ''Passiamo l'identificativo della finestra, nella quale dovrà essere mostrato l'effetto VLC:''</font> | ||
− | + | libvlc_media_player_set_xwindow(mp, id) | |
<FONT Color=gray>' ''Avvia l'esecuzione del file audio da parte del media player:''</font> | <FONT Color=gray>' ''Avvia l'esecuzione del file audio da parte del media player:''</font> | ||
− | + | libvlc_media_player_play(mp) | |
− | + | Repeat | |
− | + | tl1.Text = "Durata: " & Str(Time(0, 0, 0, libvlc_media_player_get_length(mp, 0))) | |
− | + | tl2.Text = "<FONT Color=red>" & Str(Time(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() | Stop() | ||
− | + | End | |
− | + | Public Sub ToggleButton1_Click() | |
<FONT Color=gray>' ''Pone in pausa o riprende (se già in pausa) l'esecuzione del file audio:''</font> | <FONT Color=gray>' ''Pone in pausa o riprende (se già in pausa) l'esecuzione del file audio:''</font> | ||
libvlc_media_player_pause(mp) | libvlc_media_player_pause(mp) | ||
− | + | End | |
− | + | Public Sub Form_Close() | |
If Object.IsValid(Me) Then Chiude() | If Object.IsValid(Me) Then Chiude() | ||
− | |||
− | |||
+ | End | ||
+ | |||
+ | |||
+ | Public Sub Menu_Click() | ||
− | |||
− | |||
Select Case Last.Name | Select Case Last.Name | ||
Case "Apri" | Case "Apri" | ||
Riga 446: | Riga 637: | ||
Chiude() | Chiude() | ||
End Select | End Select | ||
− | + | ||
− | + | End | |
− | + | Private Procedure Chiude() | |
If mp > 0 Then | If mp > 0 Then | ||
Riga 457: | Riga 648: | ||
<FONT Color=gray>' ''Rilascia e chiude il media player:''</font> | <FONT Color=gray>' ''Rilascia e chiude il media player:''</font> | ||
libvlc_media_player_release(mp) | libvlc_media_player_release(mp) | ||
− | |||
− | |||
Endif | Endif | ||
Riga 469: | Riga 658: | ||
Endif | Endif | ||
− | + | End | |
− | + | Private Procedure Stop() | |
If mp > 0 Then | If mp > 0 Then | ||
Riga 484: | Riga 673: | ||
tb.Value = False | tb.Value = False | ||
− | + | End | |
− | + | Private Procedure GestioneOggetti() | |
Dim mn1, mn2 As Menu | 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..." | .Caption = "Apri file..." | ||
.Name = "Apri" | .Name = "Apri" | ||
− | + | End With | |
− | + | With mn2 = New Menu(menu, False) As "Menu" | |
.Caption = "Esci" | .Caption = "Esci" | ||
.Name = "Esci" | .Name = "Esci" | ||
− | + | End With | |
− | + | End | |
− | |||
− | |||
===Esempio con applicazione ''a riga di comando''=== | ===Esempio con applicazione ''a riga di comando''=== | ||
− | Library "libvlc:5.6. | + | Library "libvlc:5.6.1" |
Private Enum libvlc_NothingSpecial = 0, | Private Enum libvlc_NothingSpecial = 0, | ||
− | + | libvlc_Opening, | |
− | + | libvlc_Buffering, | |
− | + | libvlc_Playing, | |
− | + | libvlc_Paused, | |
− | + | libvlc_Stopped, | |
− | + | libvlc_Ended, | |
− | + | libvlc_Error | |
<FONT color=gray>' ''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)'' | ||
Riga 605: | Riga 792: | ||
− | + | Public Sub Main() | |
Dim inst, m, mp As Pointer | Dim inst, m, mp As Pointer | ||
<FONT color=gray>' ''Inizializza la libreria VLC:''</font> | <FONT color=gray>' ''Inizializza la libreria VLC:''</font> | ||
− | + | inst = libvlc_new(0, Null) | |
<FONT color=gray>' ''Crea un nuovo oggetto multimedia:''</font> | <FONT color=gray>' ''Crea un nuovo oggetto multimedia:''</font> | ||
− | + | m = libvlc_media_new_path(inst, "<FONT color=darkgreen>''/percorso/del/file/audio''</font>") | |
<FONT color=gray>' ''Crea un media player:''</font> | <FONT color=gray>' ''Crea un media player:''</font> | ||
− | + | mp = libvlc_media_player_new_from_media(m) | |
− | + | ||
+ | libvlc_media_release(m) | ||
+ | |||
<FONT color=gray>' ''Avvia l'esecuzione del file audio da parte del media player:''</font> | <FONT color=gray>' ''Avvia l'esecuzione del file audio da parte del media player:''</font> | ||
− | + | libvlc_media_player_play(mp) | |
+ | |||
+ | While libvlc_media_player_get_state(mp) < libvlc_Stopped | ||
+ | Write "\r" & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) | ||
+ | White 0.01 | ||
+ | Wend | ||
+ | |||
+ | <FONT color=gray>' ''Va in chiusura:''</font> | ||
+ | libvlc_media_player_release(mp) | ||
+ | libvlc_release(inst) | ||
+ | |||
+ | End | ||
+ | Se si vuole consentire l'arresto arbitrario dell'esecuzione del file audio, allora si dovrà rendere "Globale" la variabile ''Puntatore'', chiamata "mp", e la parte finale del codice dovrà essere così modificata e integrata: | ||
+ | ...... | ||
+ | |||
+ | While (libvlc_media_player_get_state(mp) <> libvlc_Stopped) And (libvlc_media_player_get_state(mp) <> libvlc_Ended) | ||
+ | Write "\r \e[31m" & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) | ||
+ | Wait 0.01 | ||
+ | Wend | ||
+ | |||
+ | <FONT color=gray>' ''Chiude il media player, la libreria VLC e il programma:''</font> | ||
+ | libvlc_media_player_release(mp) | ||
+ | libvlc_release(inst) | ||
+ | <FONT color=gray>' ''Avendo usato l'Evento "Application_Read()", è necessario usare l'istruzione "Quit" per chiudere il programma:''</font> | ||
+ | Quit | ||
+ | |||
+ | End | ||
− | |||
− | |||
− | |||
+ | Public Sub Application_Read() | ||
− | <FONT color=gray>' '' | + | <FONT color=gray>' ''Arresta l'esecuzione del file audio:''</font> |
− | + | libvlc_media_player_stop(mp) | |
− | |||
− | |||
− | + | End | |
− | |||
Versione attuale delle 20:33, 15 ago 2024
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 condivisa "libvlc.so.5.6.1 ".
Indice
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.1" 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) ' int libvlc_audio_set_volume(libvlc_media_player_t *p_mi, int i_volume) ' Set current software audio volume. Private Extern libvlc_audio_set_volume(p_mi As Pointer, i_volume As Integer) 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) ' void 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() With Slider1 .MaxValue = 100 .MinValue = 0 .Value = 50 End With End 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) ' Chiude il descrittore dell'oggetto media: libvlc_media_release(m) ' Imposta il valore iniziale del volume dell'audio in base al valore iniziale dello "Slider": libvlc_audio_set_volume(mp, Slider1.Value) ' Avvia l'esecuzione del file audio da parte del media player: libvlc_media_player_play(mp) Repeat Wait 0.01 Until libvlc_media_player_get_length(mp, 0) > 0.0 Repeat TextLabel1.Text = "Durata: " & Str(Time(0, 0, 0, libvlc_media_player_get_length(mp, 0))) TextLabel2.Text = "<FONT Color=red>" & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) ProgressBar1.Value = libvlc_media_player_get_time(mp) / libvlc_media_player_get_length(mp, 0) Wait 0.01 Until libvlc_media_player_get_state(mp) > libvlc_Paused ' Rilascia e chiude il media player: libvlc_media_player_release(mp) ' Chiude la libreria VLC: libvlc_release(inst) End Public Sub Button2_Click() ' Arresta l'esecuzione del file audio: libvlc_media_player_stop(mp) 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 Slider1_Change() ' Modifica il volume audio: If mp > 0 Then libvlc_audio_set_volume(mp, Slider1.Value) End
Spostare l'esecuzione del file audio nell'ambito della sua durata
Si può avanzare o retrocedere all'interno dell'esecuzione di un file audio mediante la funzione esterna "libvlc_media_player_set_time()".
Innanzitutto si dichiarerà formalmente la funzione esterna "libvlc_media_player_set_time()" di libvlc:
' void libvlc_media_player_set_time (libvlc_media_player_t *p_mi, libvlc_time_t i_time) ' Set the movie time (in ms). Private Extern libvlc_media_player_set_time(player As Pointer, i_time As Long)
Volendo usare uno Slider, imposteremo i valori iniziali dello Slider, dedicato allo spostamento dell'esecuzione del file audio, nella routine:
Public Sub Form_Open() ...... With Slider2 .MaxValue = 1000 .MinValue = 0 .Value = 0 End With End
Inoltre imposteremo una specifica routine per lo Slider dedicato allo spostamento dell'esecuzione del file audio:
Public Sub Slider2_Change() ' Sposta l'esecuzione del file audio nell'ambito della sua durata: libvlc_media_player_set_time(mp, (Slider2.Value * libvlc_media_player_get_length(mp, 0)) / 1000) End
Far variare la posizione del cursore dello Slider in base all'esecuzione
Se si preferisce far variare la posizione del cursore dello Slider in base al punto, all'evolversi dell'esecuzione del file audio, e poter arbitrariamente far avanzare e retrocedere il punto dell'esecuzione muovendo il cursore dello Slider, allora potrà adottarsi un codice come il seguente:
Private inst As Pointer Private mp As Pointer Private m As Pointer Private bo As Boolean Library "libvlc:5.6.1" 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 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 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 Integer ' 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_set_time (libvlc_media_player_t *p_mi, libvlc_time_t i_time) ' Set the movie time (in ms). Private Extern libvlc_media_player_set_time(player As Pointer, i_time 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) ' void 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() With Slider1 .MaxValue = 100 .MinValue = 0 .Value = 0 End With End Public Sub Button1_Click() Slider1.Value = 0 inst = libvlc_new(0, Null) m = libvlc_media_new_path(inst, "/percorso/del/file/audio") mp = libvlc_media_player_new_from_media(m) libvlc_media_player_play(mp) Repeat Wait 0.01 Until libvlc_media_player_get_length(mp, 0) > 0.0 Repeat Me.Title = "Durata: " & Str(Time(0, 0, 0, libvlc_media_player_get_length(mp, 0))) & " - " & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) ProgressBar1.Value = libvlc_media_player_get_time(mp) / libvlc_media_player_get_length(mp, 0) If Not bo Then Slider1.Value = ProgressBar1.Value * 100 Wait 0.01 Until libvlc_media_player_get_state(mp) > libvlc_Paused Chiude() End Public Sub Button2_Click() libvlc_media_player_stop(mp) DrawingArea1.Clear End Private Procedure Chiude() libvlc_media_player_release(mp) libvlc_media_release(m) libvlc_release(inst) End Public Sub ToggleButton1_Click() libvlc_media_player_pause(mp) End Public Sub Slider1_MouseDown() bo = True End Public Sub Slider1_MouseUp() bo = False libvlc_media_player_set_time(mp, (Slider1.Value * libvlc_media_player_get_length(mp, 0)) / 100) End
Impostare anche effetti video
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 Library "libvlc:5.6.1" 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 m As Pointer 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) ' Chiude il descrittore dell'oggetto media: libvlc_media_release(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(Time(0, 0, 0, libvlc_media_player_get_length(mp, 0))) TextLabel2.Text = "<FONT Color=red>" & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) Wait 0.01 Until libvlc_media_player_get_state(mp) > libvlc_Paused ' Rilascia e chiude il media player: libvlc_media_player_release(mp) ' Chiude la libreria VLC: libvlc_release(inst) End Public Sub Button2_Click() ' Arresta l'esecuzione del file audio: libvlc_media_player_stop(mp) 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: è 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.1" 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) ' Chiude l'oggetto media: libvlc_media_release(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(Time(0, 0, 0, libvlc_media_player_get_length(mp, 0))) tl2.Text = "<FONT Color=red>" & Str(Time(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) 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.1" 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) libvlc_media_release(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 "\r" & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) White 0.01 Wend ' Va in chiusura: libvlc_media_player_release(mp) libvlc_release(inst) End
Se si vuole consentire l'arresto arbitrario dell'esecuzione del file audio, allora si dovrà rendere "Globale" la variabile Puntatore, chiamata "mp", e la parte finale del codice dovrà essere così modificata e integrata:
...... While (libvlc_media_player_get_state(mp) <> libvlc_Stopped) And (libvlc_media_player_get_state(mp) <> libvlc_Ended) Write "\r \e[31m" & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) Wait 0.01 Wend ' Chiude il media player, la libreria VLC e il programma: libvlc_media_player_release(mp) libvlc_release(inst) ' Avendo usato l'Evento "Application_Read()", è necessario usare l'istruzione "Quit" per chiudere il programma: Quit End Public Sub Application_Read() ' Arresta l'esecuzione del file audio: libvlc_media_player_stop(mp) End