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