none
mp3播放---vb6 to vb2005 RRS feed

  • 问题

  • 請問一下

    vb6播mp3的音樂可用 mciSendStringA

    轉到 VB2005..

    (1) 怎麼播 ?  

    (2) API用法一樣嗎?

    Thanks

     

     

     

     

    2008年6月24日 4:53

答案

  •  

    和vb里的方法类似

     

    Code Snippet

     

     


    Imports System.Runtime.InteropServices

    Public Class Form1
        Dim player As MP3Player

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            player = New MP3Player()
            player.Open("C:\sanbao.mp3") ' 播放
            player.Play(True)
        End Sub


        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            player.Close()
        End Sub


        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            player.SetVolume(100)
        End Sub
    End Class


    Public Class MP3Player

        Private _command As String
        Private isOpen As Boolean
        <DllImport("winmm.dll")> _
        Private Shared Function mciSendString(ByVal strCommand As String, ByVal strReturn As System.Text.StringBuilder, ByVal iReturnLength As Integer, ByVal hwndCallback As IntPtr) As Long
        End Function


        Public Sub New()
        End Sub
        Public Sub Close()
            _command = "close MediaFile"
            mciSendString(_command, Nothing, 0, IntPtr.Zero)
            isOpen = False
        End Sub

        Public Sub Open(ByVal sFileName As String)
            _command = "open """ + sFileName + """ type mpegvideo alias MediaFile"
            mciSendString(_command, Nothing, 0, IntPtr.Zero)
            isOpen = True
        End Sub

        Public Sub Play(ByVal [loop] As Boolean)
            If isOpen Then
                _command = "play MediaFile"
                If [loop] Then
                    _command += " REPEAT"
                End If
                mciSendString(_command, Nothing, 0, IntPtr.Zero)
            End If
        End Sub

        Public Function SetVolume(ByVal v As Long) As Boolean
            If isOpen Then
                _command = "setaudio  MediaFile volume to " + v.ToString
                SetVolume = mciSendString(_command, Nothing, 0, 0)
            End If
        End Function

    End Class

     

     

     

    2008年6月25日 0:25
    版主

全部回复

  • 也可以这样

     

    Code Snippet

     

    Public Class Form1

        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 sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            PlayMusic("C:\sanbao.mp3") ' 播放 
        End Sub


        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            StopMusic() 'stop
        End Sub


        Public Sub PlayMusic(ByVal FileName As String)
            mciSendStringA("open " & FileName & " alias mp3", "", 0, 0)
            mciSendStringA("play mp3", "", 0, 0)
        End Sub


        Public Function StopMusic() As Boolean
            Dim r As Long
            r = mciSendStringA("stop mp3", "", 0, 0)
            StopMusic = r = 0
            r = mciSendStringA("close mp3", "", 0, 0)
        End Function

    End Class

     

    2008年6月24日 6:13
    版主
  • 第三個做法跟VB 6.0一樣

    這應該是最簡單的方法吧 ?

    VB 2005 Express版本沒有API檢視員...

     

     

    如果用上述方法還要加做音量控制

    請問可用哪個API ?

    謝謝

     

     

    2008年6月24日 12:16
  •  

    和vb里的方法类似

     

    Code Snippet

     

     


    Imports System.Runtime.InteropServices

    Public Class Form1
        Dim player As MP3Player

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            player = New MP3Player()
            player.Open("C:\sanbao.mp3") ' 播放
            player.Play(True)
        End Sub


        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            player.Close()
        End Sub


        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            player.SetVolume(100)
        End Sub
    End Class


    Public Class MP3Player

        Private _command As String
        Private isOpen As Boolean
        <DllImport("winmm.dll")> _
        Private Shared Function mciSendString(ByVal strCommand As String, ByVal strReturn As System.Text.StringBuilder, ByVal iReturnLength As Integer, ByVal hwndCallback As IntPtr) As Long
        End Function


        Public Sub New()
        End Sub
        Public Sub Close()
            _command = "close MediaFile"
            mciSendString(_command, Nothing, 0, IntPtr.Zero)
            isOpen = False
        End Sub

        Public Sub Open(ByVal sFileName As String)
            _command = "open """ + sFileName + """ type mpegvideo alias MediaFile"
            mciSendString(_command, Nothing, 0, IntPtr.Zero)
            isOpen = True
        End Sub

        Public Sub Play(ByVal [loop] As Boolean)
            If isOpen Then
                _command = "play MediaFile"
                If [loop] Then
                    _command += " REPEAT"
                End If
                mciSendString(_command, Nothing, 0, IntPtr.Zero)
            End If
        End Sub

        Public Function SetVolume(ByVal v As Long) As Boolean
            If isOpen Then
                _command = "setaudio  MediaFile volume to " + v.ToString
                SetVolume = mciSendString(_command, Nothing, 0, 0)
            End If
        End Function

    End Class

     

     

     

    2008年6月25日 0:25
    版主