2013-02-16 111 views
0

我正在使用VB.net VS2012,並希望有一些幫助播放音頻文件。音頻文件類

這裏是我的代碼:

''' <summary> 
''' This class is a wrapper for the Windows API calls to play wave, midi or mp3 files. 
''' </summary> 
''' <remarks> 
''' </remarks> 
Public Class AudioFile 
'*********************************************************************************************************** 
'  Class: PlayFile 
' Written By: Blake Pell ([email protected]) 
' Initial Date: 03/31/2007 
' Last Updated: 02/04/2009 
'*********************************************************************************************************** 

' Windows API Declarations 
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Int32, ByVal hwndCallback As Int32) As Int32 

''' <summary> 
''' Constructor: Location is the filename of the media to play. Wave files and Mp3 files are the supported formats. 
''' </summary> 
''' <param name="Location"></param> 
''' <remarks></remarks> 
Public Sub New(ByVal location As String) 
    Me.Filename = location 
End Sub 

''' <summary> 
''' Plays the file that is specified as the filename. 
''' </summary> 
''' <remarks></remarks> 
Public Sub Play() 

    If _filename = "" Or Filename.Length <= 4 Then Exit Sub 

    Select Case Right(Filename, 3).ToLower 
     Case "mp3" 
      mciSendString("open """ & _filename & """ type mpegvideo alias audiofile", Nothing, 0, IntPtr.Zero) 

      Dim playCommand As String = "play audiofile from 0" 

      If _wait = True Then playCommand += " wait" 

      mciSendString(playCommand, Nothing, 0, IntPtr.Zero) 
     Case "wav" 
      mciSendString("open """ & _filename & """ type waveaudio alias audiofile", Nothing, 0, IntPtr.Zero) 
      mciSendString("play audiofile from 0", Nothing, 0, IntPtr.Zero) 
     Case "mid", "idi" 
      mciSendString("stop midi", "", 0, 0) 
      mciSendString("close midi", "", 0, 0) 
      mciSendString("open sequencer!" & _filename & " alias midi", "", 0, 0) 
      mciSendString("play midi", "", 0, 0) 
     Case Else 
      Throw New Exception("File type not supported.") 
      Call Close() 
    End Select 

    IsPaused = False 

End Sub 

''' <summary> 
''' Pause the current play back. 
''' </summary> 
''' <remarks></remarks> 
Public Sub Pause() 
    mciSendString("pause audiofile", Nothing, 0, IntPtr.Zero) 
    IsPaused = True 
End Sub 

''' <summary> 
''' Resume the current play back if it is currently paused. 
''' </summary> 
''' <remarks></remarks> 
Public Sub [Resume]() 
    mciSendString("resume audiofile", Nothing, 0, IntPtr.Zero) 
    IsPaused = False 
End Sub 

''' <summary> 
''' Stop the current file if it's playing. 
''' </summary> 
''' <remarks></remarks> 
Public Sub [Stop]() 
    mciSendString("stop audiofile", Nothing, 0, IntPtr.Zero) 
End Sub 

''' <summary> 
''' Close the file. 
''' </summary> 
''' <remarks></remarks> 
Public Sub Close() 
    mciSendString("close audiofile", Nothing, 0, IntPtr.Zero) 
End Sub 

Private _wait As Boolean = False 
''' <summary> 
''' Halt the program until the .wav file is done playing. Be careful, this will lock the entire program up until the 
''' file is done playing. It behaves as if the Windows Sleep API is called while the file is playing (and maybe it is, I don't 
''' actually know, I'm just theorizing). :P 
''' </summary> 
''' <value></value> 
''' <returns></returns> 
''' <remarks></remarks> 
Public Property Wait() As Boolean 
    Get 
     Return _wait 
    End Get 
    Set(ByVal value As Boolean) 
     _wait = value 
    End Set 
End Property 

''' <summary> 
''' Sets the audio file's time format via the mciSendString API. 
''' </summary> 
''' <value></value> 
''' <returns></returns> 
''' <remarks></remarks> 
ReadOnly Property Milleseconds() As Integer 
    Get 
     Dim buf As String = Space(255) 
     mciSendString("set audiofile time format milliseconds", Nothing, 0, IntPtr.Zero) 
     mciSendString("status audiofile length", buf, 255, IntPtr.Zero) 

     buf = Replace(buf, Chr(0), "") ' Get rid of the nulls, they muck things up 

     If buf = "" Then 
      Return 0 
     Else 
      Return CInt(buf) 
     End If 
    End Get 
End Property 

''' <summary> 
''' Gets the status of the current playback file via the mciSendString API. 
''' </summary> 
''' <value></value> 
''' <returns></returns> 
''' <remarks></remarks> 
ReadOnly Property Status() As String 
    Get 
     Dim buf As String = Space(255) 
     mciSendString("status audiofile mode", buf, 255, IntPtr.Zero) 
     buf = Replace(buf, Chr(0), "") ' Get rid of the nulls, they muck things up 
     Return buf 
    End Get 
End Property 

''' <summary> 
''' Gets the file size of the current audio file. 
''' </summary> 
''' <value></value> 
''' <returns></returns> 
''' <remarks></remarks> 
ReadOnly Property FileSize() As Integer 
    Get 
     Try 
      Return My.Computer.FileSystem.GetFileInfo(_filename).Length 
     Catch ex As Exception 
      Return 0 
     End Try 
    End Get 
End Property 

''' <summary> 
''' Gets the channels of the file via the mciSendString API. 
''' </summary> 
''' <value></value> 
''' <returns></returns> 
''' <remarks></remarks> 
ReadOnly Property Channels() As Integer 
    Get 
     Dim buf As String = Space(255) 
     mciSendString("status audiofile channels", buf, 255, IntPtr.Zero) 

     If IsNumeric(buf) = True Then 
      Return CInt(buf) 
     Else 
      Return -1 
     End If 
    End Get 
End Property 

''' <summary> 
''' Used for debugging purposes. 
''' </summary> 
''' <value></value> 
''' <returns></returns> 
''' <remarks></remarks> 
ReadOnly Property Debug() As String 
    Get 
     Dim buf As String = Space(255) 
     mciSendString("status audiofile channels", buf, 255, IntPtr.Zero) 

     Return Str(buf) 
    End Get 
End Property 

Private _isPaused As Boolean = False 
''' <summary> 
''' Whether or not the current playback is paused. 
''' </summary> 
''' <value></value> 
''' <returns></returns> 
''' <remarks></remarks> 
Public Property IsPaused() As Boolean 
    Get 
     Return _isPaused 
    End Get 
    Set(ByVal value As Boolean) 
     _isPaused = value 
    End Set 
End Property 

Private _filename As String 
''' <summary> 
''' The current filename of the file that is to be played back. 
''' </summary> 
''' <value></value> 
''' <returns></returns> 
''' <remarks></remarks> 
Public Property Filename() As String 
    Get 
     Return _filename 
    End Get 
    Set(ByVal value As String) 

     If My.Computer.FileSystem.FileExists(value) = False Then 
      Throw New System.IO.FileNotFoundException 
      Exit Property 
     End If 

     _filename = value 
    End Set 
End Property 
End Class 

此代碼工作得很好。我可以請一些幫助來創建一個將在音頻文件播放完畢時被調用的事件。當音頻文件完成時,'狀態'爲'已停止'。我該如何檢查以瞭解發生這種情況併爲其創建事件?

回答

0

回放狀態發生變化時,似乎沒有辦法註冊回調,因此您必須使用觀察者(即計時器)。

Private WithEvents StatusMonitor As New Timers.Timer(100) 
Private Property LastStatus As String 

Private Sub StatusMonitor_Elapsed(sender As Object, e As Timers.ElapsedEventArgs) Handles StatusMonitor.Elapsed 
    If Not String.Equals(Me.Status, Me.LastStatus) Then 
     Me.LastStatus = Me.Status 
     RaiseEvent PlaybackStatusChanged(Me, New PlaybackStatusChangedEventArgs(Me.Status)) 
    End If 
End Sub 

Public Event PlaybackStatusChanged(sender As Object, e As PlaybackStatusChangedEventArgs) 

Public Class PlaybackStatusChangedEventArgs 
    Inherits EventArgs 

    Private _status As String 

    Public Sub New(status As String) 
     _status = status 
    End Sub 

    Public ReadOnly Property Status As String 
     Get 
      Return _status 
     End Get 
    End Property 
End Class 

它的作用是將狀態存儲在私有屬性中,並將其與每100ms的當前狀態進行比較。如果狀態已更改,則會觸發PlaybackStatusChanged事件以及包含新狀態的參數。然後,您可以監聽此事件,並在事件回調中檢查e.Status,就好像您直接從AudioFile.Status獲取它一樣。

編輯

做了幾個測試運行後,我發現,狀態屬性的作用不一致。它似乎也返回了很多空白,因爲我似乎無法弄清楚。無論如何,因此,傳遞給狀態事件的狀態是錯誤的。

0

我不知道該怎麼做。但你可以做的是這個。在主窗體,昏暗的字符串,就像笑作爲字符串現在

一個按鈕,設置此代碼:

lol = "Audio file path goes here :3" 
    Dim audio As New AudioFile(lol) 
    Timer1.Start() 
    audio.Play() 

添加一個定時器,其時間間隔設置爲10

對於計時器code add this:

Dim audio As New AudioFile(lol) 
    If audio.Status.Contains("stopped") Then 
     audio.Play() 
    End If 

這樣,當歌曲完成播放時,定時器將自動開始再次播放。並且它的音頻文件位於該變量上,所以只需按下該按鈕即可設置該變量和繁榮的路徑。你的設定。

希望幫助...