Click here to Skip to main content
13,355,726 members (63,005 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 8 Oct 2004

Simple VB.NET MIDI & Wave Play Class

, 8 Oct 2004
Rate this:
Please Sign up or sign in to vote.
MIDI & Wave Player for VB.NET.


This is a simple class to play MIDI and Wave files with your VB.NET application.

Using the code

Add a new class file into your project and copy the code into it. Make a reference to DirectX7 COM-Object.

Imports System
Imports DxVBLib

Public Class SoundPlayer

    ' Basic Wave and MIDI Player Class for 1.1
    ' Requires COM-Object : DirectX7 

    Private Declare Function mciSendString Lib "Winmm.dll" Alias "mciSendStringA" _
    (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
    ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
    Private File As String

    Private m_dx As New DirectX7
    Private m_ds As DirectSound

    Public Sub New(ByVal strFileName As String)

        Me.File = strFileName

    End Sub

    ' To show what file is selected (if needed)
    Public ReadOnly Property FileName() As String

            Return File
        End Get

    End Property

    Public Function PlaySound() As Boolean

        If InitAudio() = True Then
            If PlayFile() = True Then
                Return True
                Return False
            End If
            ' Audiohardware not found
            ' exit here
            Return False
        End If

    End Function

    Public Function StopSound() As Boolean


    End Function

    Private Function PlayFile() As Boolean

        Dim lRet As Long


            lRet = mciSendString("open " & Me.File & " alias track", "", 0, 0)
            lRet = mciSendString("play track", "", 0, 0)
            PlayFile = (lRet = 0)

        Catch ex As Exception
            Return False
        End Try

        Return True

    End Function

    Public Function CloseAudio()

        mciSendString("close all", 0, 0, 0)

    End Function

    Private Function StopFile() As Boolean

        Dim lRet As Long

            lRet = mciSendString("stop track", "", 0, 0)
            lRet = mciSendString("close track", "", 0, 0)
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    Private Function InitAudio() As Boolean

        m_ds = m_dx.DirectSoundCreate("")
        If Err.Number <> 0 Then
            Return False
            Return True
        End If

    End Function

End Class

In your application, play a sound like this:

Dim Sound As New SoundPlayer(".\sound\testmidi.mid")



to stop playing.

Sound.FileName shows the filename given to the class on declare.

I didn't test it but I'm sure that it works with DX8 or DX9 too. And always remember: this code is for the beginners to show how to play MIDI and Wave files. The class plays each file once. No loop...


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Shimmy Weitzhandler20-Jun-10 16:33
memberShimmy Weitzhandler20-Jun-10 16:33 
GeneralMy vote of 1 Pin
Mike Trethowan8-Jun-09 13:38
memberMike Trethowan8-Jun-09 13:38 
GeneralPlay File name with Chinese Characters Pin
serene joey25-Sep-08 18:33
memberserene joey25-Sep-08 18:33 
Generalhelp me please! Pin
taki3310-Jun-08 23:54
membertaki3310-Jun-08 23:54 
GeneralHelp!! Pin
nicieri20-Feb-07 15:25
membernicieri20-Feb-07 15:25 
GeneralHelp with mci progress bar plzzzzz Pin
steve shafa18-Dec-06 7:43
membersteve shafa18-Dec-06 7:43 
GeneralHelp !! ( also) Pin
totok_fr2-Dec-06 15:44
membertotok_fr2-Dec-06 15:44 
QuestionAny suggestion on how to record wav / mp3 files ? Pin
Parik Advani12-Jun-06 4:00
memberParik Advani12-Jun-06 4:00 
GeneralHelp with setup project Pin
kche1289-Sep-05 18:55
memberkche1289-Sep-05 18:55 
Hi. I can play the mid files with no problem, and now i wish to make my program into an installation package. I followed the instruction, but it raises an error when I trying to add the primary output, it says

"The following files may have dependencies that cannot be determind automatically. Please cofirm that all dependencies have been added to te project.


what shoud I do.
Thank you

GeneralHelp! Pin
Anonymous29-Jan-05 11:37
sussAnonymous29-Jan-05 11:37 
GeneralRe: Help! Pin
Anonymous21-Apr-05 0:53
sussAnonymous21-Apr-05 0:53 
GeneralPlaying files sequentially Pin
learner5413-Nov-04 11:00
memberlearner5413-Nov-04 11:00 
GeneralRe: Playing files sequentially Pin
ycc7125-Jan-05 0:32
memberycc7125-Jan-05 0:32 
Questionhow we know the midi have finised Pin
subandono18-Oct-04 18:22
membersubandono18-Oct-04 18:22 
AnswerRe: how we know the midi have finised Pin
EcoSys19-Oct-04 14:37
memberEcoSys19-Oct-04 14:37 
GeneralMCI Commands Pin
EcoSys11-Oct-04 3:07
memberEcoSys11-Oct-04 3:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 8 Oct 2004
Article Copyright 2004 by EcoSys
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid