Browse DevX
Sign up for e-mail newsletters from DevX


Get Your Hands on Better Multimedia Control in .NET : Page 2

Playing audio and video is easier in version 2 of the framework, but the basic capabilities available still leave a lot to be desired. Fortunately, the free MediaPlayer component provided with this article demonstrates a more feature-rich solution to your multimedia needs—and it's compatible with all versions of the .NET Framework.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Play Time
Of course, the most fundamental functionality of the MediaPlayer component is to actually play the media file. Two overloaded versions of the Play method are provided, as shown in Listing 3.

The first Play method optionally accepts a file name to play. If no file name is provided, it uses the value of the FileName property that should have been set beforehand. After verifying a valid media file has been specified, the required MCI string is constructed and sent as a parameter to the mciSendString function. This causes the media to start playing.

The second Play method is intended for playing audio CDs. It accepts a parameter for the track number to be played, and another Boolean parameter that specifies whether it should stop after that track, or whether following tracks should continue to play once the first one is completed.

Solid audio CD support requires a few other helper methods (as shown below) to manage details such as opening and closing the CD door and verifying that an audio CD is present.

Public Function AudioCDPresent() As Boolean Dim ret As UInt32 Dim strReturn As String = New String( _ CChar(" "), 127) ret = mciSendString("status cd media present", _ strReturn, Len(strReturn), 0) strReturn = strReturn.Replace(Chr(0), _ Chr(32)).Trim.ToLower Return CBool(strReturn) End Function Public Sub OpenCDDoor() Dim retval As UInt32 Me.FileName = "cd" If Not m_Opened Then Me.Open() retval = mciSendString( _ "set cd door open", "", 0, 0) If retval.ToString <> "0" Then Debug.Write(GetMCIErrorString(retval)) End If End Sub Public Sub CloseCDDoor() Dim retval As UInt32 Me.FileName = "cd" If Not m_Opened Then Me.Open() retval = mciSendString( _ "set cd door closed", "", 0, 0) If retval.ToString <> "0" Then Debug.Write(GetMCIErrorString(retval)) End If End Sub

Another useful method the MediaPlayer component provides is the ability to retrieve information about the current state of any media file. The GetStatus method in Listing 4 can retrieve information about the length of the current CD, the number of tracks, the current position, track length, and the current play mode.

This function optionally accepts a track number parameter (for retrieving details about a specific track of an audio CD) and a MediaInfoOption enumeration value that specifies the kind of media information being requested. This enumeration is defined here, along with a few necessary private variable declarations.

#Region " variable declarations " Private m_Filename As String Private m_Opened As Boolean Private m_Type As String = String.Empty Public Enum MediaInfoOption playmode = 0 cdlength = 1 cdnumtracks = 2 cdcurrentposition = 3 cdtracklength = 4 End Enum #End Region

The final few methods provide less exciting functionality that is nonetheless necessary for any quality media component: The ability to Stop and Pause a track, and the ability to retrieve error details in the case of an unexpected exception. The following code shows the implementation details.

Public Sub Pause() If Me.FileName <> "" Then Dim retval As UInt32 retval = mciSendString( _ "pause " & Me.FileName, "", 0, 0) End If End Sub Public Sub [Stop]() If Me.FileName <> "" Then Dim retval As UInt32 retval = mciSendString( _ "stop " & Me.FileName, "", 0, 0) retval = mciSendString("seek " & Me.FileName & _ " to start", "", 0, 0) End If End Sub Private Function GetMCIErrorString( _ ByVal ErrorCode As UInt32) As String Dim buffer As String = Space(256) mciGetErrorString(ErrorCode, buffer, Len(buffer)) Return buffer.Substring(InStr(buffer, _ vbNullChar) - 1) End Function

Now that you have all the code for the MediaPlayer component, and have compiled it into a class library DLL, you can add it to your Visual Studio Toolbox by simply dragging it there from Windows Explorer.

Figure 1: The sample application demonstrates all you need to get started with the MediaPlayer component.
Then you can drag one or more of the MediaPlayer components onto any Windows Form. Using the component can be as easy as this:


Figure 1 shows a sample application that uses the MediaPlayer component. The component and sample application are both available for download.

What's New in Version 2?
Version 2 of the .NET Framework finally includes some basic support for playing audio files. For example, the following Visual Basic 2005 line of code plays a wave file:


Beta 2 of the .NET Framework version 2 suffers from some serious limitations that will hopefully be improved upon before the final release. For example, the Play method only supports PCM formatted wave files. The framework still lacks support for MP3s, WMA files, or video files of any kind. Therefore the MediaPlayer component included with this article remains a superior way to play media files in most ways (see Table 1).

Table 1: While version 2 of the .NET Framework (beta 2) provides handy support for playing audio streams and byte arrays, the MediaPlayer component outdoes the standard framework functionality in virtually every other way.


.NET Framework 2.0 Media Functionality

MediaPlayer Component

Supported File Types

WAV only (must be PCI Formatted)


Input Types

Files, Streams, Byte Arrays

Files only

Supported Functions

Synchronous, Asynchronous, Infinite Loop

Asynchronous, Pause, Stop, CD Audio (single or multiple tracks), Open/Close CD Door, Detect Audio CD, retrieve error details, retrieve track details & status

One capability the .NET Framework does provide that's beyond the capabilities of the MediaPlayer component is the ability to play a .wav file directly from stream (or byte array) instead of a file.

With My.Computer.Audio .Play(MyStream, AudioPlayMode.WaitToComplete) End With

In addition to the synchronous WaitToComplete option, the AudioPlayMode enumeration also allows you to play an audio file asynchronously or to loop the audio file infinitely.

That exhausts the new media capabilities of the .NET Framework version 2.0. As of beta 2, there is no support for pausing a currently playing audio file or for playing music CDs, and no support for retrieving any kind of information about a currently playing track, such as the remaining play time or total length of the track. Discouragingly, the Beta 2 functionality also lacks support for playing more than one media file at a time. However, the free MediaPlayer component included with this article has none of these limitations. And since the source code is included, if you do run into any limitations you can extend it to support virtually anything you need.

So, if your needs are simple, you might be able to scrape by with the new audio capabilities of the .NET Framework version 2.0, but otherwise, the MediaPlayer component described in this article provides a rich set of features—yet only scratches the surface of what the MCI can provide. You might choose to extend the control by adding in capabilities to record, fast forward & rewind, play videos within specific windows, etc. You might also turn the component into a control with a user interface of your choosing.

The source code for the MediaPlayer control and the associated sample application are available for download in Visual Studio 2003 format. You can use the MediaPlayer.dll from any version of the .NET Framework.

Steve C. Orr is a MCSD and a Microsoft MVP in ASP.NET. He�s been developing software solutions for leading companies in the Seattle area for more than a decade. When he�s not busy designing software systems or writing about them, he can often be found loitering at local user groups and habitually lurking in the ASP.NET newsgroup. Find out more about him at http://SteveOrr.net.
Thanks for your registration, follow us on our social networks to keep up-to-date