Differenze tra le versioni di "Eseguire un file mp3 usando l'API di Smpeg"

Da Gambas-it.org - Wikipedia.
Riga 12: Riga 12:
 
  Library "libsmpeg-0.4:0.1.4"
 
  Library "libsmpeg-0.4:0.1.4"
 
   
 
   
  <FONT color=gray>' ''Possible MPEG status codes''</font>
+
  <FONT color=gray>' ''Possible MPEG status codes:''</font>
 
  Private Enum SMPEG_ERROR = -1, SMPEG_STOPPED, SMPEG_PLAYING
 
  Private Enum SMPEG_ERROR = -1, SMPEG_STOPPED, SMPEG_PLAYING
 
   
 
   
Riga 20: Riga 20:
 
   
 
   
 
  <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(mp As Pointer, enable As Integer)
 
  Private Extern SMPEG_enableaudio(mp 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(mp As Pointer, vol As Integer)
 
  Private Extern SMPEG_setvolume(mp 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(mp As Pointer)
 
  Private Extern SMPEG_play(mp 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(mp As Pointer)
 
  Private Extern SMPEG_stop(mp 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(mp As Pointer)
 
  Private Extern SMPEG_pause(mp 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(mp As Pointer) As Integer
 
  Private Extern SMPEG_status(mp 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(smpeg As Pointer, infoP As Pointer)
 
   
 
   
 
  <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(mp As Pointer)
 
  Private Extern SMPEG_delete(mp As Pointer)
 
    
 
    
Riga 55: Riga 59:
 
   info = Alloc(160)
 
   info = Alloc(160)
 
    
 
    
   smpeg = SMPEG_new("''/percorso/del/file.mp3''", info, audio)
+
   smpeg = SMPEG_new("<FONT color=gray>''/percorso/del/file.mp3''</font>", info, audio)
 
    
 
    
 
   volume = 100
 
   volume = 100
Riga 65: Riga 69:
 
    
 
    
 
   While (pausa = True) Or (SMPEG_status(smpeg) = SMPEG_PLAYING)
 
   While (pausa = True) Or (SMPEG_status(smpeg) = SMPEG_PLAYING)
 +
    SMPEG_getinfo(smpeg, info)
 +
    Write #File.Out, Date(0, 0, 0, 0, 0, 0, Float@(info + 128) * 1000) & "\r"
 
     Wait 0.01
 
     Wait 0.01
 
   Wend
 
   Wend

Versione delle 11:44, 26 gen 2016

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): 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.
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

' SMPEG_getinfo( SMPEG* mpeg, SMPEG_Info* info )
' Get current information about an SMPEG object.
Private Extern SMPEG_getinfo(smpeg As Pointer, infoP As Pointer)

' 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
 
  info = Alloc(160)
 
  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 #File.Out, Date(0, 0, 0, 0, 0, 0, Float@(info + 128) * 1000) & "\r"
    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