Differenze tra le versioni di "Eseguire un file audio mediante le funzioni esterne del API di Mpv"
Da Gambas-it.org - Wikipedia.
Riga 4: | Riga 4: | ||
Mostriamo un semplice esempio: | Mostriamo un semplice esempio: | ||
+ | Library "libc:6" | ||
+ | |||
+ | Private Const LC_NUMERIC As Integer = 1 | ||
+ | |||
+ | <FONT Color=gray>' ''char *setlocale (int __category, const char *__locale)'' | ||
+ | ' ''Set and/or return the current locale.''</font> | ||
+ | Private Extern setlocale(__category As Integer, __locale As String) As Pointer | ||
+ | |||
+ | |||
Library "libmpv:1.107.0" | Library "libmpv:1.107.0" | ||
Riga 33: | Riga 42: | ||
' ''Disconnect and destroy the mpv_handle.''</font> | ' ''Disconnect and destroy the mpv_handle.''</font> | ||
Private Extern mpv_terminate_destroy(ctx As Pointer) | Private Extern mpv_terminate_destroy(ctx As Pointer) | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Versione delle 17:12, 24 mag 2021
La libreria esterna Libmpv consente di eseguire diversi formati di file audio.
Per poter utilizzare le risorse di Mpv, è necessario avere installata e richiamare in Gambas la libreria condivisa: "libmpv.so.1.107.0"
Mostriamo un semplice esempio:
Library "libc:6" Private Const LC_NUMERIC As Integer = 1 ' char *setlocale (int __category, const char *__locale) ' Set and/or return the current locale. Private Extern setlocale(__category As Integer, __locale As String) As Pointer Library "libmpv:1.107.0" Private Enum MPV_FORMAT_NONE = 0, MPV_FORMAT_STRING, MPV_FORMAT_OSD_STRING, MPV_FORMAT_FLAG, MPV_FORMAT_INT64, MPV_FORMAT_DOUBLE, MPV_FORMAT_NODE, MPV_FORMAT_NODE_ARRAY, MPV_FORMAT_NODE_MAP, MPV_FORMAT_BYTE_ARRAY ' mpv_handle *mpv_create(void) ' Create a new mpv instance. Private Extern mpv_create() As Pointer ' int mpv_initialize(mpv_handle *ctx) ' Initialize an uninitialized mpv instance. Private Extern mpv_initialize(ctx As Pointer) As Integer ' int mpv_command(mpv_handle *ctx, const char **args) ' Send a command to the player. Private Extern mpv_command(ctx As Pointer, args As String[]) As Integer ' int mpv_set_property(mpv_handle *ctx, const char *name, mpv_format format, void *data) ' Set a property. Private Extern mpv_set_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer ' int mpv_get_property(mpv_handle *ctx, const char *name, mpv_format format, void *data) ' Read the value of the given property. Private Extern mpv_get_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) ' void mpv_terminate_destroy(mpv_handle *ctx) ' Disconnect and destroy the mpv_handle. Private Extern mpv_terminate_destroy(ctx As Pointer) Public Sub Main() Dim mpv As Pointer Dim fileaudio As String Dim d, l, v As Long fileaudio = "/percorso/del/file/audio" Print "File audio: "; fileaudio Print setlocale(LC_NUMERIC, "C") mpv = mpv_create() If mpv == 0 Then Error.Raise("Errore !") mpv_initialize(mpv) mpv_command(mpv, ["loadfile", fileaudio, Null]) ' Imposta il volume. 0 significa silenzio, 100 significa nessuna riduzione del volume o amplificazione. ' Valori superiori a 100 amplificano il volume di uscita. v = 50 mpv_set_property(mpv, "volume", MPV_FORMAT_INT64, VarPtr(v)) Repeat mpv_get_property(mpv, "duration", MPV_FORMAT_INT64, VarPtr(d)) Until d > 0 ' Si esce dal ciclo, solo quando sarà rilevata la durata del file audio Repeat mpv_get_property(mpv, "time-pos", MPV_FORMAT_INT64, VarPtr(l)) Write "\e[0m\rDurata: " & Date(0, 0, 0, 0, 0, 0, CInt(d) * 1000) & " Tempo trascorso: \e[31m" & Date(0, 0, 0, 0, 0, 0, CInt(l) * 1000) Wait 0.001 Until l >= d mpv_terminate_destroy(mpv) End
Un'altra soluzione simile, ma più essenziale:
Library "libc:6" Private Const LC_NUMERIC As Integer = 1 ' char *setlocale (int __category, const char *__locale) ' Set and/or return the current locale. Private Extern setlocale(__category As Integer, __locale As String) As Pointer Library "libmpv:1.107.0" Public Struct mpv_event event_id As Integer error_ As Integer reply_userdata As Long data As Pointer End Struct Private Const MPV_EVENT_END_FILE As Integer = 7 ' mpv_handle *mpv_create(void) ' Create a new mpv instance. Private Extern mpv_create() As Pointer ' int mpv_initialize(mpv_handle *ctx) ' Initialize an uninitialized mpv instance. Private Extern mpv_initialize(ctx As Pointer) As Integer ' int mpv_command(mpv_handle *ctx, const char **args) ' Send a command to the player. Private Extern mpv_command(ctx As Pointer, args As String[]) As Integer ' mpv_event *mpv_wait_event(mpv_handle *ctx, double timeout) ' Wait for the next event, or until the timeout expires. Private Extern mpv_wait_event(ctx As Pointer, timeout As Float) As Mpv_event ' void mpv_terminate_destroy(mpv_handle *ctx) ' Disconnect and destroy the mpv_handle. Private Extern mpv_terminate_destroy(ctx As Pointer) Public Sub Main() Dim mpv As Pointer Dim err As Integer Dim cmd As String[] = ["loadfile", Null, Null] Dim tm As Date Dim ev As Mpv_event cmd[1] = "/home/ploppo/Musica/Morales Pie Jesu (Requiem).mp3" setlocale(LC_NUMERIC, "C") mpv = mpv_create() If mpv == 0 Then Error.Raise("Error !") err = mpv_initialize(mpv) If err < 0 Then Terminus(mpv) Error.Raise("Error !") Endif err = mpv_command(mpv, cmd) If err < 0 Then Terminus(mpv) Error.Raise("Error !") Endif tm = Now Repeat Write "\r\e[31m" & Str(Time(0, 0, 0, DateDiff(tm, Now, gb.Millisecond))) Flush ev = mpv_wait_event(mpv, 1) Until ev.event_id == MPV_EVENT_END_FILE ' Il ciclo termina, quando termina l'esecuzione del file audio Terminus(mpv) End Private Procedure Terminus(po As Pointer) mpv_terminate_destroy(po) End