Can I play two sounds at once?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Can I play two sounds at once?

  1. #1
    Join Date
    Jul 2003
    Posts
    135

    Can I play two sounds at once?

    I have app that has a background sound and a sound when someone clicks something. But when they click the something it stops the background sound, which is supposed to loop. How can I play both sounds and leave the background looping? Heres a example of the code:

    Code:
        Private WheelTickSound As Media.SoundPlayer = New Media.SoundPlayer("Sounds\tick.wav")
        Private BackgroundSound As Media.SoundPlayer = New Media.SoundPlayer("Sounds\BackgroundMusic.wav")
    
        Private Sub GameBoard_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            WheelTickSound.Load()
            BackgroundSound.Load()
            BackgroundSound.PlayLooping()
        End Sub
    
        Private Sub SpinWheel()
            WheelTickSound.Play()
        End Sub
    So the form loads and loads the two wav files then starts playing the background clip in a loop. But when someone clicks on a button that executes the "SpinWheel" routine the WheelTick sound plays but stops the background music. How can I make it keep playing?

    -Allan.

  2. #2
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Can I play two sounds at once?

    u can play sound in different thread.
    Multithreading Sample

  3. #3
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,891

    Arrow Re: Can I play two sounds at once?

    I'm 98% sure that the PlaySound API (which SoundPlayer.Play is wrapping) does not support this no matter how you do it. You will have to look at something like DirectX if you really want to do this.
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow threads, so if you have a secondary question, message me.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,091

    Re: Can I play two sounds at once?

    Here's an explanation of the PlaySound API:
    Code:
        Private Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name _
           As String, ByVal hmod As Integer, ByVal flags As Integer) As Integer
        ' name specifies the sound file when the SND_FILENAME flag is set.
        ' hmod specifies an executable file handle.
        ' hmod must be Nothing if the SND_RESOURCE flag is not set.
        ' flags specifies which flags are set. 
    
        ' The PlaySound documentation lists all valid flags.
        Private Const SND_SYNC = &H0          ' play synchronously
        Private Const SND_ASYNC = &H1         ' play asynchronously
        Private Const SND_FILENAME = &H20000  ' name is file name
        Private Const SND_RESOURCE = &H40004  ' name is resource name or atom

  5. #5
    Join Date
    Jul 2003
    Posts
    135

    Re: Can I play two sounds at once?

    Quote Originally Posted by aniskhan
    u can play sound in different thread.
    Multithreading Sample
    Tried using a background worker and it didn't help

    Quote Originally Posted by Craig Gemmill
    I'm 98% sure that the PlaySound API (which SoundPlayer.Play is wrapping) does not support this no matter how you do it. You will have to look at something like DirectX if you really want to do this.
    Yeah...its looking like this because I found that even a msgbox default chime sound interupts the background sound.


    This is supposed to be a very simple game that is jsut being written to demostrat some things in our office so I'm not looking to spend a lot of time on it. I'll probably just go without background music as its easier.

    -Allan.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center