Play wavs in VB2005.

  • Thread starter Thread starter ZikO
  • Start date Start date
Z

ZikO

Hi.

I'm struggle with playing a wav in Visual Basic. First I wanted to use
My.Computer.Audio.Play() method but I realized it's not working if I want to
have music in background.

I found in internet SoundPlayer Class but unfortunately I can't find it in
my Visual Studio Application. Whenever I try to use it

Dim Player New SoundPlayer ...

VS 2005 doesn't find it.

What's wrong ? Please help me. Thnaks

Regards.
 
If you search for SoundPlayer in the Object Browser (View / ObjectBrowser),
you'll find that it lives in the System.Media.Soundplayer namespace.

The ObjectBrowser is handy for figuring out what you need references to.

Robin S.
 
RobinS said:
If you search for SoundPlayer in the Object Browser (View /
ObjectBrowser), you'll find that it lives in the
System.Media.Soundplayer namespace. The ObjectBrowser is handy for
figuring out what you need references to. Robin S.
"ZikO" <[email protected]> wrote in message news:[email protected]...

What would be the reason I don't see it in Object Browser? I can't See
System.Media.SoundPlayer, I can't even see System.Media ?!

Was it something wrong during instalation?

Regards.
 
ZikO said:
What would be the reason I don't see it in Object Browser? I can't See
System.Media.SoundPlayer, I can't even see System.Media ?!

Was it something wrong during instalation?

Maybe your project is lacking references to the library containing the
class. Look up the DLL name in the documentation on the 'SoundPlayer' class
and add a reference via "Project" -> "Add reference...".
 
Herfried K. Wagner said:
Maybe your project is lacking references to the library containing the
class. Look up the DLL name in the documentation on the 'SoundPlayer'
class and add a reference via "Project" -> "Add reference...".

I found it in Add reference ... window, I've added as System reference but
it still can't recognize that it exist, I still can't use it. This is a very
new area for me and probably Im doing something wrong. Can someone explain
it to me by step by step, please? Thanks so much.
 
Try adding

Imports System.Media.Soundplayer

to the top of your class, before the Class definition.

Or go back to the References for your project (double-click
on My Project in Solution Explorer and choose the References
tab). In the bottom of the window, you see "Imported namespaces".
Find System.Media.Soundplay and check the checkbox.
(You have to click on it twice; there's a bug in VS2005).
This will import it into your entire project.

Robin S
 
Try adding
Imports System.Media.Soundplayer
to the top of your class, before the Class definition.
Or go back to the References for your project (double-click
on My Project in Solution Explorer and choose the References
tab). In the bottom of the window, you see "Imported namespaces".
Find System.Media.Soundplay and check the checkbox.
(You have to click on it twice; there's a bug in VS2005).
This will import it into your entire project.
Robin S
-----------------

In Imported namespaces I can see only System.Media, I can't see
SoundPlay!Maybe it's because System.Media are namespaces whilst SoundPlay is
a class. Anyway, even when I import System.Media by double-click I can't see
difference. The tick has appeared besides System.Media but in code window,
editor still can't see SoundPlay class. It's really odd.
 
You're right, it's the System.Media namespace.

If you type in Dim Player as System.Media.Soundplayer
does the Intellisense kick in after Media. and show
Soundplayer?

Open the object browser (View / ObjectBrowser).
Search for "soundplayer". Do you see it?

Here's a link to an article on msdn about how to use it;
this has an example in VB. Is there something about how
you're using it that is incorrect?

http://msdn2.microsoft.com/en-us/library/system.media.soundplayer.aspx

What did you mean about not being able to use
My.Computer.Audio.Play() because it doesn't play music
in the background? Do you mean you want to have it playing
while you're doing other stuff, and if you click off of the
program, it stops playing?

Robin S.
---------------------------------------
 
I found it in Video Tutorials about VB.NET 2005 SoundPlayer class
http://msdn.microsoft.com/vstudio/express/media/en/visualcsharp/AudioPart1CS.wvx

There was one missing line:

' Friend WithEvents MyPlayer As New System.Media.SoundPlayer

(of course I could use Imports System.Media ...) After this I could work
with SoundPlayer class e.g.

' myPlayer.SoundLocation = "/..."
' myPlayer.Play()

Without this line SoundPlayer couldn't be recognized by VB.
Open the object browser (View / ObjectBrowser).
Search for "soundplayer". Do you see it?
Yes.

Here's a link to an article on msdn about how to use it;
this has an example in VB. Is there something about how
you're using it that is incorrect?
http://msdn2.microsoft.com/en-us/library/system.media.soundplayer.aspx

I just couldn't use it anyhow because VB didn't recognized it but now it
works but that line above is not clear for me :/. Anyway it works now.
What did you mean about not being able to use
My.Computer.Audio.Play() because it doesn't play music

I mean that I want to play a few sounds at the same time, say song in the
background and some short sounds during clicking e.g. When I used
My.Computer.Audio.Play() and I use sound for clicking, the song stops
playing, as sound for clicking is supposed to play. Anyway SoundPlayer work
very similarly. I think I need to use Windows Media Player, although I
wanted to avoid that.

Thanks so much for giving me a hand :). I have done it with your help :).
Regards.
 
Back
Top