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

Article translations Article translations
Article ID: 821767 - View products that this article applies to.
Expand all | Collapse all

On This Page

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.

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

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.
  4. Under Templates, click Windows Application.
  5. In the Name box, type AudioDemo, and then click OK. By default, Form1.vb is created.

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.
  3. Click the COM Components tab, and then click Browse.
  4. 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.
  5. 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.
  6. 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.
  7. AxMediaPlayer1 is added to Form1.

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.

Add an OpenFileDialog Component to Load an Audio File

Add a OpenFileDialog component to Form1.

Add Initialization Code

  1. On the View menu, click Code.
  2. In the "Windows Form Designer generated code" region, locate the following code:
    InitializeComponent()
  3. 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
    

Add Code to Load an Audio File

  1. On the View menu, click Designer.
  2. 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

Add Code to Play an Audio File

  1. On the View menu, click Designer.
  2. 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

Add Code to Pause an Audio File

  1. On the View menu, click Designer.
  2. 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

Add Code to Stop an Audio File

  1. On the View menu, click Designer.
  2. 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

Sample 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:
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.
  7. Click Pause or Stop to control the audio output from your application.

Troubleshoot

  • 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.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
Windows Media Player 6.4 SDK
http://msdn2.microsoft.com/en-us/library/ms984011.aspx
COM Interop
http://msdn2.microsoft.com/en-us/library/6bw51z5z(vs.71).aspx

Properties

Article ID: 821767 - Last Review: November 26, 2007 - Revision: 2.8
APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbvs2005applies kbvs2005swept kbwaveaudio kbsdk kbcominterop kbcomctrls kbsound kbinterop kbappdev kbctrl kbcontrol kbprogramming kbsample kbhowtomaster KB821767

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com