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

Da Gambas-it.org - Wikipedia.
 
(8 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.
+
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''
 
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'':
+
==Esecuzione di file audio mp3 con un'applicazione grafica==
Private Const audio As Integer = 1
+
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 "libsmpeg-0.4:0.1.4"
 
   
 
   
  Library "libsmpeg-0.4:0.1.4"
+
  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
+
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, infoP As Pointer, sdl_audio As Integer) As Pointer
+
  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>''</font>
+
  ' ''Enable or disable audio playback in MPEG stream.''</font>
  Private Extern SMPEG_enableaudio(mp As Pointer, enable As Integer)
+
  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(mp As Pointer, vol As Integer)
+
  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(mp As Pointer)
+
  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(mp As Pointer)
+
  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(mp As Pointer)
+
  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(mp As Pointer) As Integer
+
  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(mp As Pointer)
+
  Private Extern SMPEG_delete(mpeg As Pointer)
 
    
 
    
 
   
 
   
 
  Public Sub Button1_Click()
 
  Public Sub Button1_Click()
 
   
 
   
   Dim info As Pointer
+
   Dim info As New SMPEG_Info
 
   Dim volume As Byte
 
   Dim volume As Byte
 
+
   
   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 63: Riga 81:
 
    
 
    
 
   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, "Tempo trascorso: " & Time(0, 0, 0, info.current_time * 1000) & "\r"
 
     Wait 0.01
 
     Wait 0.01
 
   Wend
 
   Wend
Riga 69: Riga 89:
 
   SMPEG_delete(smpeg)
 
   SMPEG_delete(smpeg)
 
   
 
   
  End
+
  '''End'''
 
   
 
   
 
   
 
   
Riga 90: Riga 110:
 
     SMPEG_play(smpeg)
 
     SMPEG_play(smpeg)
 
   Endif
 
   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 "libsmpeg-0.4:0.1.4"
 +
 +
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=gray>''/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 #File.Out, "Tempo trascorso: " & Time(0, 0, 0, info.current_time * 1000) & "\r"
 +
  Wend
 +
 +
<FONT color=gray>' ''Va in chiusura:''</font>
 +
  SMPEG_delete(smpeg)
 
   
 
   
 
  '''End'''
 
  '''End'''

Versione attuale delle 10:15, 14 giu 2021

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


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 "libsmpeg-0.4:0.1.4"

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 #File.Out, "Tempo trascorso: " & Time(0, 0, 0, info.current_time * 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


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 "libsmpeg-0.4:0.1.4"

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 #File.Out, "Tempo trascorso: " & Time(0, 0, 0, info.current_time * 1000) & "\r"
  Wend

' Va in chiusura:
  SMPEG_delete(smpeg)

End