Hello, I'm working at a project and I want to add a MP3 song an play it when the program that i made will be started... any ideas?

Dani AI

Generated

— the reason your MP3 didn’t play is that the snippet posted uses System.Media.SoundPlayer, and SoundPlayer only supports WAV (it will not decode MP3). That’s why the code runs but you hear nothing.

Two easy, reliable alternatives:

  • Use the Windows Media Player ActiveX control (quick for WinForms): add the COM component "Windows Media Player" to the Toolbox, drop the AxWindowsMediaPlayer onto the form, then set its URL and play. Example:
' (WinForms) put an AxWindowsMediaPlayer on the form first
axWindowsMediaPlayer1.URL = System.IO.Path.Combine(Application.StartupPath, "song.mp3")
axWindowsMediaPlayer1.settings.volume = 50
axWindowsMediaPlayer1.Ctlcontrols.play()

Set the MP3 file’s project property "Copy to Output Directory" to "Copy if newer" (or use an absolute path) so the file is found at runtime.

  • Use a managed library for more control (recommended if you want programmatic control or no COM): NAudio (install via NuGet). Example:
Imports NAudio.Wave

Private outputDevice As WaveOutEvent
Private audioFile As AudioFileReader

' in Form_Load:
outputDevice = New WaveOutEvent()
audioFile = New AudioFileReader(System.IO.Path.Combine(Application.StartupPath, "song.mp3"))
outputDevice.Init(audioFile)
outputDevice.Play()

' on closing:
outputDevice.Stop()
audioFile.Dispose()
outputDevice.Dispose()

Quick troubleshooting: verify the MP3 plays in Windows Media Player first; use an absolute path or ensure the file is copied to the output folder; watch for exceptions in your Form.Load; if you embed the MP3 in resources, extract it to a temp file before playing. For simple start-up playback put the play call in Form.Load (or Sub Main) so it runs when the app starts.

Recommended Answers

All 4 Replies

Hi,

Try this (in your form or a Module):

Private PlaySound As New System.Media.SoundPlayer

Public Sub PlaySoundFile(ByVal SoundPath As String)
        PlaySound .SoundLocation = SoundPath
        PlaySound .Load()
        PlaySound .Play()
End Sub

Then call it from the Form Load event (or wherever you like):

PlaySoundFile(<path to mp3 here>)

I know this works for wav files so it should be good for mp3s too.

HTH,

Chris.

well...i tried this and doesn't work...i don't know why..... does anyone know some other examples?

yeah...and still doesn't work...i really don't get this...but..can anyone give me other examples..

sincerely,

pettrelutza

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.