Microsoft KB Archive/821767

= How to play audio files by using Visual Basic .NET or Visual Basic 2005 =

Article ID: 821767

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



IN THIS TASK

 * SUMMARY
 * Requirements
 * Create a Windows Application
 * Add a Windows Media Player Control to the Application
 * Add Buttons to Control the Windows Media Player
 * Add an OpenFileDialog Component to Load an Audio File
 * Add Initialization Code
 * Add Code to Load an Audio File
 * Add Code to Play an Audio File
 * Add Code to Pause an Audio File
 * Add Code to Stop an Audio File
 * Sample Code Listing (Module1.vb)
 * Verify That the Code Works
 * Troubleshoot
 * REFERENCES



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 top

Requirements
The 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 top

Create a Windows Application

 * 1) Start Microsoft Visual Studio .NET or Microsoft Visual Basic 2005.
 * 2) On the File menu, point to New, and then click Project.
 * 3) Under Project Types, click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types.
 * 1) Under Templates, click Windows Application.
 * 2) In the Name box, type AudioDemo, and then click OK. By default, Form1.vb is created.

back to the top

Add a Windows Media Player control to the Application

 * 1) Start Visual Studio .NET or Visual Studio 2005.
 * 2) 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.
 * 1) Click the COM Components tab, and then click Browse.
 * 2) Locate and then click Msdxm.ocx, and then click Open.

Note Msdxm.ocx is typically located in /System32, where   is the location of the Windows directory on your computer.
 * 1) 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.
 * 2) 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.
 * 3) AxMediaPlayer1 is added to Form1.

back to the top

Add Buttons to Control the Windows Media Player

 * 1) Add four Button controls to Form1.
 * 2) Click Button1.
 * 3) In the Properties pane, change the Text property of Button1 to Load.
 * 4) Click Button2.
 * 5) In the Properties pane, change the Text property of Button2 to Play.
 * 6) Click Button3.
 * 7) In Properties pane, change the Text property of Button3 to Pause.
 * 8) Click Button4.
 * 9) In the Properties pane, change the Text property of Button4 to Stop.

back to the top

Add an OpenFileDialog Component to Load an Audio File
Add a OpenFileDialog component to Form1.

back to the top

Add Initialization Code
 On the View menu, click Code.  In the &quot;Windows Form Designer generated code&quot; 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 top

Add 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 = &quot;&quot; AxMediaPlayer1.FileName = &quot;&quot; ' Display the Open File dialog box. OpenFileDialog1.ShowDialog ' Verify that Cancel was not clicked. If Not OpenFileDialog1.FileName = &quot;&quot; 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(&quot;The following file has been loaded in the Media Player control: &quot; + AxMediaPlayer1.FileName) ' Enable the Play button. Button2.Enabled = True Else ' Disable the Play button. Button2.Enabled = False End If 

back to the top

Add Code to Play an Audio File
 On the View menu, click Designer.</li>  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 </li></ol>

back to the top

Add Code to Pause an Audio File
 On the View menu, click Designer.</li>  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 </li></ol>

back to the top

Add Code to Stop an Audio File
 On the View menu, click Designer.</li>  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 </li></ol>

back to the top

Sample Code Listing (Form1.vb)
Option Strict On

Public Class Form1 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

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 = &quot;AxMediaPlayer1&quot; Me.AxMediaPlayer1.OcxState = CType(resources.GetObject(&quot;AxMediaPlayer1.OcxState&quot;), 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 = &quot;Button1&quot; Me.Button1.TabIndex = 1 Me.Button1.Text = &quot;Load&quot; '     'Button2 '     Me.Button2.Location = New System.Drawing.Point(32, 104) Me.Button2.Name = &quot;Button2&quot; Me.Button2.TabIndex = 2 Me.Button2.Text = &quot;Play&quot; '     'Button3 '     Me.Button3.Location = New System.Drawing.Point(40, 136) Me.Button3.Name = &quot;Button3&quot; Me.Button3.TabIndex = 3 Me.Button3.Text = &quot;Pause&quot; '     'Button4 '     Me.Button4.Location = New System.Drawing.Point(56, 192) Me.Button4.Name = &quot;Button4&quot; Me.Button4.TabIndex = 4 Me.Button4.Text = &quot;Stop&quot; '     '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 = &quot;Form1&quot; Me.Text = &quot;Form1&quot; CType(Me.AxMediaPlayer1, System.ComponentModel.ISupportInitialize).EndInit Me.ResumeLayout(False)

End Sub


 * 1) 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 = &quot;&quot; AxMediaPlayer1.FileName = &quot;&quot; ' Display the Open File dialog box. OpenFileDialog1.ShowDialog ' Verify that Cancel was not clicked. If Not OpenFileDialog1.FileName = &quot;&quot; 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(&quot;The following file has been loaded in the Media Player control: &quot; + 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:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

Verify That the Code Works

 * 1) On the Build menu, click Build Solution.
 * 2) To run the application, click Start on the Debug menu. Form1 appears.
 * 3) Click Load. The Open dialog box appears.
 * 4) Locate and select any valid audio file, and then click Open. A message box with the full file path appears.
 * 5) To close the message box, click OK.
 * 6) 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.
 * 1) Click Pause or Stop to control the audio output from your application.

back to the top

Troubleshoot
<ul> 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.</li>  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 &quot;Windows Form Designer generated code&quot; 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 </li>  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 </li> This sample may not play audio formats that are not supported by Windows Media Player.</li></ul>

back to the top

<div class="references_section">