![]() |
Multimedia »
Audio and Video »
Audio
Intermediate
CD Player library using MCIBy Andrew BoisenWrapping the Multimedia Control Interface (MCI) through interop to provide a simple library to play CDs. |
C#, Windows, .NET 1.1VS.NET2003, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
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.
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 
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. :-)
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 20 Nov 2003 Editor: Smitha Vijayan |
Copyright 2003 by Andrew Boisen Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |