How to play audio files by using Visual Basic .NET or Visual Basic 2005 (821767)
The information in this article applies to:
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
SUMMARY 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. back to the topRequirementsThe following list outlines the recommended hardware, software,
network infrastructure, and service packs that you need:
- Microsoft Windows Server 2003, Microsoft Windows 2000, Microsoft Windows XP, or Microsoft
Windows XP Service Pack 1
- Visual Basic .NET or Visual Basic 2005
Note The code that is provided in this article is not supported on Microsoft
Windows Server 2003. This article assumes that you are familiar with
the following topics:
- Visual Basic .NET or Visual Basic 2005 programming
- The Windows Media Player software development kit (SDK)
- Component Object Model (COM) interoperability in Visual Basic .NET or in Visual Basic 2005
back to the topCreate a Windows Application- Start Microsoft Visual Studio .NET or Microsoft Visual Basic 2005.
- On the File menu, point to
New, and then click Project.
- Under Project Types, click
Visual Basic Projects.
Note In Visual Studio 2005, click Visual Basic under Project Types. - Under Templates, click
Windows Application.
- In the Name box, type
AudioDemo, and then click OK. By
default, Form1.vb is created.
back to the topAdd 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.
- AxMediaPlayer1 is added to
Form1.
back to the topAdd Buttons to Control the Windows Media Player- Add four Button controls to Form1.
- Click Button1.
- In the Properties pane, change the Text property of Button1 to
Load.
- Click Button2.
- In the Properties pane, change the Text property of Button2 to
Play.
- Click Button3.
- In Properties pane, change the Text property of Button3 to
Pause.
- Click Button4.
- In the Properties pane, change the Text property of Button4 to
Stop.
back to the topAdd an OpenFileDialog Component to Load an Audio FileAdd a OpenFileDialog component to Form1. back to
the topAdd Initialization Code- On the View menu, click
Code.
- 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
back to the topAdd Code to Load an Audio File- On the View menu, click Designer.
- 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 back to the topAdd Code to Play an Audio File- On the View menu, click Designer.
- 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 back to the topAdd Code to Pause an Audio File- On the View menu, click Designer.
- Double-click the Pause control, and then add the following
code to the Button3_Click event-handler:
' Disable the Pause button.
Button3.Enabled = False
' Pause the audio file.
AxMediaPlayer1.Pause()
' Enable the Play button.
Button2.Enabled = True back to the topAdd Code to Stop an Audio File- On the View menu, click Designer.
- 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 back to the topSample Code Listing (Form1.vb)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:
For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:
Verify That the Code Works- On the Build menu, click Build
Solution.
- 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.
back to the topTroubleshootback to the
topREFERENCES For more information, visit the following Microsoft
Developer Network (MSDN) Web sites: back to the
top
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005applies kbvs2005swept kbWaveAudio kbSDK kbCOMInterop kbComCtrls kbsound kbinterop kbAppDev kbCtrl kbControl kbProgramming kbSample kbHOWTOmaster KB821767 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|