Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / Windows Forms

Mixing and playing sounds with the Windows Media Player

Rate me:
Please Sign up or sign in to vote.
4.66/5 (18 votes)
30 May 20066 min read 77.3K   1.4K   40  
A simple utility to create a mixing sounds keyboard.
Imports WMPLib
Public Class RunningSound

  Private mHostingDataRow As DataRow
  Private mSoundFilename As String
  Private WithEvents mPlayer As New WindowsMediaPlayerClass
  Private mPlayerControls As IWMPControls

  Public ReadOnly Property SoundFilename() As String
    Get
      Return mSoundFilename
    End Get
  End Property

  Public ReadOnly Property Player() As WindowsMediaPlayerClass
    Get
      Return mPlayer
    End Get
  End Property

  Public ReadOnly Property PlayerControls() As IWMPControls
    Get
      Return mPlayer.controls
    End Get
  End Property

  Public Sub New(ByVal HostingDataRow As DataRow, ByVal SoundsBasePath As String)
    Dim SoundFilename As String = SoundsBasePath & "\" & HostingDataRow("Filename")
    mHostingDataRow = HostingDataRow
    mSoundFilename = SoundFilename
    mPlayer.settings.autoStart = False
    Dim m As IWMPMedia = mPlayer.newMedia(SoundFilename)
    mPlayer.currentMedia = m
    AddHandler mPlayer.PlayStateChange, AddressOf mPlayer_PlayStateChange
  End Sub

  Public Overrides Function ToString() As String
    Dim result As String
    Select Case mPlayer.playState
      Case WMPPlayState.wmppsPlaying
        result = "► "
      Case WMPPlayState.wmppsPaused
        result = "# "
      Case Else
        result = ""
    End Select
    If mPlayer.settings.getMode("LOOP") Then
      result &= "∞"
    End If
    Return result
  End Function

  Private Sub mPlayer_PlayStateChange(ByVal NewState As Integer)
    ' To update the Status on grid
    mHostingDataRow.Table.AcceptChanges()
  End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
Technical Lead
Italy Italy
I was born in 1970.

My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.

I graduated in Electronic Engineering and earned the following Microsoft certifications:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

I worked in IT as a developer, a teacher, a consultant, a technical writer, a technical leader.
IT knowledge applied to real life is my primary interest and focus.

Comments and Discussions