Results 1 to 7 of 7

Thread: Random sounds/Embedding project files into self-containing exe

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Random sounds/Embedding project files into self-containing exe

    I know how to play a single sound, but I need code that will play any unset number of sounds at random intervals, and at random times. Also, I need to know how to embed these sounds and other project files into a self-containing exe program so I only have to distribute the exe file and not the external sounds, etc. Thanks.

  2. #2
    Member
    Join Date
    Oct 2006
    Location
    Australia
    Posts
    56

    Re: Random sounds/Embedding project files into self-containing exe

    if you add alot of sound files.
    your exe could be upto 500 mb big?

    you sure you want 2 do that
    my questions will be in vb6 or visual studios 2005

  3. #3
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Random sounds/Embedding project files into self-containing exe

    To embed sound files into your executable you'll need to store them in a resource file which is compiled with your application. Search these forums for resource and you should find many examples.

    Here's a link to a pretty good tutorial....
    http://www.thevbzone.com/l_res.htm

    Once you've got that part of it working, then you can concentrate on the random selection bit.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Random sounds/Embedding project files into self-containing exe

    One exe file without an installation in VB... Not something you really want to do...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Re: Random sounds/Embedding project files into self-containing exe

    OK got that to work, thanks. Now how do I randomly play sounds at different intervals or at certain times? Thanks.

  6. #6
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Random sounds/Embedding project files into self-containing exe

    Try something like this...
    Start a new project, put a timer on the form and set its Enabled property to False and paste the following code onto the form. This code assumes your resource file contains 6 wavs with ids numbered from 1 to 6.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const SND_MEMORY = &H4
    4. Private Const SND_NODEFAULT = &H2
    5. Private Const SND_SYNC = &H0
    6.  
    7. Private Declare Function PlaySound Lib "WINMM.DLL" Alias "PlaySoundA" ( _
    8.             ByRef Sound As Any, _
    9.             ByVal hLib As Long, _
    10.             ByVal lngFlag As Long) As Long
    11.  
    12. Private Const MinInterval As Long = 100     ' Minimum interval between sounds
    13. Private Const MaxInterval As Long = 10000   ' Maximum interval between sounds
    14.  
    15. Private Const NumWavResources As Long = 6   ' Set to the number of wavs in the resource file
    16.  
    17. Private Sub PlayRandomSound()
    18.     '
    19.     '   This assumes the wav resource ids are numbered from 1 to NumWavResources
    20.     '
    21.    Dim DataArray() As Byte
    22.    
    23.    DataArray = LoadResData(Int(Rnd * NumWavResources) + 1, "WAVE")
    24.    Call PlaySound(DataArray(0), 0, SND_MEMORY Or SND_NODEFAULT Or SND_SYNC)
    25.    
    26.    Erase DataArray
    27.  
    28. End Sub
    29.  
    30. Private Sub Form_Load()
    31.     '
    32.     '   Seed the random number generator
    33.     '
    34.     Randomize
    35.     '
    36.     '   Set the timer's initial interval to a random number
    37.     '   between MinInterval and MaxInterval.
    38.     '   The timer is initially disabled at design time
    39.     '
    40.     Timer1.Interval = CLng(Rnd * MaxInterval) + MinInterval + 1
    41.     Timer1.Enabled = True
    42.    
    43. End Sub
    44.  
    45. Private Sub Timer1_Timer()
    46.     '
    47.     '   Play a random sound when the timer fires
    48.     '
    49.     PlayRandomSound
    50.     '
    51.     '   Change the timer's interval
    52.     '
    53.     Timer1.Enabled = False
    54.     Timer1.Interval = CLng(Rnd * MaxInterval) + MinInterval + 1
    55.     Timer1.Enabled = True
    56.    
    57. End Sub
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Re: Random sounds/Embedding project files into self-containing exe

    Thanks guys so much! If you want to see why I needed this program, go download it at http://www.kelceycoe.com/software/Kelcey Coe.zip.

    WARNING: Not for anyone under 18!

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