0

我一直想知道这一点,我尝试了从不同站点获得的多个建议。我在这里有我的代码,但它不起作用。

Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal _
uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim fileName As String

    FileName = Chr(34) & (Button1.Text) & Chr(34)
    mciSendString("open " & FileName & " alias myDevice", Nothing, 0, 0)
    mciSendString("play myDevice", Nothing, 0, 0)


    FileName = Chr(34) & (Button2.Text) & Chr(34)
    mciSendString("open " & FileName & " alias myDevice", Nothing, 0, 0)
    mciSendString("play myDevice", Nothing, 0, 0)

此代码仅播放第一首歌曲,不会播放第二首歌曲...

我正在考虑创建另一个类似于上面的函数但名称不同但仍然没有运气的函数。

Private Declare Function mciSendString2 Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal _
uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

任何想法?或者是否可以同时播放多个 mp3?

4

2 回答 2

0

虽然我自己正在处理一个不同的问题,但我在搜索中遇到了这个问题,并且可以告诉你它无法同时播放 2 个文件的原因是因为你的别名对两者都是相同的。

于 2013-03-11T17:10:32.660 回答
0

这种方法只在开发过程中对我很有效,但是当我通过 mcisendstring 发出打开命令时,我安装的大多数计算机都会崩溃。我还没弄清楚为什么。这是我的代码。也许它会帮助某人,也许有人可以弄清楚我做错了什么。我在从 64 位开发机器上运行 32 位应用程序时遇到问题。

Imports System.Runtime.InteropServices
Imports System.Text

Public Class MediaPlayerClass
    <DllImport("winmm.dll")> _
    Private Shared Function mciSendString(ByVal command As String, ByVal buffer As StringBuilder, ByVal bufferSize As Integer, ByVal hwndCallback As IntPtr) As Integer
    End Function
<DllImport("winmm.dll")> _
Private Shared Function mciGetErrorString(errCode As Integer, ByVal errMsg As StringBuilder, bufferSize As Integer) As Integer
End Function

<DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Public Shared Function GetShortPathName(ByVal longPath As String, _
      <MarshalAs(UnmanagedType.LPTStr)> ByVal ShortPath As System.Text.StringBuilder, _
      <MarshalAs(Runtime.InteropServices.UnmanagedType.U4)> ByVal bufferSize As Integer) As Integer
End Function


Private _filename As String
Private _MediaAlias As String
Private _Length As TimeSpan
Private _err As Integer
Public Property PlaylistId As Integer = 0
Private _OriginalVolume As Integer = 1000

Function ShortPathName(ByVal Path As String) As String
    Dim sb As New System.Text.StringBuilder(1024)

    Dim tempVal As Integer = GetShortPathName(Path, sb, 1024)
    If tempVal <> 0 Then
        Dim Result As String = sb.ToString()
        Return Result
    Else
        Throw New Exception("Failed to return a short path")
    End If
End Function

