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:

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.

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!

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??

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.

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 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:

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".

This article has been dead for over six months. Start a new discussion instead.