Results 1 to 3 of 3

Thread: Playing 2 wav file at a same time. HELP!

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    2

    Exclamation Playing 2 wav file at a same time. HELP!

    I am now doing a project of a mini car game. during game play the background music will be playing.. when the car hit something it will play another sound but it play the hit sound and the background music stop. please help mi slove this... i have use multithread before still won't work.. below is the code i use to play wav file

    Declare Auto Function sndPlaySound Lib "WINMM.DLL" (ByVal FileName As String, ByVal Options As Int32) As Int32
    Const SND_SYNC = &H0 'synchronize playback - 'locks' app until sound finished
    Const SND_ASYNC = &H1 ' played async - app continues while sound playing
    Const SND_ASYNC2 = &H1 ' played async - app continues while sound playing
    Const SND_NODEFAULT = &H2 ' No default sound played if file not found
    Const SND_LOOP = &H8 ' 'loop the wave
    Const SND_NOSTOP = &H10 'don't stop current sound if one playing
    Const SND_NOWAIT = &H2000 'Do not wait if the sound driver is busy.
    Const SND_ALIAS = &H10000 ' Play a Windows sound (such as SystemStart, Asterisk, etc.).


    'Select Game Play Music
    Public Sub SelectGP()
    GP = Int(Rnd() * 8)
    GP = "GP" + GP + ".wav"
    sndPlaySound(GP.ToString, SND_ASYNC2 Or SND_LOOP)
    End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Playing 2 wav file at a same time. HELP!

    You can't play more than one sound at a time that way. You can make your background music a different format or you can delve into DirectX.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Playing 2 wav file at a same time. HELP!

    Well I decided to delve into DirectX, and came across a few hurdles. After that, however, it is fairly simple. In order to play a sound using DirectX, you must first have the DirectX SDK (that contains managed extensions), which includes the DLL files that you will need to reference inside of your project.

    If you are wanting just sound, you have to add a reference to "Microsoft.DirectX.AudioVideoPlayback", and "Microsoft.DirectX.DirectSound". These are both 1.1 assemblies for use in 2003, but tested in 2005 and it worked, however with a catch.

    There is a problem with a LoaderLock exception when using 2005, and a description and solution to the problem was found here. In short, you have to disable the loader lock MDA by going to :
    "Debug > Exceptions, Open the Managed Debugging Assistants tree node and uncheck Loader Lock. This setting is per solution so it will only affect this solution."
    Once that is done, you should not run into an exception anymore, and it will not affect the project anyway since it is only displayed while debugging, and will not occur in release mode. It is a known issue by Microsoft, but probably won't be fixed since everything is moving to the XNA framework for use with 2.0.

    Anyways, back to the code. As I said before, its simple once you get done with all that. Below plays two sounds (same file) with a 5 second delay between them. I just used a sample mp3 file (you can use wav or any other sound format that directx recognizes), and the same song played twice at the same time (one being 5 seconds behind the other). This is not meant to be the exact code you would use, just sample to show that it works...
    VB Code:
    1. Dim MySound1 As New Microsoft.DirectX.AudioVideoPlayback.Audio("c:\test.mp3")
    2.         Dim MySound2 As New Microsoft.DirectX.AudioVideoPlayback.Audio("c:\test.mp3")
    3.         MySound1.Play()
    4.         System.Threading.Thread.Sleep(5000)
    5.         MySound2.Play()
    6.         System.Threading.Thread.Sleep(5000)
    7.         MySound1.Stop()
    8.         MySound2.Stop()
    Last edited by gigemboy; Aug 17th, 2006 at 02:19 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width