Important alert: (current site time 7/16/2013 1:50:44 AM EDT)
 

VB icon

[ 3 lines of codes ]

Email
Submitted on: 2/11/2005 9:01:16 AM
By: Power Of Anubis 
Level: Beginner
User Rating: By 12 Users
Compatibility: VB 6.0
Views: 38198
author picture
(About the author)
 
     Play a wave file with only 3 lines of codes! It works with .wav files 100%, with other extension I don't know. Try and then report.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: [ 3 lines of codes ]
' Description:Play a wave file with only 3 lines of codes!
It works with .wav files 100%, with other extension I don't know. Try and then report.
' By: Power Of Anubis
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=58843&lngWId=1'for details.'**************************************

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_NODEFAULT = &H2
Const SND_LOOP = &H8
Const SND_NOSTOP = &H10
'----------PLAY WAVE SOUND--------
Private Sub PlayWaveSound_Click()
 soundfile$ = "c:/TheCustomSoundIWant.wav"
 wFlags% = SND_ASYNC Or SND_NODEFAULT
 HaHa = sndPlaySound(soundfile$, wFlags%)
End Sub
'-------STOP WAVE SOUND-------
Private Sub StopTheSound_Click()
StopTheSoundNOW = sndPlaySound(soundfile$, wFlags%)
End Sub
'Replace "c:/TheCustomSoundIWant.wav" with your sound


Other 10 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
2/11/2005 9:57:32 AMLaVolpe

FYI: PSC is a fine place to see if "it" exists before spending a ton of time reinventing the wheel. Congrats for figuring it out, but a quick search on PSC returns dozens of posts with same code & one post shows a way to check if a sound card exists before executing the wav: txtCodeId=682&lngWId=1
(If this comment was disrespectful, please report it.)

 
2/11/2005 10:16:51 AMPower Of Anubis

Oh sorry, if really exists I can delete it?
(If this comment was disrespectful, please report it.)

 
2/11/2005 11:14:27 AMLaVolpe

That wasn't the point I was trying to make. To many of us try to hard to do it ourselves from scratch. I was only trying to suggest that we (me included) could use PSC as a resource more often. It its been done before, maybe looking at the samples will provoke a "I can do this differently or maybe better". Of course doing it from scratch beefs up our own learning skills too. Cheers.
(If this comment was disrespectful, please report it.)

 
2/12/2005 2:06:01 PMBramPelgrom

Actually, it's more then 3 lines of code..
5 globes from me
(If this comment was disrespectful, please report it.)

 
2/13/2005 2:57:52 AMRenato Ciuffo

WindowsMediaPlayer1.play 'there 1 line!
:) 5 form me.
(If this comment was disrespectful, please report it.)

 
2/14/2005 12:46:34 PM

very nice..
how i can stop this sound? :(

(If this comment was disrespectful, please report it.)

 
2/14/2005 4:42:36 PMPower Of Anubis

Well acctualy to stop is simple:

Private Sub StopTheSound_Click()
StopTheSoundNOW = sndPlaySound(soundfile$, wFlags%)

End Sub
(If this comment was disrespectful, please report it.)

 
2/14/2005 4:45:12 PMPower Of Anubis

Hope It will help you
(If this comment was disrespectful, please report it.)

 
4/24/2005 6:26:45 AM

st
(If this comment was disrespectful, please report it.)

 
6/6/2005 5:56:04 PM

is the % and $ really needed?
and doesent it work if i just write "sndplaysound soundfile$,
wFlags%" instead of "haha = sndplaysound (soundfile$,
wFlags%)" ??
(If this comment was disrespectful, please report it.)

 
6/20/2005 7:21:49 PMMiles Cloud

If I remember correctly, the % means that the variable is an integer, and the $ means that it's a string. By the way, you can also code that like so:
Call sndPlaySound("[File name here]", &H3)
If the HTML showed up in there, sorry, I'm still trying to figure out how to insert line breaks. Good job, though, it took me quite a while to figure this one out.
(If this comment was disrespectful, please report it.)

 
6/20/2005 7:22:43 PMMiles Cloud

(Whoops, it did show up.)
>_>;
(If this comment was disrespectful, please report it.)

 
2/2/2006 1:00:01 AMkyntiewlang jamu

i like the code ,hey thanks
(If this comment was disrespectful, please report it.)

 
2/5/2006 9:31:32 AMPredrag

IS best code
(If this comment was disrespectful, please report it.)

 
2/19/2006 2:47:52 AMashrit

send a zip file plz
(If this comment was disrespectful, please report it.)

 
6/16/2006 1:29:46 AM

okie, so heres the thing. i modified the code so it works as form_load, now how do i add the loop command so it loops the sound over and over?
(If this comment was disrespectful, please report it.)

 
7/11/2006 12:51:18 PMVignesh

the code is found in msdn itself
(If this comment was disrespectful, please report it.)

 
7/11/2007 4:05:58 PMJohn

you could inculde the stop and start in the module and yud only have to use StopTheSoundNOW() to stop
(If this comment was disrespectful, please report it.)

 
8/10/2008 2:50:25 AM

... this is not the best C&P
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.