MultiMedia Supplementary Chapter

[The book doesn’t have a good chapter on using multimedia in VB so we decided to add this chapter here on our site.]

What is multimedia?  Taken literally, multimedia would be the use of “more than one method of expression” in your program.  But in actual use, the word multimedia generally means adding sound and/or video to the text and non-moving pictures your program already has.

Some common types of multimedia you can use in Microsoft Windows (for the unabbreviated names of these formats, please see the page that lists “Media Types Supported by Windows Media Player”):

Audio

Video

CD

DVD

MP3

MP2

AU

MP4

RA

WMV

WAV

RAM

WMA

MOV

MIDI

MPEG

AIFF

DIVX

ASF

AVI

The chart above only lists a fraction of the different types of encoding and compression techniques currently in use for distributing sound and video.

In order to decode a digital multimedia file you either need to add a lot of complex code to your program or use a pre-built add-in control that you can put in the Visual Studio Toolbox.

Windows Media Player

One of the most common multimedia player controls is the Windows Media Player.  Most Windows computers already have this player installed and so you can simply write code to use it.  (Other common players – which you could also use in your programs – are Apple’s Quicktime player and RealNetwork’s RealOne player.)

By default the Windows Media Player is not in your toolbox (although in the labs it is likely that somebody has already added it to the toolbox before you).  If it is not, then you must add it as per the instructions in Chapter 3 of the book.  The icon for the control is a movie director’s clapboard.

Once you add the control to your form, all you need is code somewhere (on a command button for instance) to set the filename property of the MediaPlayer to the location of a valid multimedia file.  For example:

mpSound.URL = “c:\winnt\clock.avi”

 [assuming that you have changed the “(Name)” property of the Windows Media Player from “AxMediaPlayer1” to “mpSound”]

Any time the filename property is changed in the MediaPlayer, then that file will automatically play.  This is because the autostart property of the Windows Media Player is set to “True” by default.

Application.StartUpPath (dynamic file location)

Not everyone has a file named "clock.avi" on their c: drive in a folder called "winnt" (see example above).  This could pose a problem to making your programs work on other machines.  When you distribute your program you will want to make sure every necessary piece gets installed but you also usually need to give people the option to install your program wherever they like (my office computer, for instance, does not even have a "c" drive . . . my main hard-drive is called "g".)

The way that you can insure that all the pieces of your program are where you expect them to be -- regardless of where somebody installs the main program -- is to use relative paths.  A relative path differs from a hard-coded path in that the relative path is much more flexible.  With a relative path you can just say in effect "I don't care where this program is I just want to find the folder called 'Whatever' that should have been installed with it and play file 'Whaddyacallit.mp3'."

The easiest way to use a relative path in your code is to use Application.StartupPath.  This tells the computer to fill in the blank as far as directories go and put in the directory that your program is actually running from when the program finally gets run.  For instance, with our example above of playing 'clock.avi' we could make sure our installation program (which we will learn how to make later in the semester) installs a directory called "Videos" wherever our program gets installed.  And we could ensure that 'clock.avi' gets installed into that Videos directory.  Then we can change our code:

mpSound.URL = “c:\winnt\clock.avi” needs to be changed to

mpSound.URL = Application.StartupPath & “\Videos\clock.avi” [Don't forget the initial '\' (backslash) inside the double-quotes]

**The StartUpPath when we are still creating our program is always the bin directory.

Windows Media Player Versions

There are some compatibility problems with working on your code on computers that have different versions of the Windows Media Player.

The lab computers have Media Player 10 (or possibly 11) which is the latest type of media player.   If you haven’t done your Windows updates, your home computer may have Windows Media Player 6, 7, 8, or 9.  According to Microsoft documentation, this shouldn't matter (and if you’re not writing code it won’t) but beginning with version 9 they broke their own rules and changed a number of significant object properties of the media player. 

For instance, the property "URL" is "Filename" in versions 6 – 8 so your code has to be written differently.  Curiously enough, however, once you compile your project it doesn't matter what specific properties your code refers to or what media player you used in the design process – your exe will work with all the versions of Windows Media Player . . . just don't try actually editing your code in Visual Studio on a machine with one of the old versions (before 9) of Windows Media Player.

The best solution is to update all your computers to the newest Windows Media Player. You can download it at http://www.microsoft.com/windows/windowsmedia/player/download/download.aspx