Public Sub New(Filename As String, MediaAlias As String)
    _filename = ShortPathName(Filename)
    _MediaAlias = MediaAlias.Replace(" ", "_")
    '_Length = GetLength()

    Try
        My.Application.Log.WriteEntry("MediaPlayerClass.New - calling MCI OPEN")
       ' here is where it crashes
        _err = mciSendString("open """ & _filename & """ alias " & MediaAlias, Nothing, 0, 0)
    Catch ex As Exception
        MsgBox(ex.ToString & vbCrLf & GetLastErrorMessage())
    End Try

End Sub

Public Sub NewMP3(Filename As String)
    Me.StopIt()
    Me.CloseIt()
    _filename = Filename


    Try
        My.Application.Log.WriteEntry("MediaPlayerClass.NewMP3 - calling MCI OPEN ")
        _err = mciSendString("open """ & Filename & """ alias " & _MediaAlias, Nothing, 0, 0)
    Catch ex As Exception
        MsgBox(ex.ToString & vbCrLf & GetLastErrorMessage())
    End Try

End Sub

Public ReadOnly Property Length As TimeSpan
    Get
        Return _length
    End Get
End Property

Private Function GetLength() As TimeSpan
    Dim lengthBuf As New StringBuilder(32)

    Try
        My.Application.Log.WriteEntry("MediaPlayerClass.GetLength - calling MCI OPEN")
        _err = mciSendString("open """ & _filename & """ type waveaudio alias " & _MediaAlias, Nothing, 0, 0)
    Catch ex As Exception
        MsgBox(ex.ToString & vbCrLf & GetLastErrorMessage())
    End Try

    ' Get the duration of the music
    Try
        _err = mciSendString("status wave length", lengthBuf, lengthBuf.Capacity, 0)
    Catch ex As Exception
        MsgBox(ex.ToString & vbCrLf & GetLastErrorMessage())
    End Try

    'mciSendString("close wave", Nothing, 0, 0)
    Dim len As Integer = Integer.TryParse(lengthBuf.ToString, len)
    Dim ts As TimeSpan = TimeSpan.FromMilliseconds(len)

    Return ts
End Function

Public Function PlayIt(Optional WaitUntilFinishedPlaying As Boolean = False) As Integer
    Try
        My.Application.Log.WriteEntry("MediaPlayerClass.PlayIt - calling MCI PLAY")
        _err = mciSendString("play " & _MediaAlias, Nothing, 0, IntPtr.Zero)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

    While WaitUntilFinishedPlaying
        If IsPlaying() Then
            Threading.Thread.Sleep(250)
        Else
            Exit While
        End If
    End While

    Return _err
End Function

Public Function PauseIt() As Integer
    _err = mciSendString("pause " & _MediaAlias, Nothing, 0, IntPtr.Zero)
    Return _err
End Function

Public Function ResumeIt() As Integer
    _err = mciSendString("resume " & _MediaAlias, Nothing, 0, IntPtr.Zero)
    Return _err
End Function

Public Function StopIt() As Boolean
    _err = mciSendString("stop " & _MediaAlias, Nothing, 0, IntPtr.Zero)
    Return _err
End Function


Public Function CloseIt() As Boolean
    _err = mciSendString("close " & _MediaAlias, Nothing, 0, IntPtr.Zero)
    Return _err
End Function

Public Function IsPlaying() As Boolean
    Dim returnData As New StringBuilder(128)
    _err = mciSendString("status " & _MediaAlias & " mode", returnData, 128, IntPtr.Zero)
    Return (returnData.Length = 7 AndAlso returnData.ToString.Substring(0, 7) = "playing")
End Function

Public Function SetVolume(vol As Integer) As Integer
    _err = -1
    If vol >= 0 And vol <= 1000 Then
        _err = mciSendString("setaudio " & _MediaAlias & " volume to " & vol.ToString, Nothing, 0, IntPtr.Zero)
    End If
    Return _err
End Function

Public Sub FadeOutAndPause()
    _OriginalVolume = GetVolume()
    For x As Integer = 30 To 1 Step -1
        Me.SetVolume(Int(x / 30 * _OriginalVolume))
        Threading.Thread.Sleep(100)
    Next
    Me.PauseIt()
End Sub

Public Sub PlayAndFadeIn()
    Me.PlayIt()
    For x As Integer = 1 To 30 Step 1
        Me.SetVolume(Int(x / 30 * _OriginalVolume))
        Threading.Thread.Sleep(100)
    Next
End Sub

Public Function GetVolume() As Integer
    Dim returnData As New StringBuilder(128)
    _err = mciSendString("status " & _MediaAlias & " volume", returnData, 128, IntPtr.Zero)
    'MsgBox(returnData.ToString)
    If _err = 0 Then
        Return CInt(returnData.ToString)
    Else
        Return 1000
    End If

End Function

Public Function SetBalance(bal As Integer) As Integer
    If bal >= 0 AndAlso bal <= 1000 Then
        _err = mciSendString("setaudio " & _MediaAlias & " left volume to " + (1000 - bal).ToString, Nothing, 0, IntPtr.Zero)
        _err = mciSendString("setaudio " & _MediaAlias & " right volume to " + bal.ToString, Nothing, 0, IntPtr.Zero)
    End If
    Return _err
End Function

Public Function GetLastErrorMessage() As String
    Dim returnData As New StringBuilder(128)
    _err = mciGetErrorString(_err, returnData, 128)
    Return returnData.ToString.Trim

    End Function

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        CloseIt()
    End Sub
End Class
于 2013-05-18T13:16:00.040 回答