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)

このコードは最初の曲のみを再生し、2番目の曲は再生しません...

上記と同様の名前の異なる別の関数を作成することを考えていますが、それでも運がありません。

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

この方法は、開発中にのみうまく機能しましたが、mcisendstring を介して open コマンドを発行すると、インストールしたほとんどのコンピューターがクラッシュしました。理由がわかりません。これが私のコードです。多分それは誰かを助けるかもしれませんし、誰かが私が間違っていることを理解できるかもしれません. 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 に答える
0

私自身別の問題に取り組んでいますが、検索でこれに出くわし、2 つのファイルを同時に再生できない理由は、両方のエイリアスが同じであるためだと言えます。

于 2013-03-11T17:10:32.660 に答える