Differenze tra le versioni di "Eseguire un file mp3 usando l'API di Smpeg"
Da Gambas-it.org - Wikipedia.
(6 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 1: | Riga 1: | ||
La libreria di '''SMPEG''' consente di gestire file ''mpeg'' e si interfaccia con il livello ''DirectMedia Simple'' (SDL) per fornire una riproduzione multipiattaforma dei file MP3, utilizzando lo standard MPEG-1. | La libreria di '''SMPEG''' consente di gestire file ''mpeg'' e si interfaccia con il livello ''DirectMedia Simple'' (SDL) per fornire una riproduzione multipiattaforma dei file MP3, utilizzando lo standard MPEG-1. | ||
− | Per poter utilizzare le risorse della libreria ''libsmpeg'' è necessario richiamare nell'applicazione Gambas la libreria (nella sua attuale versione): '' | + | Per poter utilizzare le risorse della libreria ''libsmpeg'' è necessario richiamare nell'applicazione Gambas la libreria (nella sua attuale versione): "''libsmpeg2-2.0:0.0.0'' ". |
− | Mostriamo di seguito un semplice codice per eseguire un file audio ''mp3'': | + | ==Esecuzione di file audio mp3 con un'applicazione grafica== |
− | + | Mostriamo di seguito un semplice codice per eseguire un file audio ''mp3'' con un'applicazione grafica: | |
Private smpeg As Pointer | Private smpeg As Pointer | ||
Private pausa As Boolean | Private pausa As Boolean | ||
+ | Library "libsmpeg2-2.0:0.0.0" | ||
− | + | Public Struct SMPEG_Info | |
+ | has_audio As Integer | ||
+ | has_video As Integer | ||
+ | width As Integer | ||
+ | height As Integer | ||
+ | current_frame As Integer | ||
+ | current_fps As Float | ||
+ | audio_string[80] As Byte | ||
+ | audio_current_frame As Integer | ||
+ | current_offset As Integer | ||
+ | total_size As Integer | ||
+ | current_time As Float | ||
+ | total_time As Float | ||
+ | End Struct | ||
− | <FONT color=gray>' ''Possible MPEG status codes''</font> | + | Private Const audio As Integer = 1 |
− | + | Private Enum SMPEG_ERROR = -1, SMPEG_STOPPED, SMPEG_PLAYING <FONT color=gray>' ''Possible MPEG status codes.''</font> | |
<FONT color=gray>' ''SMPEG* SMPEG_new(const char *file, SMPEG_Info* info, int sdl_audio)'' | <FONT color=gray>' ''SMPEG* SMPEG_new(const char *file, SMPEG_Info* info, int sdl_audio)'' | ||
' ''Create a new SMPEG object from an MPEG file. This function returns a new SMPEG object.''</font> | ' ''Create a new SMPEG object from an MPEG file. This function returns a new SMPEG object.''</font> | ||
− | Private Extern SMPEG_new(file$ As String, | + | Private Extern SMPEG_new(file$ As String, Info As SMPEG_Info, sdl_audio As Integer) As Pointer |
<FONT color=gray>' ''void SMPEG_enableaudio( SMPEG* mpeg, int enable )'' | <FONT color=gray>' ''void SMPEG_enableaudio( SMPEG* mpeg, int enable )'' | ||
− | ' ''Enable or disable audio playback in MPEG stream''</font> | + | ' ''Enable or disable audio playback in MPEG stream.''</font> |
− | Private Extern SMPEG_enableaudio( | + | Private Extern SMPEG_enableaudio(mpeg As Pointer, enable As Integer) |
<FONT color=gray>' ''void SMPEG_setvolume( SMPEG* mpeg, int volume )'' | <FONT color=gray>' ''void SMPEG_setvolume( SMPEG* mpeg, int volume )'' | ||
− | ' ''Set the audio volume of an MPEG stream, in the range 0-100''</font> | + | ' ''Set the audio volume of an MPEG stream, in the range 0-100.''</font> |
− | Private Extern SMPEG_setvolume( | + | Private Extern SMPEG_setvolume(mpeg As Pointer, vol As Integer) |
<FONT color=gray>' ''void SMPEG_play( SMPEG* mpeg )'' | <FONT color=gray>' ''void SMPEG_play( SMPEG* mpeg )'' | ||
− | ' ''Play an SMPEG object''</font> | + | ' ''Play an SMPEG object.''</font> |
− | Private Extern SMPEG_play( | + | Private Extern SMPEG_play(mpeg As Pointer) |
<FONT color=gray>' ''void SMPEG_stop( SMPEG* mpeg )'' | <FONT color=gray>' ''void SMPEG_stop( SMPEG* mpeg )'' | ||
− | ' ''Stop playback of an SMPEG object''</font> | + | ' ''Stop playback of an SMPEG object.''</font> |
− | Private Extern SMPEG_stop( | + | Private Extern SMPEG_stop(mpeg As Pointer) |
<FONT color=gray>' ''void SMPEG_pause( SMPEG* mpeg )'' | <FONT color=gray>' ''void SMPEG_pause( SMPEG* mpeg )'' | ||
− | ' ''Pause/Resume playback of an SMPEG object''</font> | + | ' ''Pause/Resume playback of an SMPEG object.''</font> |
− | Private Extern SMPEG_pause( | + | Private Extern SMPEG_pause(mpeg As Pointer) |
<FONT color=gray>' ''SMPEGstatus SMPEG_status( SMPEG* mpeg )'' | <FONT color=gray>' ''SMPEGstatus SMPEG_status( SMPEG* mpeg )'' | ||
− | ' ''Get the current status of an SMPEG object''</font> | + | ' ''Get the current status of an SMPEG object.''</font> |
− | Private Extern SMPEG_status( | + | Private Extern SMPEG_status(mpeg As Pointer) As Integer |
+ | |||
+ | <FONT color=gray>' ''SMPEG_getinfo( SMPEG* mpeg, SMPEG_Info* info )'' | ||
+ | ' ''Get current information about an SMPEG object.''</font> | ||
+ | Private Extern SMPEG_getinfo(mpeg As Pointer, Info As SMPEG_Info) | ||
<FONT color=gray>' ''void SMPEG_delete( SMPEG* mpeg )'' | <FONT color=gray>' ''void SMPEG_delete( SMPEG* mpeg )'' | ||
− | ' ''Delete an SMPEG object''</font> | + | ' ''Delete an SMPEG object.''</font> |
− | Private Extern SMPEG_delete( | + | Private Extern SMPEG_delete(mpeg As Pointer) |
Public Sub Button1_Click() | Public Sub Button1_Click() | ||
− | Dim info As | + | Dim info As New SMPEG_Info |
Dim volume As Byte | Dim volume As Byte | ||
− | + | smpeg = SMPEG_new("<FONT color=darkgreen>''/percorso/del/file.mp3''</font>", info, audio) | |
− | + | volume = 100 | |
− | + | SMPEG_setvolume(smpeg, volume) | |
− | + | SMPEG_enableaudio(smpeg, audio) | |
− | + | SMPEG_play(smpeg) | |
− | + | While (pausa = True) Or (SMPEG_status(smpeg) = SMPEG_PLAYING) | |
− | + | SMPEG_getinfo(smpeg, info) | |
− | + | Write "\rTempo trascorso: " & Str(Time(0, 0, 0, info.current_time * 1000)) | |
+ | Wait 0.01 | ||
+ | Wend | ||
<FONT color=gray>' ''Va in chiusura:''</font> | <FONT color=gray>' ''Va in chiusura:''</font> | ||
− | + | SMPEG_delete(smpeg) | |
− | + | End | |
− | + | Public Sub Button2_Click() <FONT color=gray>' ''Arresta l'esecuzione:''</font> | |
SMPEG_stop(smpeg) | SMPEG_stop(smpeg) | ||
− | + | End | |
− | + | Public Sub ToggleButton1_Click() | |
If Last.Value Then | If Last.Value Then | ||
Riga 91: | Riga 111: | ||
Endif | Endif | ||
− | '''End''' | + | End |
+ | |||
+ | |||
+ | |||
+ | ==Esecuzione di file audio mp3 con un'applicazione ''a riga di comando''== | ||
+ | Mostriamo di seguito un semplice codice per eseguire un file audio ''mp3'' con un'applicazione ''a riga di comando'': | ||
+ | Library "libsmpeg2-2.0:0.0.0" | ||
+ | |||
+ | Public Struct SMPEG_Info | ||
+ | has_audio As Integer | ||
+ | has_video As Integer | ||
+ | width As Integer | ||
+ | height As Integer | ||
+ | current_frame As Integer | ||
+ | current_fps As Float | ||
+ | audio_string[80] As Byte | ||
+ | audio_current_frame As Integer | ||
+ | current_offset As Integer | ||
+ | total_size As Integer | ||
+ | current_time As Float | ||
+ | total_time As Float | ||
+ | End Struct | ||
+ | |||
+ | Private Const audio As Integer = 1 | ||
+ | Private Enum SMPEG_ERROR = -1, SMPEG_STOPPED, SMPEG_PLAYING <FONT color=gray>' ''Possible MPEG status codes.''</font> | ||
+ | |||
+ | <FONT color=gray>' ''SMPEG* SMPEG_new(const char *file, SMPEG_Info* info, int sdl_audio)'' | ||
+ | ' ''Create a new SMPEG object from an MPEG file. This function returns a new SMPEG object.''</font> | ||
+ | Private Extern SMPEG_new(file$ As String, Info As SMPEG_Info, sdl_audio As Integer) As Pointer | ||
+ | |||
+ | <FONT color=gray>' ''void SMPEG_enableaudio( SMPEG* mpeg, int enable )'' | ||
+ | ' ''Enable or disable audio playback in MPEG stream.''</font> | ||
+ | Private Extern SMPEG_enableaudio(mpeg As Pointer, enable As Integer) | ||
+ | |||
+ | <FONT color=gray>' ''void SMPEG_setvolume( SMPEG* mpeg, int volume )'' | ||
+ | ' ''Set the audio volume of an MPEG stream, in the range 0-100.''</font> | ||
+ | Private Extern SMPEG_setvolume(mpeg As Pointer, vol As Integer) | ||
+ | |||
+ | <FONT color=gray>' ''void SMPEG_play( SMPEG* mpeg )'' | ||
+ | ' ''Play an SMPEG object.''</font> | ||
+ | Private Extern SMPEG_play(mpeg As Pointer) | ||
+ | |||
+ | <FONT color=gray>' ''void SMPEG_stop( SMPEG* mpeg )'' | ||
+ | ' ''Stop playback of an SMPEG object.''</font> | ||
+ | Private Extern SMPEG_stop(mpeg As Pointer) | ||
+ | |||
+ | <FONT color=gray>' ''void SMPEG_pause( SMPEG* mpeg )'' | ||
+ | ' ''Pause/Resume playback of an SMPEG object.''</font> | ||
+ | Private Extern SMPEG_pause(mpeg As Pointer) | ||
+ | |||
+ | <FONT color=gray>' ''SMPEGstatus SMPEG_status( SMPEG* mpeg )'' | ||
+ | ' ''Get the current status of an SMPEG object.''</font> | ||
+ | Private Extern SMPEG_status(mpeg As Pointer) As Integer | ||
+ | |||
+ | <FONT color=gray>' ''SMPEG_getinfo( SMPEG* mpeg, SMPEG_Info* info )'' | ||
+ | ' ''Get current information about an SMPEG object.''</font> | ||
+ | Private Extern SMPEG_getinfo(mpeg As Pointer, Info As SMPEG_Info) | ||
+ | |||
+ | <FONT color=gray>' ''void SMPEG_delete( SMPEG* mpeg )'' | ||
+ | ' ''Delete an SMPEG object.''</font> | ||
+ | Private Extern SMPEG_delete(mpeg As Pointer) | ||
+ | |||
+ | |||
+ | Public Sub Main() | ||
+ | |||
+ | Dim info As New SMPEG_Info | ||
+ | Dim smpeg As Pointer | ||
+ | Dim volume As Byte | ||
+ | |||
+ | smpeg = SMPEG_new("<FONT color=darkgreen>''/percorso/del/file.mp3''</font>", info, audio) | ||
+ | |||
+ | volume = 100 | ||
+ | SMPEG_setvolume(smpeg, volume) | ||
+ | |||
+ | SMPEG_enableaudio(smpeg, audio) | ||
+ | |||
+ | SMPEG_play(smpeg) | ||
+ | |||
+ | While SMPEG_status(smpeg) = SMPEG_PLAYING | ||
+ | SMPEG_getinfo(smpeg, info) | ||
+ | Write "\rTempo trascorso: " & Str(Time(0, 0, 0, info.current_time * 1000)) | ||
+ | Wait 0.01 | ||
+ | Wend | ||
+ | |||
+ | <FONT color=gray>' ''Va in chiusura:''</font> | ||
+ | SMPEG_delete(smpeg) | ||
+ | |||
+ | End |
Versione attuale delle 05:26, 4 giu 2024
La libreria di SMPEG consente di gestire file mpeg e si interfaccia con il livello DirectMedia Simple (SDL) per fornire una riproduzione multipiattaforma dei file MP3, utilizzando lo standard MPEG-1.
Per poter utilizzare le risorse della libreria libsmpeg è necessario richiamare nell'applicazione Gambas la libreria (nella sua attuale versione): "libsmpeg2-2.0:0.0.0 ".
Esecuzione di file audio mp3 con un'applicazione grafica
Mostriamo di seguito un semplice codice per eseguire un file audio mp3 con un'applicazione grafica:
Private smpeg As Pointer Private pausa As Boolean Library "libsmpeg2-2.0:0.0.0" Public Struct SMPEG_Info has_audio As Integer has_video As Integer width As Integer height As Integer current_frame As Integer current_fps As Float audio_string[80] As Byte audio_current_frame As Integer current_offset As Integer total_size As Integer current_time As Float total_time As Float End Struct Private Const audio As Integer = 1 Private Enum SMPEG_ERROR = -1, SMPEG_STOPPED, SMPEG_PLAYING ' Possible MPEG status codes. ' SMPEG* SMPEG_new(const char *file, SMPEG_Info* info, int sdl_audio) ' Create a new SMPEG object from an MPEG file. This function returns a new SMPEG object. Private Extern SMPEG_new(file$ As String, Info As SMPEG_Info, sdl_audio As Integer) As Pointer ' void SMPEG_enableaudio( SMPEG* mpeg, int enable ) ' Enable or disable audio playback in MPEG stream. Private Extern SMPEG_enableaudio(mpeg As Pointer, enable As Integer) ' void SMPEG_setvolume( SMPEG* mpeg, int volume ) ' Set the audio volume of an MPEG stream, in the range 0-100. Private Extern SMPEG_setvolume(mpeg As Pointer, vol As Integer) ' void SMPEG_play( SMPEG* mpeg ) ' Play an SMPEG object. Private Extern SMPEG_play(mpeg As Pointer) ' void SMPEG_stop( SMPEG* mpeg ) ' Stop playback of an SMPEG object. Private Extern SMPEG_stop(mpeg As Pointer) ' void SMPEG_pause( SMPEG* mpeg ) ' Pause/Resume playback of an SMPEG object. Private Extern SMPEG_pause(mpeg As Pointer) ' SMPEGstatus SMPEG_status( SMPEG* mpeg ) ' Get the current status of an SMPEG object. Private Extern SMPEG_status(mpeg As Pointer) As Integer ' SMPEG_getinfo( SMPEG* mpeg, SMPEG_Info* info ) ' Get current information about an SMPEG object. Private Extern SMPEG_getinfo(mpeg As Pointer, Info As SMPEG_Info) ' void SMPEG_delete( SMPEG* mpeg ) ' Delete an SMPEG object. Private Extern SMPEG_delete(mpeg As Pointer) Public Sub Button1_Click() Dim info As New SMPEG_Info Dim volume As Byte smpeg = SMPEG_new("/percorso/del/file.mp3", info, audio) volume = 100 SMPEG_setvolume(smpeg, volume) SMPEG_enableaudio(smpeg, audio) SMPEG_play(smpeg) While (pausa = True) Or (SMPEG_status(smpeg) = SMPEG_PLAYING) SMPEG_getinfo(smpeg, info) Write "\rTempo trascorso: " & Str(Time(0, 0, 0, info.current_time * 1000)) Wait 0.01 Wend ' Va in chiusura: SMPEG_delete(smpeg) End Public Sub Button2_Click() ' Arresta l'esecuzione: SMPEG_stop(smpeg) End Public Sub ToggleButton1_Click() If Last.Value Then ' Mette in pausa l'esecuzione: SMPEG_pause(smpeg) pausa = True Else ' Riprende l'esecuzione: pausa = False SMPEG_play(smpeg) Endif End
Esecuzione di file audio mp3 con un'applicazione a riga di comando
Mostriamo di seguito un semplice codice per eseguire un file audio mp3 con un'applicazione a riga di comando:
Library "libsmpeg2-2.0:0.0.0" Public Struct SMPEG_Info has_audio As Integer has_video As Integer width As Integer height As Integer current_frame As Integer current_fps As Float audio_string[80] As Byte audio_current_frame As Integer current_offset As Integer total_size As Integer current_time As Float total_time As Float End Struct Private Const audio As Integer = 1 Private Enum SMPEG_ERROR = -1, SMPEG_STOPPED, SMPEG_PLAYING ' Possible MPEG status codes. ' SMPEG* SMPEG_new(const char *file, SMPEG_Info* info, int sdl_audio) ' Create a new SMPEG object from an MPEG file. This function returns a new SMPEG object. Private Extern SMPEG_new(file$ As String, Info As SMPEG_Info, sdl_audio As Integer) As Pointer ' void SMPEG_enableaudio( SMPEG* mpeg, int enable ) ' Enable or disable audio playback in MPEG stream. Private Extern SMPEG_enableaudio(mpeg As Pointer, enable As Integer) ' void SMPEG_setvolume( SMPEG* mpeg, int volume ) ' Set the audio volume of an MPEG stream, in the range 0-100. Private Extern SMPEG_setvolume(mpeg As Pointer, vol As Integer) ' void SMPEG_play( SMPEG* mpeg ) ' Play an SMPEG object. Private Extern SMPEG_play(mpeg As Pointer) ' void SMPEG_stop( SMPEG* mpeg ) ' Stop playback of an SMPEG object. Private Extern SMPEG_stop(mpeg As Pointer) ' void SMPEG_pause( SMPEG* mpeg ) ' Pause/Resume playback of an SMPEG object. Private Extern SMPEG_pause(mpeg As Pointer) ' SMPEGstatus SMPEG_status( SMPEG* mpeg ) ' Get the current status of an SMPEG object. Private Extern SMPEG_status(mpeg As Pointer) As Integer ' SMPEG_getinfo( SMPEG* mpeg, SMPEG_Info* info ) ' Get current information about an SMPEG object. Private Extern SMPEG_getinfo(mpeg As Pointer, Info As SMPEG_Info) ' void SMPEG_delete( SMPEG* mpeg ) ' Delete an SMPEG object. Private Extern SMPEG_delete(mpeg As Pointer) Public Sub Main() Dim info As New SMPEG_Info Dim smpeg As Pointer Dim volume As Byte smpeg = SMPEG_new("/percorso/del/file.mp3", info, audio) volume = 100 SMPEG_setvolume(smpeg, volume) SMPEG_enableaudio(smpeg, audio) SMPEG_play(smpeg) While SMPEG_status(smpeg) = SMPEG_PLAYING SMPEG_getinfo(smpeg, info) Write "\rTempo trascorso: " & Str(Time(0, 0, 0, info.current_time * 1000)) Wait 0.01 Wend ' Va in chiusura: SMPEG_delete(smpeg) End