Playing Back an Audio or Video File Simply

In order to use the audio and video playback namespace, you will first need to add a reference to this namespace into your application. You may also want to include a using clause for the namespace in your code file.

Our goal when we designed these classes was to make them extremely simple to use, so if we wanted to play a file, it should be accomplished with just one line of code. If you wanted to play an audio file called "piano.mp3", let's say, the code would look like this:

 Audio someAudio = new Audio("piano.mp3", true); 

There are two main classes that are included in this namespace: the video class and the audio class. These names are pretty self-explanatory. Each class's constructor has two overloads; the first parameter of each overload is the name of the file you wish to play. The other constructor for each class contains a Boolean value that determines whether or not this file should begin playing automatically. The default option is false, and the file will not begin playing until you call Play on it.

As you can see by our call, we are creating an audio object, and we want it to begin playing automatically. For the simplest of cases, this is all that is required for you to do. Using the constructor without the Boolean parameter, or specifying false for the parameter when you call the constructor, will cause the file to be loaded, but it will not be played.

The constructors aren't the only way to create (or load) these files, though. The most similar method would be the static FromFile method, which takes the same parameters as the constructor does and returns a new instance of the class. There is also a static FromUrl method, which behaves exactly the same as the FromFile method, with the exception that it can be used to load the data from a Web site (or any valid URL). If possible, this data will be streamed from this URL to your system, so playback should begin much quicker than waiting for the entire file to be downloaded.

There are also Open and OpenUrl methods on the classes themselves that take the same parameters as their static counterparts. They will replace the data in an already created object with the data from the new file or URL you specify.



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net