Click here to Skip to main content
6,634,665 members and growing! (15,671 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Multimedia     Intermediate

Simple VB.NET MIDI & Wave Play Class

By EcoSys

MIDI & Wave Player for VB.NET.
VB.NET 1.1, Win2K, WinXP, Win2003, DirectX, VS.NET2003, Dev
Posted:8 Oct 2004
Views:66,974
Bookmarked:21 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 2.00 Rating: 2.86 out of 5
1 vote, 20.0%
1
1 vote, 20.0%
2
1 vote, 20.0%
3
1 vote, 20.0%
4
1 vote, 20.0%
5

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


Member

Location: Germany Germany

Other popular Audio and Video articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberMike Trethowan13:38 8 Jun '09  
GeneralPlay File name with Chinese Characters Pinmemberserene joey18:33 25 Sep '08  
Generalhelp me please! Pinmembertaki3323:54 10 Jun '08  
GeneralHelp!! Pinmembernicieri15:25 20 Feb '07  
GeneralHelp with mci progress bar plzzzzz Pinmembersteve shafa7:43 18 Dec '06  
GeneralHelp !! ( also) Pinmembertotok_fr15:44 2 Dec '06  
GeneralAny suggestion on how to record wav / mp3 files ? PinmemberParik Advani4:00 12 Jun '06  
GeneralHelp with setup project Pinmemberkche12818:55 9 Sep '05  
GeneralHelp! PinsussAnonymous11:37 29 Jan '05  
GeneralRe: Help! PinsussAnonymous0:53 21 Apr '05  
GeneralPlaying files sequentially Pinmemberlearner5411:00 13 Nov '04  
GeneralRe: Playing files sequentially Pinmemberycc710:32 25 Jan '05  
Generalhow we know the midi have finised Pinmembersubandono18:22 18 Oct '04  
GeneralRe: how we know the midi have finised PinmemberEcoSys14:37 19 Oct '04  
GeneralMCI Commands PinmemberEcoSys3:07 11 Oct '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Oct 2004
Editor: Smitha Vijayan
Copyright 2004 by EcoSys
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project