以下是vb.net中播放mp3的方法,整理了网上不少方法,挑出了一个相对实用简单的方法,大家可以尝试下。
声明部分:
Declare Function FlashWindow Lib "user32" (ByVal hwnd As Integer, ByVal bInvert As Boolean) As Integer
' 宣告 API
Private Declare Function mciSendStringA Lib "winmm.dll" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
以下是
Private Function PlayMidiFile(ByVal MusicFile As String) As Boolean
If System.IO.File.Exists(MusicFile) Then
mciSendStringA("stop music", "", 0, 0)
mciSendStringA("close music", "", 0, 0)
'mciSendStringA("open " & MusicFile & " alias music", "", 0, 0)
mciSendStringA("open " & MusicFile & " alias music", "", 0, 0)
PlayMidiFile = mciSendStringA("play music", "", 0, 0) = 0
End If
Return Nothing
End Function
Private Function StopMidi() As Boolean
StopMidi = mciSendStringA("stop music", "", 0, 0) = 0
mciSendStringA("close music", "", 0, 0)
End Function
Private Function PauseMidi() As Boolean
Return mciSendStringA("pause music", "", 0, 0) = 0
End Function
Private Function ContinueMidi() As Boolean
Return mciSendStringA("play music", "", 0, 0) = 0
End Function
以下是调用方法:
PlayMidiFile(ptShortPath) ' 播放 MP3 音樂
其中ptShortPath是mp3文件目录地址,注意这个地址中不能包含空格,否则不能正常播放,获取文件目录的短地址可以看以下这篇文章