Results 1 to 9 of 9

Thread: Sound

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Sound

    Is there an easy way to play a sound using an if statement?

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Sound

    What kind of sound? An If statement "conditionally executes a group of statements, depending on the value of an expression".
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    25

    Re: Sound

    Did you mean by "easy" that you want to do as little programming as possible?
    You could write a reusable function call inside of any if statements for playing sound.


    Although I do not know how to play sound in VB6, I do know from working in other languages that most sounds that you want to play have to be streamed from a file.

    That is... you need to obtain a buffer (or array) that can hold X amount of bytes of sound data. You then read data from the sound file into this buffer, and play it.
    You would loop through the entire contents of the file until all the sound data is played.

    I warn you though, if you load the whole file in one big buffer, then you will consume much more RAM than you need to.


    I hope this helps in your search for whatever it is you are trying to find out. (If you spent some more time explaining thoroughly what issue you were having, it would benefit you by letting everyone know how we can help).

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Sound

    Hi,
    Thanks, I wasn't very clear!

    The function idea is exactly what i'm looking for.
    I want to be able to call the sound in whenever I want it.
    its a small file - a ringing bell to signal the end of a round.

    How would I go about putting this into a function?

    Thanks again,
    Klivingston

  5. #5
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Sound

    Your level of naiveté raises some red flags.

    VB6 is far from easy to come by today. Are you sure you are not using VB.Net instead? If so, you are in the wrong forum.

    Note that we don't help people using stolen copies of VB6 (e.g. "Portable Edition").

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Sound

    No, I am most certainly using VB6. Schools still teach using visual basic 6.

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Sound

    If you put the following code in a new Module in your project, you can call it each time you want to produce a 'sound':

    Code:
    Public Declare Function PlaySound _
           Lib "winmm.dll" Alias "PlaySoundA" _
           (ByVal lpszName As String, _
           ByVal hModule As Long, _
           ByVal dwFlags As Long) As Long
    
    Public Const SND_ASYNC = &H1
    Public Const SND_LOOP = &H8
    Public Const SND_MEMORY = &H4
    Public Const SND_NODEFAULT = &H2
    Public Const SND_NOSTOP = &H10
    Public Const SND_SYNC = &H0
    Public Const SND_ALIAS = &H10000
    Public Const SND_APPLICATION = &H80
    Public Const SND_ALIAS_ID = &H110000
    Public Const SND_FILENAME = &H20000
    Public Const SND_NOWAIT = &H2000
    Public Const SND_PURGE = &H40
    Public Const SND_RESOURCE = &H40004
    
    Public gbResults As Boolean
    You 'call' it by something like this:

    Code:
    gbResults = playsound(mySound, 0, SND_ASYNC)
    or

    Code:
     gbResults = playsound(mySound, 0, SND_SYNC)
    Where mySound is a string variable holding the path&fileName of the sound file.

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Sound

    You can also use the MMC (Multimedia control) if you prefer. Also, the code I sent plays wave (.wav) files. And the MMC can play .mp3 files. Not sure what other formats each play.....I use either, depending upon if I have .wav or .mp3 files, although, like I say, each may play different formats as well.

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Sound

    Play and Stop Sound Example
    Attached Files Attached Files


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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