Click here to Skip to main content
Click here to Skip to main content

Simple VB.NET MIDI & Wave Play Class

By , 7 Oct 2004
 

Introduction

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 VB.net 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

        Get
            Return File
        End Get

    End Property

    Public Function PlaySound() As Boolean

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

    End Function

    Public Function StopSound() As Boolean

        StopFile()

    End Function

    Private Function PlayFile() As Boolean

        Dim lRet As Long

        Try
            StopFile()

            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

        Try
            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
        Else
            Return True
        End If

    End Function

End Class

In your application, play a sound like this:

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

or

Sound.StopSound()

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

License

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

EcoSys
Germany Germany
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: Help!sussAnonymous20 Apr '05 - 23:53 
To add a reference, go to the project menu and select add reference close to the bottom, select the .com sub-menu
and find directx 7, click select and then ok. The problems should be fixed.
GeneralPlaying files sequentiallymemberlearner5413 Nov '04 - 10:00 
I'm very new to programming. I created your SoundPlayer. Where do the "Dim Sound As New SoundPlayer (".\sound\testmidi.mid")... code lines fit in? Are these put into the same "class" file that was added?Also, how would I play 3 files sequentially...no pause in between. Thanks for any help!
GeneralRe: Playing files sequentiallymemberycc7124 Jan '05 - 23:32 
hi, i am also new to VB_NET.
I struggle quite a while on this (no sound coming out). because when invalid file given, the program did not give warning...
Now finally able to play mid sound.
 
"Dim Sound As New SoundPlayer (".\sound\testmidi.mid")... code can fit in your Private Sub form1_Load and any other SUB under the Public form1 class...
you can put your test.mid in bin directory then use "Dim Sound As New SoundPlayer ("test.mid")"
[Initially i tried (".test.mid") which is not valid]Smile | :)
 

 

Questionhow we know the midi have finisedmembersubandono18 Oct '04 - 17:22 
please show me the code how to detect the midi have finished
 
thx from indonesia Smile | :)
AnswerRe: how we know the midi have finisedmemberEcoSys19 Oct '04 - 13:37 
Hi
 
This could be done by the MCI Command MCI_STATUS.
 
HowTo's could be found on the Microsoft Site.
The link is in my own (1st) post.
 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_mci_status.asp[^]
GeneralMCI CommandsmemberEcoSys11 Oct '04 - 2:07 
For all who wants to know what commands could be send:
 
A list of all MCI commands from the Microsoft Site:
 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_mci_commands.asp

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

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