Question How to call "mciSendString" in a thread

Neyja

New member
Joined
Feb 3, 2014
Messages
4
Programming Experience
Beginner
Hello all,
I use "mciSendString" function to play audio file , it works great in a button
VB.NET:
Public Class Form1
    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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        play()
    End Sub

    Sub play()
        Dim musicAlias As String = "myAudio"
        Dim musicPath As String = "Call.mp3"
        mciSendString("Open " & Chr(34) & musicPath & Chr(34) & " alias " & musicAlias, CStr(0), 0, 0)
        mciSendString("play " & musicAlias, CStr(0), 0, 0)
    End Sub

End Class

But i need to use it in a thread like this:

VB.NET:
Public Class Form1
    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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim essai As New Threading.Thread(AddressOf play)
        essai.IsBackground = True
        essai.Start()
    End Sub

    Sub play()
        Dim musicAlias As String = "myAudio"
        Dim musicPath As String =  "Call.mp3"
        mciSendString("Open " & Chr(34) & musicPath & Chr(34) & " alias " & musicAlias, CStr(0), 0, 0)
        mciSendString("play " & musicAlias, CStr(0), 0, 0)
    End Sub



End Class

My problem is that it doesnt work in a thread , so if someone can explain me how to use it in a thread, it iwll be great.
Thank you.
 
mciSendString is not thread-safe. It also need to be tied to a winform handle. Instead you should use the MediaPlayer class:

    Public Sub Test()
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf PlaySound), "D:\Music\Black Sabbath\Best of Black Sabbath\02. The Wizard.mp3")
        Thread.Sleep(5000)
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf PlaySound), "D:\Music\Black Sabbath\Best of Black Sabbath\14. Children of the Grave.mp3")
    End Sub

    Public Sub PlaySound(ByVal filename As String)
        Dim mplayer As New MediaPlayer
        AddHandler mplayer.MediaEnded, AddressOf MediaEndedHandler
        mplayer.Open(New Uri(filename))
        mplayer.Play()
    End Sub

    Private Sub MediaEndedHandler(sender As Object, e As EventArgs)
        DirectCast(sender, MediaPlayer).Close()
    End Sub
 
Last edited:
@Herman I dont want to use "Mediaplayer" because i forgot to specify that i dont want to have a ".dll" file
and i dont know an other method to play a mp3 audio.
 
I'm not sure what you mean by "i don't want to have a DLL". You won't. Just add a reference to PresentationCore and WindowsBase to your project, and an Imports to System.Windows.Media. There will be no DLL in the output when you build.
 
Back
Top