0

I have a problem about a game. I want to play a background sound and another sound when you click a button. When this happens, the background sound stops and the 2nd sound starts. When the 2nd sound finishes, no sound will be played.

You can see my code below:

    Imports System.Media.SoundPlayer

Public Class GamePlaying
    Dim mainmenusndplayer As New System.Media.SoundPlayer
    Private Sub GamePlaying_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        MainMenu.Show()
        mainmenusndplayer.Stop()
    End Sub
    Private Sub btn_Fire_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Fire.Click
        btn_Fire.Enabled = False
        Dim shotsound As New System.Media.SoundPlayer
        shotsound.Stream = My.Resources.TankShoot
        shotsound.Play()
    End Sub
    Private Sub GamePlaying_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mainmenusndplayer.Stream = My.Resources.WoT_Roll_Out
        mainmenusndplayer.Load()
        mainmenusndplayer.PlayLooping()
    End Sub
End Class
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
Cristian
  • 3
  • 2
  • 7
  • I'm not very confidenent on the the audio piece, but it sounds like a case for threading -- a C# example I found here -- http://stackoverflow.com/questions/6240002/play-two-sounds-simultaneusly – InbetweenWeekends Apr 15 '15 at 17:19
  • possible duplicate of [Play multiple sounds using SoundPlayer](http://stackoverflow.com/questions/1285294/play-multiple-sounds-using-soundplayer) – jaket Apr 15 '15 at 18:00
  • This question already has an answer: http://stackoverflow.com/questions/1285294/play-multiple-sounds-using-soundplayer. Also, it's not a threading issue - SoundPlayer.Play is already Async. – jaket Apr 15 '15 at 18:01

2 Answers2

0

I had been struggling with the same issue.....

The short answer is you can't. The system will only permit one channel to the sound interface within any given process. As such, when you start a new sound, the previous one terminates.

There are a number of work-arounds which mostly use either Direct-X or Media-Player. However, I found neither of those solutions to be either easy or robust in that you are depending on the users machine to have the right things installed.

However, I found a quick and simple solution to the problem.

I added a special "hook" to my application so that when I run it with an appropriate command line argument, all it does is play a particular sound and quit. I then use the process object to start another instance of the application with the appropriate argument.. and hey presto.. multiple sounds.

If your application is configured to automatically be be a single instance, you need to disable that and replace it with a little code to detect another instance and quit.

Another alternative is to add a small exe to your application package that simply does the sound part.

I know it does not sound very "pure", but really, all you are doing is starting a process to play a sound in the background. It doesn't really matter if that process is a windows native function or one of your own making.

0

you can use winmm.dll with mciSendString :

Imports System.Text
Imports System.Runtime.InteropServices


<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

 Private Sub MyForm_Click(sender As Object, e As EventArgs) Handles Me.Click

     mciSendString("open " & Chr(34) & "C:\myFile1.wav" & Chr(34) " type waveaudio alias Sound1" & internalName, Nothing, 0, IntPtr.Zero)

     mciSendString("open " & Chr(34) & "C:\myFile2.wav" & Chr(34) " type waveaudio alias Sound2" & internalName, Nothing, 0, IntPtr.Zero)

     mciSendString("play Sound1", Nothing, 0, Me.Handle.ToInt64())
     mciSendString("play Sound2", Nothing, 0, Me.Handle.ToInt64())

     mciSendString("setaudio Sound1 volume to 1000", Nothing, 0, Me.Handle.ToInt64())
     mciSendString("setaudio Sound2 volume to 1000", Nothing, 0, Me.Handle.ToInt64())

End Sub

and to stop the sounds you have to :

  mciSendString("close Name1", Nothing, 0, Me.Handle.ToInt64())
  mciSendString("close Name2", Nothing, 0, Me.Handle.ToInt64())

Download winmm.dll at https://www.dll-files.com/winmm.dll.html

Ale865
  • 116
  • 9