none
Separated Instances of My.Computer.Audio.Play in vb.net RRS feed

  • Question

  • Hi,

    Am trying to play / alert sounds from my application based on few flags. But When i call for stop it stops all the background voices. Is there a way to stop a instance of audio.play ?

    Below is my code.

                    If flag AndAlso mySettings.getASound() Then
                        My.Computer.Audio.Play(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\alert1.wav", AudioPlayMode.BackgroundLoop)
    
                    ElseIf flag2 AndAlso mySettings.getbSound() Then
                        My.Computer.Audio.Play(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\alert2.wav", AudioPlayMode.BackgroundLoop)
                    End If
    
                    If Not flag Then
                        My.Computer.Audio.Stop() 'this should only stop alert1
                    ElseIf Not flag2 Then
                        My.Computer.Audio.Stop() 'this should only stop alert2
                    End If

    Thank you in advance :)


    Coderv9

    Friday, September 27, 2019 2:33 PM

Answers

  • Here is an example. Each button shows how to start and stop a song independently. You can play the songs at the same time.

    The example makes the controls just copy and paste the code into an empty form. Change the form name as required. Change the paths to the two songs A and B that are on your system.

    Edit v2 add repeat.

    'v2 play multiple sounds with stop and repeat
    Imports System.Runtime.InteropServices
    
    Public Class Form4
        Private Snds As New MultiSoundsEx
        Private WithEvents ButtonA As New Button With {.Parent = Me, .Text = "Play A",
            .Location = New Point(20, 20)}
        Private WithEvents ButtonB As New Button With {.Parent = Me, .Text = "Play B",
            .Location = New Point(120, 20)}
        Private WithEvents RepeatCb As New CheckBox With {.Parent = Me, .Text = "Repeat",
            .Location = New Point(220, 20)}
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Text = "Play Multiple Sounds"
            Snds.AddSound("Note A", "C:\test sounds\w_hwy.wav")
            Snds.AddSound("Note B", "C:\test sounds\W_LL484.wav")
        End Sub
    
        Private Sub ButtonA_Click_1(sender As Object, e As EventArgs) Handles ButtonA.Click
            If ButtonA.Text = "Stop A" Then
                Snds.Stop("Note A")
                ButtonA.Text = "Play A"
            Else
                Snds.Play("Note A")
                ButtonA.Text = "Stop A"
            End If
    
        End Sub
    
        Private Sub ButtonB_Click(sender As Object, e As EventArgs) Handles ButtonB.Click
            If ButtonB.Text = "Stop B" Then
                Snds.Stop("Note B")
                ButtonB.Text = "Play B"
            Else
                Snds.Play("Note B")
                ButtonB.Text = "Stop B"
            End If
        End Sub
    
        Private Sub RepeatCb_CheckedChanged(sender As Object, e As EventArgs) Handles RepeatCb.CheckedChanged
            Snds.repeat = RepeatCb.Checked
        End Sub
    End Class
    
    
    Public Class MultiSoundsEx
        Private Snds As New Dictionary(Of String, String)
        Private sndcnt As Integer = 0
        Public repeat As Boolean
    
        <DllImport("winmm.dll", EntryPoint:="mciSendStringW")>
        Private Shared Function mciSendStringW(<MarshalAs(UnmanagedType.LPTStr)> ByVal lpszCommand As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszReturnString As System.Text.StringBuilder, ByVal cchReturn As UInteger, ByVal hwndCallback As IntPtr) As Integer
        End Function
    
        Public Function AddSound(ByVal SoundName As String, ByVal SndFilePath As String) As Boolean
            If SoundName.Trim = "" Or Not IO.File.Exists(SndFilePath) Then Return False
            'to repeat must use type mpegvideo ????
            If mciSendStringW("open " & Chr(34) & SndFilePath & Chr(34) & " type mpegvideo alias " & "Snd_" & sndcnt.ToString, Nothing, 0, IntPtr.Zero) <> 0 Then Return False
            'If mciSendStringW("open " & Chr(34) & SndFilePath & Chr(34) & " alias " & "Snd_" & sndcnt.ToString, Nothing, 0, IntPtr.Zero) <> 0 Then Return False
            Snds.Add(SoundName, "Snd_" & sndcnt.ToString)
            sndcnt += 1
            Return True
        End Function
    
        Public Function Play(ByVal SoundName As String) As Boolean
            If Not Snds.ContainsKey(SoundName) Then Return False
            mciSendStringW("seek " & Snds.Item(SoundName) & " to start", Nothing, 0, IntPtr.Zero)
    
            If repeat Then
                If mciSendStringW("play " & Snds.Item(SoundName) & " repeat", Nothing, 0, IntPtr.Zero) <> 0 Then Return False
            Else
                If mciSendStringW("play " & Snds.Item(SoundName), Nothing, 0, IntPtr.Zero) <> 0 Then Return False
            End If
    
            Return True
        End Function
    
        Public Function [Stop](ByVal SoundName As String) As Boolean
            If Not Snds.ContainsKey(SoundName) Then Return False
            If mciSendStringW("stop " & Snds.Item(SoundName), Nothing, 0, IntPtr.Zero) <> 0 Then Return False
            Return True
        End Function
    End Class
    


    Saturday, September 28, 2019 9:35 AM

All replies

  • You can use mciSendString to open, play, stop, several sounds with aliases

    (you can find many samples on those forums...)

    • Proposed as answer by tommytwotrain Saturday, September 28, 2019 9:35 AM
    Friday, September 27, 2019 3:20 PM
  • Here is an example. Each button shows how to start and stop a song independently. You can play the songs at the same time.

    The example makes the controls just copy and paste the code into an empty form. Change the form name as required. Change the paths to the two songs A and B that are on your system.

    Edit v2 add repeat.

    'v2 play multiple sounds with stop and repeat
    Imports System.Runtime.InteropServices
    
    Public Class Form4
        Private Snds As New MultiSoundsEx
        Private WithEvents ButtonA As New Button With {.Parent = Me, .Text = "Play A",
            .Location = New Point(20, 20)}
        Private WithEvents ButtonB As New Button With {.Parent = Me, .Text = "Play B",
            .Location = New Point(120, 20)}
        Private WithEvents RepeatCb As New CheckBox With {.Parent = Me, .Text = "Repeat",
            .Location = New Point(220, 20)}
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Text = "Play Multiple Sounds"
            Snds.AddSound("Note A", "C:\test sounds\w_hwy.wav")
            Snds.AddSound("Note B", "C:\test sounds\W_LL484.wav")
        End Sub
    
        Private Sub ButtonA_Click_1(sender As Object, e As EventArgs) Handles ButtonA.Click
            If ButtonA.Text = "Stop A" Then
                Snds.Stop("Note A")
                ButtonA.Text = "Play A"
            Else
                Snds.Play("Note A")
                ButtonA.Text = "Stop A"
            End If
    
        End Sub
    
        Private Sub ButtonB_Click(sender As Object, e As EventArgs) Handles ButtonB.Click
            If ButtonB.Text = "Stop B" Then
                Snds.Stop("Note B")
                ButtonB.Text = "Play B"
            Else
                Snds.Play("Note B")
                ButtonB.Text = "Stop B"
            End If
        End Sub
    
        Private Sub RepeatCb_CheckedChanged(sender As Object, e As EventArgs) Handles RepeatCb.CheckedChanged
            Snds.repeat = RepeatCb.Checked
        End Sub
    End Class
    
    
    Public Class MultiSoundsEx
        Private Snds As New Dictionary(Of String, String)
        Private sndcnt As Integer = 0
        Public repeat As Boolean
    
        <DllImport("winmm.dll", EntryPoint:="mciSendStringW")>
        Private Shared Function mciSendStringW(<MarshalAs(UnmanagedType.LPTStr)> ByVal lpszCommand As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszReturnString As System.Text.StringBuilder, ByVal cchReturn As UInteger, ByVal hwndCallback As IntPtr) As Integer
        End Function
    
        Public Function AddSound(ByVal SoundName As String, ByVal SndFilePath As String) As Boolean
            If SoundName.Trim = "" Or Not IO.File.Exists(SndFilePath) Then Return False
            'to repeat must use type mpegvideo ????
            If mciSendStringW("open " & Chr(34) & SndFilePath & Chr(34) & " type mpegvideo alias " & "Snd_" & sndcnt.ToString, Nothing, 0, IntPtr.Zero) <> 0 Then Return False
            'If mciSendStringW("open " & Chr(34) & SndFilePath & Chr(34) & " alias " & "Snd_" & sndcnt.ToString, Nothing, 0, IntPtr.Zero) <> 0 Then Return False
            Snds.Add(SoundName, "Snd_" & sndcnt.ToString)
            sndcnt += 1
            Return True
        End Function
    
        Public Function Play(ByVal SoundName As String) As Boolean
            If Not Snds.ContainsKey(SoundName) Then Return False
            mciSendStringW("seek " & Snds.Item(SoundName) & " to start", Nothing, 0, IntPtr.Zero)
    
            If repeat Then
                If mciSendStringW("play " & Snds.Item(SoundName) & " repeat", Nothing, 0, IntPtr.Zero) <> 0 Then Return False
            Else
                If mciSendStringW("play " & Snds.Item(SoundName), Nothing, 0, IntPtr.Zero) <> 0 Then Return False
            End If
    
            Return True
        End Function
    
        Public Function [Stop](ByVal SoundName As String) As Boolean
            If Not Snds.ContainsKey(SoundName) Then Return False
            If mciSendStringW("stop " & Snds.Item(SoundName), Nothing, 0, IntPtr.Zero) <> 0 Then Return False
            Return True
        End Function
    End Class
    


    Saturday, September 28, 2019 9:35 AM
  • Here is an example. Each button shows how to start and stop a song independently. You can play the songs at the same time.

    The example makes the controls just copy and paste the code into an empty form. Change the form name as required. Change the paths to the two songs A and B that are on your system.

    Imports System.Runtime.InteropServices
    
    Public Class Form4
        Private Snds As New MultiSoundsEx
        Private WithEvents ButtonA As New Button With {.Parent = Me, .Text = "Play A",
            .Location = New Point(20, 20)}
        Private WithEvents ButtonB As New Button With {.Parent = Me, .Text = "Play B",
            .Location = New Point(120, 20)}
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Text = "Play Multiple Sounds"
            Snds.AddSound("Note A", "C:\test sounds\w_hwy.wav")
            Snds.AddSound("Note B", "C:\test sounds\W_LL484.wav")
        End Sub
    
        Private Sub ButtonA_Click_1(sender As Object, e As EventArgs) Handles ButtonA.Click
            If ButtonA.Text = "Stop A" Then
                Snds.Stop("Note A")
                ButtonA.Text = "Play A"
            Else
                Snds.Play("Note A")
                ButtonA.Text = "Stop A"
            End If
    
        End Sub
    
        Private Sub ButtonB_Click(sender As Object, e As EventArgs) Handles ButtonB.Click
            If ButtonB.Text = "Stop B" Then
                Snds.Stop("Note B")
                ButtonB.Text = "Play B"
            Else
                Snds.Play("Note B")
                ButtonB.Text = "Stop B"
            End If
        End Sub
    End Class
    
    
    Public Class MultiSoundsEx
        Private Snds As New Dictionary(Of String, String)
        Private sndcnt As Integer = 0
    
        <DllImport("winmm.dll", EntryPoint:="mciSendStringW")>
        Private Shared Function mciSendStringW(<MarshalAs(UnmanagedType.LPTStr)> ByVal lpszCommand As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszReturnString As System.Text.StringBuilder, ByVal cchReturn As UInteger, ByVal hwndCallback As IntPtr) As Integer
        End Function
    
        Public Function AddSound(ByVal SoundName As String, ByVal SndFilePath As String) As Boolean
            If SoundName.Trim = "" Or Not IO.File.Exists(SndFilePath) Then Return False
            If mciSendStringW("open " & Chr(34) & SndFilePath & Chr(34) & " alias " & "Snd_" & sndcnt.ToString, Nothing, 0, IntPtr.Zero) <> 0 Then Return False
            Snds.Add(SoundName, "Snd_" & sndcnt.ToString)
            sndcnt += 1
            Return True
        End Function
    
        Public Function Play(ByVal SoundName As String) As Boolean
            If Not Snds.ContainsKey(SoundName) Then Return False
            mciSendStringW("seek " & Snds.Item(SoundName) & " to start", Nothing, 0, IntPtr.Zero)
            If mciSendStringW("play " & Snds.Item(SoundName), Nothing, 0, IntPtr.Zero) <> 0 Then Return False
            Return True
        End Function
    
        Public Function [Stop](ByVal SoundName As String) As Boolean
            If Not Snds.ContainsKey(SoundName) Then Return False
            If mciSendStringW("stop " & Snds.Item(SoundName), Nothing, 0, IntPtr.Zero) <> 0 Then Return False
            Return True
        End Function
    End Class

    Thank you. This is so close. Is there any possibility to play the audio in loop till the stop button is pressed ? As the wav file will be a short voice.

    Coderv9

    Saturday, September 28, 2019 3:45 PM
  • Coder,

    Did I not just show you how to do repeat or did I dejavu that?

    I have updated the example v2 above with repeat option checkbox.

    Saturday, September 28, 2019 5:01 PM
  • Thank's a lot Tommy.... The updated code works great..


    Coderv9

    Sunday, September 29, 2019 9:12 PM