Posts Tagged ‘playing mp3 wav files’

One of the cool things of the program is to be able to play sounds, and play other formats on-demand.  For this example we will play a chime clock at noon.

There are a couple of ways of doing this (API Calls, command line etc), The way I will show is for me the simplest way, and really only requires that you have windows media player installed.
If you are going to play MP3 files, you will also need to install the codec’s for it, the easiest way to do this is just download and install winamp (Free version).

1st Step is to make sure you Media player is setup and ready to go.

Note: Using the media player in the background allows me pause, get status, use playlists, set the volume and many other things, sure you can probably do this with API calls and other functions but the main purpose of my CPU power I want to dedicate to phrasing the executing Insteon commands.  Also besides being clean and simple, once the player is started you code will continue to run, it will not pause or stop while the media is playing. It will  only stop unless you tell it to, as well if you break the program.

Again using I was using VB2005 / XP, the first thing you will do is reference the object in your code.   Here is a picture of my reference.

We’ll use for our code the variable WMP and reference this,
In this case I refernce it twice so I have one for quick sounds and another for Internet Radio which can be playing all the time if needed, so they dont cancel each other out.

Public WMP As New WMPLib.WindowsMediaPlayer
Public WMP_Radio as New WMPLib.WindowsMediaPlayer

To point the player to the filename it actually called using the WMP.URL=”<full path to filename or source>” this can point to a streaming URL, Wav,.MP3 file even a saved playlist. (f.y.i. Playlist are simple text files which can easily be created if needed)

Here is the function I use to call the Media play and play a specific file, doesnt matter if its .WAV or .MP3, All my sounds are stored locally under C:\voicemail\sounds\ my function also passes the volume which ranges from 1-100. This is for the player not for the Windows Mixer.

Play_wav_file("doorchime.wav",100) - Play doorchime full blast!

    Public Function Play_wav_file(ByVal Filename As String, ByVal vol As Integer)
        WMP.URL = "c:\voicemail\sounds\" + Filename
        WMP.settings.volume = vol

    End Function

You can also test to see if the Media player is playing to prevent errors with its WMP.Status
Here are some others

WMP_Radio.controls.stop() - Will stop the player.

Here is my function to play Internet Radio, I can call it using various command such as

PlayMusic(“play80”) or PlayMusic(“stop”) etc…

 Public Function PlayMusic(ByVal cmd As String)
        Dim volume As Integer
        cmd = LCase(cmd)

        If cmd = "play80" Then
            WMP_Radio.URL = ""
            WMP_Radio.settings.volume = 30
        End If

        If cmd = "playsoft" Then
            WMP_Radio.URL = ""
            WMP_Radio.settings.volume = 20
        End If

        If cmd = "playhits" Then
            WMP_Radio.URL = ""
            WMP_Radio.settings.volume = 20
        End If

        If cmd = "stop" Then
        End If

        If cmd = "up" Then
            volume = WMP_Radio.settings.volume
            If volume <= 90 Then WMP_Radio.settings.volume = volume + 10
        End If

        If cmd = "down" Then
            volume = WMP_Radio.settings.volume
            If volume >= 10 Then WMP_Radio.settings.volume = volume - 10
        End If

    End Function

A very good link which I use for some of my sounds is, you will need to register to download them.

Yes, this is me around 20 years ago.  Enjoy!