2013-02-26 4 views
0

나는 이것에 대해 궁금해했다. 나는 여러 사이트에서 얻은 여러 제안을 시도했다. 여기에 내 코드가 있지만 작동하지 않습니다.VB.Net 동시에 mp3 재생

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를 재생할 수 있습니까?

답변

0

다른 문제를 직접 처리하고 있지만 검색 결과에서이 문제가 발생하여 두 파일을 동시에 재생할 수없는 이유를 알 수 있습니다. 별칭이 두 가지 모두 동일하기 때문입니다.

+0

별칭을 변경하려고했지만 "winmm.dll"이 인식하지 못하는 것 같습니다. 어쨌든이 방법을 사용하지 않고 대신 2 개의 Windows 미디어 플레이어 COM을 사용했습니다. 잘 작동하는 – Ikong

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