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.

Per poter utilizzare le risorse della libreria libsmpeg è necessario richiamare nell'applicazione Gambas la libreria (nella sua attuale versione): libsmpeg-0.4.so.0.1.4


Mostriamo di seguito un semplice codice per eseguire un file audio mp3:

Private Const audio As Integer = 1
Private smpeg As Pointer
Private pausa As Boolean


Library "libsmpeg-0.4:0.1.4"

' Possible MPEG status codes
Private Enum SMPEG_ERROR = -1, SMPEG_STOPPED, SMPEG_PLAYING

' 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, infoP As Pointer, sdl_audio As Integer) As Pointer

' void SMPEG_enableaudio( SMPEG* mpeg, int enable )
' Enable or disable audio playback in MPEG stream</font>
Private Extern SMPEG_enableaudio(mp 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(mp As Pointer, vol As Integer)

' void SMPEG_play( SMPEG* mpeg )
' Play an SMPEG object
Private Extern SMPEG_play(mp As Pointer)

' void SMPEG_stop( SMPEG* mpeg )
' Stop playback of an SMPEG object
Private Extern SMPEG_stop(mp As Pointer)

' void SMPEG_pause( SMPEG* mpeg )
' Pause/Resume playback of an SMPEG object
Private Extern SMPEG_pause(mp As Pointer)

' SMPEGstatus SMPEG_status( SMPEG* mpeg )
' Get the current status of an SMPEG object
Private Extern SMPEG_status(mp As Pointer) As Integer

' void SMPEG_delete( SMPEG* mpeg )
' Delete an SMPEG object
Private Extern SMPEG_delete(mp As Pointer)
 

Public Sub Button1_Click()

 Dim info 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 (pausa = True) Or (SMPEG_status(smpeg) = SMPEG_PLAYING)
    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