954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Background sound

i want to add bakground sound to my application that i am doing as a project for my class.
does anyone know the code for that?
:mrgreen:

tupadjia
Newbie Poster
11 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

If you are using vb express 2005 do this:

Dim done As Boolean = False

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If done Then Exit Sub
        done = True
        My.Computer.Audio.Play("c:\windows\media\Windows XP Logoff Sound.wav", AudioPlayMode.BackgroundLoop)
        MessageBox.Show("Now playing Windows XP Logoff Sound.wav")

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MessageBox.Show("button2 clicked - Sound will be turned off")
        My.Computer.Audio.Stop()
        done = False
    End Sub

vb 2002 or vb 2003 shouldn't be much different, however they do not use the my keyword.

waynespangler
Posting Pro in Training
461 posts since Dec 2002
Reputation Points: 84
Solved Threads: 58
 

thanks for the code! i highly appreciate that! i tried and its working, but i also tried with mp3's, but its not working, is there like a code where when someone us using my software they can choose which song they can listen? provided i uploaded several songs for them to listen to.
thanks in advance!

tupadjia
Newbie Poster
11 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

I think that is good only for avi and wav sound files. You will have to use windows media player for others. Here is a link that should help.
http://www.codeproject.com/useritems/Play_sound_in_VBnet_with.asp

waynespangler
Posting Pro in Training
461 posts since Dec 2002
Reputation Points: 84
Solved Threads: 58
 

How Can We Add A Sound Background Witout Buttons??

I Mean When The First Form Is Opened The Sound Starts With It At The Same Time??

**UM HASSAN**
Newbie Poster
8 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 
How Can We Add A Sound Background Witout Buttons?? I Mean When The First Form Is Opened The Sound Starts With It At The Same Time??



You can add it like this

PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
 
My.Computer.Audio.Play("c:\windows\media\Windows XP Logoff Sound.wav", AudioPlayMode.BackgroundLoop)
MessageBox.Show("Now playing Windows XP Logoff Sound.wav")
End Sub


However, while it works, there may be a better/cleaner way to accomplish the same result.

ChadW
Light Poster
42 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

i cot the followin errors::
(362): Name 'My' is not declared.
(362): Name 'AudioPlayMode' is not declared.

and is it ok if i write .mp3 instead of .wav

**UM HASSAN**
Newbie Poster
8 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 
i cot the followin errors:: (362): Name 'My' is not declared. (362): Name 'AudioPlayMode' is not declared. and is it ok if i write .mp3 instead of .wav



I think maybe you missed a few dots in the commands :)

My.Computer.Audio.Play("c:\windows\media\Windows XP Logoff Sound.wav", AudioPlayMode.BackgroundLoop)
MessageBox.Show("Now playing Windows XP Logoff Sound.wav")

note the following:

My.Computer.Audio.Play
does NOT equal
My Computer Audio Play

If you copy and paste the above code into(replacing) existing code where there errors are being thrown, you should be fine. It works on my end. Hope this helps. :mrgreen:

ChadW
Light Poster
42 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Actually I Copied And Pasted The Code You Kept But Stiil The Same Errors Appear :(

**UM HASSAN**
Newbie Poster
8 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Are you using vb net 2002 or 2003? If so they don't have the My keyword. That is only in vb express 2005. I don't know the code for them. Maybe you can do a Google search for "vb net playsound".

waynespangler
Posting Pro in Training
461 posts since Dec 2002
Reputation Points: 84
Solved Threads: 58
 

YA I'M USING 2003 ...

ANYWAY THX EVERYBODY FOR TRYING TO HELP ME..

i'll TRY TO SEARCH MORE ..

**UM HASSAN**
Newbie Poster
8 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You