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:

Dani AI

Generated

This thread already shows two useful paths: simple WAV playback (as suggested by and ) and the observation that VB.NET 2003 users (see @UMHASSAN) hit the missing My helper. For a cross-version solution that supports MP3 in older VB.NET (2002/2003) without relying on My, use the Windows Multimedia API (mciSendString) or the Windows Media Player control. Below is a compact, proven mciSendString approach that works on .NET 1.1 and later.

' VB.NET (works in VS 2003 / .NET 1.1)
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

Sub PlayBackgroundMP3(ByVal path As String)
    mciSendString("open """ & path & """ alias BgMusic", Nothing, 0, 0)
    mciSendString("play BgMusic repeat", Nothing, 0, 0)
End Sub

Sub StopBackgroundMP3()
    mciSendString("stop BgMusic", Nothing, 0, 0)
    mciSendString("close BgMusic", Nothing, 0, 0)
End Sub

Alternatives:

  • Embed the Windows Media Player ActiveX control on the form (add COM reference to Windows Media Player) for richer controls and MP3 support.
  • For modern projects, consider a managed audio library like NAudio (NAudio on GitHub) for more features and finer control.
  • For WAV-only playback, use the built-in System.Media.SoundPlayer class (SoundPlayer docs).

Troubleshooting and deployment notes: mciSendString relies on system codecs, so MP3 playback can fail if an MP3 codec/decoder is missing. Always use full paths (quote them if they contain spaces), call Stop/Close when exiting, and set audio files to copy to output or embed/extract them at runtime to avoid missing-file errors. For reference on the API used above, see the Microsoft mciSendString documentation: .

Recommended Answers

All 10 Replies

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!

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.

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:

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

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

YA I'M USING 2003 ...

ANYWAY THX EVERYBODY FOR TRYING TO HELP ME..

i'll TRY TO SEARCH MORE ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.