Click here to Skip to main content
13,190,404 members (50,040 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 20 Nov 2003

CD Player library using MCI

, 20 Nov 2003
Rate this:
Please Sign up or sign in to vote.
Wrapping the Multimedia Control Interface (MCI) through interop to provide a simple library to play CDs.


This project was inspired by an obscure piece of VB.NET code I found one day on a forum (and never saw again). This code wrapped MCI and enabled you to select a specific track from a CD and play it. The class design was already established, but the functionality was very limited and flawed in some places. I rewrote the class design in C# and added functionality to give access to all the necessary features you would expect in a comprehensive library. I would like to give credit to the authors of the original VB.NET class, but sadly don't know who they are.

Using the DeviceController.CDDevice class

First add a reference to the DeviceController.dll, and then include the DeviceController namespace.

using DeviceController;

Instantiate the CDDevice object like so...

CDDevice CDAudioObject = new CDDevice();

Now we can really get started. To set the drive device to read from, you simply use a statement like this, where 0 is the first device available:

this.CDAudioObject.ChangeDrive( 0 );

To encapsulate the task of detecting drive devices, I've included a property that gets an ArrayList of all the available devices that the class library can use to play CDs. You would use it like this in your interface...

foreach(string Drive in CDAudioObject.Drives)

The next thing you will probably want to do is detect how many tracks are on the CD.

int tracks = this.CDAudioObject.TotalTracks;

The class assigns the first track on the CD as the current track, as soon as you select a device with a CD in it. So, as soon as you select a drive, you can start playing. To play, pause or stop a track from playing, simply use the following method calls:


Likewise, you can call methods to move forward or backward through the tracks. If you try to move past the limits of the track range of the CD, the method call will simply do nothing.


To physically open and close the currently selected device, you can call the Eject() method. However, I couldn't find a way to detect if the drive is already open or closed, so the class library assumes that the drive is closed when it is first instantiated. Therefore, if the drive is open, you have to call this method twice to close the drive, and from then on, the object is synched to the drive.


So that's the core functionality.

I've also included a range of properties to access information about the length, current position etc., for CDs and their tracks. It's worth noting that I've doubled up on some of these properties, providing a property that gives a formatted string (such as TrackDuration), and then another property that gives an integer value in seconds (such as TrackDurationSeconds). This is just to make things easy for an interface to display these values without having to worry about formatting, while still providing functionality for other purposes. The following is a list of properties that developers will find useful, the names are fairly self-describing.

  • int CurrentDrive
  • int TotalTracks
  • int CurrentTrack
  • int TrackDurationSeconds
  • int TrackPositionSeconds
  • string TrackDuration
  • string TrackPosition
  • ArrayList Drives
  • PlayStatus Status
  • bool CDAvailable


Demonstration Application

I've written a demonstration app that implements pretty much every piece of functionality from the class library. I think it works pretty well, but don't be too critical of it. It's mainly to provide an example of how you could use the different aspects of the class library.


Hopefully this class can be of use to developers, if only as a base to implement functionality on top of. It'll probably be redundant in a couple of years when Longhorn gets released anyway. I was looking at the GAC assemblies that are packaged with LongHorn the other day, and there are a range of assemblies dealing solely with multimedia and audio. Something that's been missing from the .NET framework since it was released. Can't wait. :-)


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

Andrew Boisen
Software Developer
Australia Australia
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionHow I can detect opening cd tray? Pin
uelfox19-Nov-10 11:59
memberuelfox19-Nov-10 11:59 
QuestionIt skips a lot... Pin
forinti26-Jul-06 4:03
memberforinti26-Jul-06 4:03 
QuestionHow to add GraphicControls.GraphicButton ??? Pin
richarding25-Aug-05 17:40
memberricharding25-Aug-05 17:40 
QuestionNo Audio? Pin
lsenft9-Mar-04 16:47
memberlsenft9-Mar-04 16:47 
AnswerRe: No Audio? Pin
lsenft9-Mar-04 18:10
memberlsenft9-Mar-04 18:10 
Generalhey!!! Pin
l a u r e n21-Nov-03 18:08
memberl a u r e n21-Nov-03 18:08 
GeneralRe: hey!!! Pin
Rickard Andersson1822-Nov-03 5:26
memberRickard Andersson1822-Nov-03 5:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171016.2 | Last Updated 21 Nov 2003
Article Copyright 2003 by Andrew Boisen
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid