I am new to VB coding so I'm trying some easy projects.

I figured an alarm clock would be a good place to start. But I'm Having a hard time with the MP3 Part of it all.

I want a pre-selected MP3 to start, when my timer2 is = to the time of day.

But i Have never really used MP3's in any project. So dose anyone know how i would go about.

A. Adding a MP3 to the coding in the background and then.
B. Calling on it when the alarm goes off.

Dani AI

Generated

Quick summary and which route to pick (short): the forum suggestions already point to three practical approaches. The VB6-era "Microsoft Multimedia Control" mentioned by is a legacy ActiveX and is often not present in modern VS installs (as found). The simplest built-in route for MP3 is the Windows Media Player ActiveX (what and used) — add it from Toolbox -> Choose Items -> COM Components and control playback from the form. For tiny, WAV-only alerts the Framework SoundPlayer exists, but it only supports WAV files (not MP3). (learn.microsoft.com)

Better modern option: use NAudio (open-source, NuGet). It removes COM/ActiveX packaging headaches and gives clean control over play/stop/dispose from VB.NET. Example (VB) using NAudio.Wave:

' Requires NAudio via NuGet
Imports NAudio.Wave

Private outputDevice As WaveOutEvent
Private audioFile As AudioFileReader

Private Sub PlayMp3(path As String)
    StopPlayback()
    audioFile = New AudioFileReader(path)
    outputDevice = New WaveOutEvent()
    outputDevice.Init(audioFile)
    outputDevice.Play()
End Sub

Private Sub StopPlayback()
    If outputDevice IsNot Nothing Then
        outputDevice.Stop()
        outputDevice.Dispose()
        outputDevice = Nothing
    End If
    If audioFile IsNot Nothing Then
        audioFile.Dispose()
        audioFile = Nothing
    End If
End Sub

NAudio is actively maintained and widely used for this exact scenario. (github.com)

Scheduling note (avoid equality mistakes): do not rely on a strict equality check between Timer tick and the alarm time. Check if DateTime.Now >= alarmTime and guard with a boolean so the alarm runs once. Example timer loop:

' Timer interval = 1000ms
Private alarmTime As DateTime = DateTime.Today.AddHours(7) ' example
Private alarmTriggered As Boolean = False

Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    If DateTime.Now >= alarmTime AndAlso Not alarmTriggered Then
        alarmTriggered = True
        PlayMp3("C:\path\to\alarm.mp3") ' or call NAudio playback
    End If
End Sub

This approach avoids missed triggers due to tick granularity. (stackoverflow.com)

Embedding MP3s: possible via Project -> Properties -> Resources, but embedding large MP3s inflates the EXE and VS may link or embed depending on type. A common pattern is to embed and write the resource to a temp file at runtime, then hand that path to the player (sample code below handles byte[] or UnmanagedMemoryStream). For a simple alarm app, shipping the MP3 alongside the EXE or letting the user choose a file is often easier. (scribd.com)

Dim tempFile = IO.Path.Combine(IO.Path.GetTempPath(), "alarm.mp3")
If Not IO.File.Exists(tempFile) Then
    Dim res = My.Resources.AlarmMp3
    If TypeOf res Is Byte() Then
        IO.File.WriteAllBytes(tempFile, CType(res, Byte()))
    ElseIf TypeOf res Is IO.UnmanagedMemoryStream Then
        Using s = CType(res, IO.UnmanagedMemoryStream)
            Using out As New IO.FileStream(tempFile, IO.FileMode.Create, IO.FileAccess.Write)
                s.CopyTo(out)
            End Using
        End Using
    End If
End If
PlayMp3(tempFile)

Notes and cautions drawn from the thread: 's MMControl advice is historically correct but not a great choice for VS2008+ projects; 's AxWindowsMediaPlayer approach is the fastest to get MP3 working, while NAudio is the most robust long-term solution for a VB.NET alarm app.

Recommended Answers

All 5 Replies

A. use Microsoft Multimedia Control. find this control on component.
add this code to play mp3 file :

Private Sub PlaySound()
      On Error GoTo ErrMsg
         AxMMControl1.Wait = True
         AxMMControl1.FileName = "D:\test.mp3"
         AxMMControl1.Command = "Open"
         AxMMControl1.Command = "Play"
         Exit Sub
         ErrMsg:
         MsgBox(Err.Description)
   End Sub

B. just call procedure name

Hello,
I can't find the multimedia control on VS 2008 Pro !!

are you sure it's there?

Thanks

Hi,

Looks like the .Net MultiMediaControl isn't for free, look here.

What you can do is add the Windows mediaplayer control.

put this in the function:

AxWindowsMediaPlayer1.URL = "e:\music\Wanksta.mp3"
AxWindowsMediaPlayer1.Visible = False

and if you want to stop:

AxWindowsMediaPlayer1.close()

AxWindowsMediaPlayer1 is a COM component. this component you can add in the ToolBox whit right click and choose component.

I hope this helps

Yep, thanks so much :)
the Windows mediaplayer control is great...

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.