Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Audio files

Status
Not open for further replies.

exodus300

Programmer
Mar 22, 2002
262
AU
I saw the post "Can VB do something on a sound?", and it inspired an idea for my trivia game (see my earlier posts). Thankx, Kryzsoccer!

So how can I open and play a .wav file from VB, in the simplest possible way?

Thanks for your help! [Thanks in advance|Hope I helped you]
Exodus300
World-Wide Alliance of Evil and Twisted People
[red]"Experimentation by Example is the best learning tool" - Exodus300[/red]
Code:
if (pop_tarts == 0)
{
   tantrum = 1;
}
[pc3]
 
Try this.

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Dim sndSound As String

Const SND_ALIAS = &H10000
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
Const SND_LOOP = &H8
Const SND_MEMORY = &H4
Const SND_NODEFAULT = &H2
Const SND_NOSTOP = &H10
Const SND_SYNC = &H0

Private Sub Form_Load()
sndSound = LoadSound(App.Path & "\sound.wav")
sndPlaySound sndSound, SND_MEMORY Or SND_ASYNC
End Sub
 
Compile error:
Sub or function not defined

(PlaySound is highlited) [Thanks in advance|Hope I helped you]
Exodus300
World-Wide Alliance of Evil and Twisted People
[red]"Experimentation by Example is the best learning tool" - Exodus300[/red]
Code:
if (pop_tarts == 0)
{
   tantrum = 1;
}
[pc3]
 
? [Thanks in advance|Hope I helped you]
Exodus300
World-Wide Alliance of Evil and Twisted People
[red]"Experimentation by Example is the best learning tool" - Exodus300[/red]
Code:
if (pop_tarts == 0)
{
   tantrum = 1;
}
[pc3]
 
?? Help please?? [Thanks in advance|Hope I helped you]
Exodus300
World-Wide Alliance of Evil and Twisted People
[red]"Experimentation by Example is the best learning tool" - Exodus300[/red]
Code:
if (pop_tarts == 0)
{
   tantrum = 1;
}
[pc3]
 

exodus300, look at phonesex's answer to your question. If you will notice they did not give you the declaration to PlaySound (another valid API if declared correctly). They gave you a declaration to sndPlaySound. So looking at their post and then you reply (PlaySound is highlited) I can surmise that you may have done something wrong. Once again I will point you to microsoft's msdn site where you can learn all you want about API's.

Good Luck
 
grr i fKKKed it up... LoadSound is highlited. sorry :) [Thanks in advance|Hope I helped you]
Exodus300
World-Wide Alliance of Evil and Twisted People
[red]"Experimentation by Example is the best learning tool" - Exodus300[/red]
Code:
if (pop_tarts == 0)
{
   tantrum = 1;
}
[pc3]
 
Sorry to keep bringing my posts to the top of ur lists, but I would really like to know how to fix my problem.

phonesex, where exactly should I put the code you mentioned? at the top of the code for the form i want it on, or in a code module? I tried in a code module (cos i'm using one anyway, so that was a logical place to put it). I wuold like to play sound on more thjan 1 form.. I noticed the private in the line Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
. does this need to be public for it to work from a code module/on multiple forms?

as i said, it stops with error message Compile error: Sub or function not defined, and LoadSound is highlited.

your help is much appreciated, guys :)
[Thanks in advance|Hope I helped you]
Exodus300
World-Wide Alliance of Evil and Twisted People
[red]"Experimentation by Example is the best learning tool" - Exodus300[/red]
Code:
if (pop_tarts == 0)
{
   tantrum = 1;
}
[pc3]
 

exodus300,

I have never heard of loadsound but the parameter you need for the sndPlaySound to work is the path to the wav file. So make...

sndSound = App.Path & "\wavfile.wav"

and it should work.

Good Luck

 
OK I changed these things, it runs but does not make any sound.

Code:
' In Module1.bas

Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Public sndSound As String

Public Const SND_ALIAS = &H10000
Public Const SND_ASYNC = &H1
Public Const SND_FILENAME = &H20000
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




' In frmCongrats.frm

Private Sub Form_Load()

    '... other code ...
    
    sndSound = App.Path & "\girl_yay.wav"
    sndPlaySound sndSound, SND_MEMORY Or SND_ASYNC
End Sub [Thanks in advance|Hope I helped you]
[b]Exodus300[/b]
[i]World-Wide Alliance of Evil and Twisted People[/i]
[red][i]"Experimentation by Example is the best learning tool" - Exodus300[/i][/red]
[code]if (pop_tarts == 0)
{
   tantrum = 1;
}
[pc3]
 

First lets add another API declaration to your module...
[tt]
Public Declare Function waveOutGetNumDevs Lib "winmm.dll" () As Long
[/tt]
Then lets add some error checking in your program and remove one of the constants to check to see if that may be it...

[tt]
sndSound = App.Path & "\girl_yay.wav"

If Dir(sndSound) <> &quot;&quot; And waveOutGetNumDevs > 0 Then
sndPlaySound sndSound, SND_ASYNC
End If
[/tt]

The api if you look it up will tell you the number of audio devices you have on your system, and if zero your program will not be able to play sounds.

Good Luck

 
Thank you. I got it to work by eliminating the SND_MEMORY flag from my original code. [Thanks in advance|Hope I helped you]
Exodus300
World-Wide Alliance of Evil and Twisted People
[red]&quot;Experimentation by Example is the best learning tool&quot; - Exodus300[/red]
Code:
if (pop_tarts == 0)
{
   tantrum = 1;
}
[pc3]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top