Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

Introduction

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)
{
    this.DriveView.Items.Add(Drive);
}

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:

this.CDAudioObject.Play();
this.CDAudioObject.Pause();
this.CDAudioObject.Stop();

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.

this.CDAudioObject.Next();
this.CDAudioObject.Previous();

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.

this.CDAudioObject.Eject();

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

Screenshot

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.

Conclusion

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. Smile | :)

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

Share

About the Author

Andrew Boisen
Software Developer
Australia Australia
No Biography provided

Comments and Discussions

 
QuestionHow I can detect opening cd tray? Pinmemberuelfox19-Nov-10 11:59 
QuestionIt skips a lot... Pinmemberforinti26-Jul-06 4:03 
QuestionHow to add GraphicControls.GraphicButton ??? Pinmemberricharding25-Aug-05 17:40 
QuestionNo Audio? Pinmemberlsenft9-Mar-04 16:47 
AnswerRe: No Audio? Pinmemberlsenft9-Mar-04 18:10 
Generalhey!!! Pinmemberl a u r e n21-Nov-03 18:08 
GeneralRe: hey!!! PinmemberRickard Andersson1822-Nov-03 5:26 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 21 Nov 2003
Article Copyright 2003 by Andrew Boisen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid