2012年5月8日 星期二

vb.net 播放音樂


Public Class Form1

' 宣告 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 Sub Button1_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button1.Click
PlayMidiFile("C:\死了都要愛.mp3") ' 播放 MP3 音樂
'或
'PlayMidiFile("C:\頑皮豹.mid") ' 播放 MIDI 音樂
End Sub

Private Sub Button2_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button2.Click
StopMidi() ' 停止播放
End Sub

Private Sub Button3_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button3.Click
PauseMidi() ' 暫停播放
End Sub

Private Sub Button4_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button4.Click
ContinueMidi() ' 繼續播放
End Sub

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)
PlayMidiFile = mciSendStringA("play music", "", 0, 0) = 0
End If
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

End Class

沒有留言:

張貼留言