Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleStart and stop a MIDI audio file by using DirectX in VB .NET
DescriptionThis example shows how to start and stop a MIDI audio file by using DirectX in VB .NET. It uses an Audio object.
KeywordsMIDI, DirectX, Audio, multimedia, VB .NET
CategoriesMultimedia, VB.NET
 
First add a reference to the Microsoft.DirectX.AudioVideoPlayback library.

When the user clicks the Play/Stop button, the program checks the button's caption to see whether it should start or stop playing. To start playing, it creates a new Audio object, passing its constructor the name of the file to play. It calls the object's Play method, records the time, and enables the tmrPosition Timer control.

When the Timer's event fires, the program displays the elapsed time and the audio file's total time.

To stop playing, the program calls the Audio object's Stop method. It then calls its Dispose method to free system resources.

 
Imports Microsoft.DirectX.AudioVideoPlayback

Private Sub btnPlay_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnPlay.Click
    If btnPlay.Text = "Play" Then
        ' Start playing.
        btnPlay.Text = "Stop"
        m_Audio = New Audio(txtFile.Text)
        m_Audio.Play()
        m_StartTime = Now
        tmrPosition.Enabled = True
    Else
        ' Stop playing.
        btnPlay.Text = "Play"
        m_Audio.Stop()
        m_Audio.Dispose()
        m_Audio = Nothing
        tmrPosition.Enabled = False
        lblPosition.Text = ""
    End If
End Sub

Private Sub tmrPosition_Tick(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles tmrPosition.Tick
    Dim elapsed_time As TimeSpan = Now.Subtract(m_StartTime)
    lblPosition.Text = _
        elapsed_time.Seconds.ToString & "/" & _
        m_Audio.Duration.ToString
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated