This step-by-step article describes how to play audio files
by using Microsoft Visual Basic .NET or Microsoft Visual Basic 2005. This
article also contains sample code that illustrates the concepts that are
discussed in the article.
Add a Windows Media Player control to the Application
Start Visual Studio .NET or Visual Studio 2005.
On the Tools menu, click
Add/Remove Toolbox Items.
Note In Visual Studio .NET 2002, on the Tools menu,
click Customize Toolbox.
In Visual Studio 2005,
click Choose Toolbox Items on the Tools
menu.
Click the COM Components tab, and then
click Browse.
Locate and then click Msdxm.ocx, and then
click Open.
Note Msdxm.ocx is typically located in
%WINDIR%/System32, where
%WINDIR% is the location of the Windows directory on
your computer.
In the Customize Toolbox or Choose
Toolbox Item dialog box, click OK. In Visual Studio
.NET 2003 and in Visual Studio 2005, a WindowsMediaPlayer
control is added to the toolbox. In Visual Studio .NET 2002, a
MediaPlayer control is added to the toolbox.
In Visual Studio .NET 2003 or in Visual Studio 2005, add a
WindowsMediaPlayer control to Form1. In Visual Studio .NET 2002, add a MediaPlayer control.
In the "Windows Form Designer generated code" region,
locate the following code:
InitializeComponent()
Add the following code after the code that you located in
step 2:
' Disable the Play, the Pause, and the Stop buttons.
Button2.Enabled = False
Button3.Enabled = False
Button4.Enabled = False
' Hide the Windows Media Player.
AxMediaPlayer1.Visible = False
Double-click the Load control, and then
add the following code to the Button1_Click event-handler:
' Reset the file names for the Open File dialog box and for the Media Player.
OpenFileDialog1.FileName = ""
AxMediaPlayer1.FileName = ""
' Display the Open File dialog box.
OpenFileDialog1.ShowDialog()
' Verify that Cancel was not clicked.
If Not OpenFileDialog1.FileName = "" Then
' Disable the Load button.
Button1.Enabled = False
' Prevent the Media Player from automatically playing loaded files.
AxMediaPlayer1.AutoStart = False
' Set the Media Player audio file.
AxMediaPlayer1.FileName = OpenFileDialog1.FileName
MessageBox.Show("The following file has been loaded in the Media Player control: " + AxMediaPlayer1.FileName)
' Enable the Play button.
Button2.Enabled = True
Else
' Disable the Play button.
Button2.Enabled = False
End If
Double-click the Play control, and then
add the following code to the Button2_Click event-handler:
' Disable the Load and the Play buttons.
Button1.Enabled = False
Button2.Enabled = False
' Play the audio file.
AxMediaPlayer1.Play()
' Enable the Pause and the Stop buttons.
Button3.Enabled = True
Button4.Enabled = True
Double-click the Stop control, and then
add the following code to the Button4_Click event-handler:
' Disable the Pause and the Stop buttons.
Button3.Enabled = False
Button4.Enabled = False
' Stop playing the audio file, and then reset the next play position to the beginning.
AxMediaPlayer1.Stop()
AxMediaPlayer1.CurrentPosition = 0
' Enable the Load and the Play buttons.
Button1.Enabled = True
Button2.Enabled = True
Option Strict On
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()
' Disable the Play, the Pause, and the Stop buttons.
Button2.Enabled = False
Button3.Enabled = False
Button4.Enabled = False
' Hide the Media Player.
AxMediaPlayer1.Visible = False
' 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 AxMediaPlayer1 As AxMediaPlayer.AxMediaPlayer
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.AxMediaPlayer1 = New AxMediaPlayer.AxMediaPlayer
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Button4 = New System.Windows.Forms.Button
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
CType(Me.AxMediaPlayer1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'AxMediaPlayer1
'
Me.AxMediaPlayer1.Location = New System.Drawing.Point(224, 0)
Me.AxMediaPlayer1.Name = "AxMediaPlayer1"
Me.AxMediaPlayer1.OcxState = CType(resources.GetObject("AxMediaPlayer1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxMediaPlayer1.Size = New System.Drawing.Size(286, 225)
Me.AxMediaPlayer1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(16, 72)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Load"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(32, 104)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 2
Me.Button2.Text = "Play"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(40, 136)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 3
Me.Button3.Text = "Pause"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(56, 192)
Me.Button4.Name = "Button4"
Me.Button4.TabIndex = 4
Me.Button4.Text = "Stop"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(712, 397)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.AxMediaPlayer1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.AxMediaPlayer1, 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
' Reset the file names for the Open dialog box and for the Media Player.
OpenFileDialog1.FileName = ""
AxMediaPlayer1.FileName = ""
' Display the Open File dialog box.
OpenFileDialog1.ShowDialog()
' Verify that Cancel was not clicked.
If Not OpenFileDialog1.FileName = "" Then
' Disable the Load button.
Button1.Enabled = False
' Prevent the Media Player from automatically playing loaded files.
AxMediaPlayer1.AutoStart = False
' Set the Media Player audio file.
AxMediaPlayer1.FileName = OpenFileDialog1.FileName
MessageBox.Show("The following file has been loaded in the Media Player control: " + AxMediaPlayer1.FileName)
' Enable the Play button.
Button2.Enabled = True
Else
' Disable the Play button.
Button2.Enabled = False
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Disable the Load and the Play buttons.
Button1.Enabled = False
Button2.Enabled = False
' Play the audio file.
AxMediaPlayer1.Play()
' Enable the Pause and the Stop buttons.
Button3.Enabled = True
Button4.Enabled = True
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' Disable the Pause button.
Button3.Enabled = False
' Pause the audio file.
AxMediaPlayer1.Pause()
' Enable the Play button.
Button2.Enabled = True
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
' Disable the Pause and the Stop buttons.
Button3.Enabled = False
Button4.Enabled = False
' Stop playing the audio file, and then reset the next play position to the beginning.
AxMediaPlayer1.Stop()
AxMediaPlayer1.CurrentPosition = 0
' Enable the Load and the Play buttons.
Button1.Enabled = True
Button2.Enabled = True
End Sub
End Class
Note You must change the code in Visual Basic 2005. By default, Visual
Basic creates two files for the project when you create a Windows Forms
project. If the form is named Form1, the two files that represent the form are
named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file.
The Windows Forms Designer writes the code in the Form1.Designer.vb file. The
Windows Forms Designer uses the partial keyword to divide the implementation of
Form1 into two separate files. This behavior prevents the designer-generated
code from being interspersed with your code.
For more information
about the new Visual Basic 2005 language enhancements, visit the following
Microsoft Developer Network (MSDN) Web site:
To run the application, click Start on the
Debug menu. Form1 appears.
Click Load. The Open
dialog box appears.
Locate and select any valid audio file, and then click
Open. A message box with the full file path
appears.
To close the message box, click
OK.
To start playing the audio file, click
Play. You hear corresponding audio output.
Note You hear audio output only if your computer has a sound card that
is configured correctly and if you use speakers, earphones, or headphones to
hear the output.
Click Pause or Stop to
control the audio output from your application.
You hear the audio output only if your computer has a sound
card that is configured correctly and if you use speakers, earphones, or
headphones to hear the output.
You may hear audio output before you click
Play. This behavior occurs when the AutoStart property of your Media Player is not set to false. By default, the AutoStart property is set to true. To work around this issue, locate the following line in the
"Windows Form Designer generated code" region:
InitializeComponent()
and then add the following code after the code that you have located.
' Prevent the Media Player from automatically playing loaded files.
AxMediaPlayer1.AutoStart = False
When you click Stop and then click
Play, the audio file may not start playing from the beginning.
Instead, the audio file starts playing from where the file stopped playing.
This behavior occurs when the play position is not reset to the beginning of
the audio file. To work around this issue, use the following code in the Button4_Click event-handler:
AxMediaPlayer1.CurrentPosition = 0
This sample may not play audio formats that are not
supported by Windows Media Player.