Eseguire un file mp3 usando l'API di Smpeg

Da Gambas-it.org - Wikipedia.

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 Enum DISABILITA = 0, ABILITA
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 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, ABILITA)
 
 volume = 100
 SMPEG_setvolume(smpeg, volume)
 
 SMPEG_enableaudio(smpeg, ABILITA)
 
 SMPEG_play(smpeg)
 
 While (pausa = True) Or (SMPEG_status(smpeg) = SMPEG_PLAYING)
   SMPEG_getinfo(smpeg, info)
   Me.Title = "Tempo 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:

Private Enum DISABILITA = 0, ABILITA


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 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, ABILITA)
 
 volume = 100
 SMPEG_setvolume(smpeg, volume)
 
 SMPEG_enableaudio(smpeg, ABILITA)
 
 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