2020年5月24日 星期日

VB.Net 如何播放 MP3 或 MIDI 音樂 (播放、停止、暫停) [Play MP3 with Control]


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    ' 宣告 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




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
    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


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
    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


沒有留言:

張貼留言