Click here to Skip to main content
15,891,763 members
Articles / Desktop Programming / Windows Forms

Play sound in VB.NET

Rate me:
Please Sign up or sign in to vote.
3.47/5 (14 votes)
27 Jan 2006CPOL 255.3K   5.9K   44  
This sample presents a simple solution to play sound (MP3, Wav etc..) in a VB.NET (2003) solution.
'*******************************************************************************
'* VB.NET sample from Daniel Hofstetter
'* www.daniel-hofstetter.ch
'*******************************************************************************
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
Friend WithEvents AxWindowsMediaPlayer1 As AxWMPLib.AxWindowsMediaPlayer
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TrackBar1 As System.Windows.Forms.TrackBar
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.AxWindowsMediaPlayer1 = New AxWMPLib.AxWindowsMediaPlayer
Me.Button1 = New System.Windows.Forms.Button
Me.TrackBar1 = New System.Windows.Forms.TrackBar
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
CType(Me.AxWindowsMediaPlayer1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'AxWindowsMediaPlayer1
'
Me.AxWindowsMediaPlayer1.Enabled = True
Me.AxWindowsMediaPlayer1.Location = New System.Drawing.Point(234, 228)
Me.AxWindowsMediaPlayer1.Name = "AxWindowsMediaPlayer1"
Me.AxWindowsMediaPlayer1.OcxState = CType(resources.GetObject("AxWindowsMediaPlayer1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxWindowsMediaPlayer1.Size = New System.Drawing.Size(66, 36)
Me.AxWindowsMediaPlayer1.TabIndex = 0
Me.AxWindowsMediaPlayer1.Visible = False
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(60, 54)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(90, 42)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Select a sond file..."
'
'TrackBar1
'
Me.TrackBar1.Location = New System.Drawing.Point(54, 144)
Me.TrackBar1.Maximum = 100
Me.TrackBar1.Name = "TrackBar1"
Me.TrackBar1.TabIndex = 2
Me.TrackBar1.Value = 100
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(78, 132)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(48, 12)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Volume"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(162, 156)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(48, 12)
Me.Label2.TabIndex = 4
Me.Label2.Text = "Max."
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(12, 156)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(30, 12)
Me.Label3.TabIndex = 5
Me.Label3.Text = "Min."
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.TrackBar1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.AxWindowsMediaPlayer1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.AxWindowsMediaPlayer1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

    End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) _
                          Handles Button1.Click

        Const DATA_FILE_EXTENSION As String = ".mp3"
        Dim dlgFileDialog As New OpenFileDialog

            With dlgFileDialog
            .Filter = DATA_FILE_EXTENSION & _
            " files (*" & DATA_FILE_EXTENSION & "|*" & DATA_FILE_EXTENSION

            .FilterIndex = 1
            .RestoreDirectory = True

            If .ShowDialog() = DialogResult.OK Then

                'Play the sound...
                Me.AxWindowsMediaPlayer1.URL = dlgFileDialog.FileName

            End If

        End With

End Sub

Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll

    'Volume setting
    Me.AxWindowsMediaPlayer1.settings.volume = Me.TrackBar1.Value

End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Switzerland Switzerland
VBA and .net software writer.
For details see my website www.daniel-hofstetter.ch

Comments and Discussions