Audio Video Playback


The AudioVideoPlayback application programming interface (API) provides for basic playback and simple control of audio and video files. For more information, see the Microsoft.DirectX.AudioVideoPlayback managed code reference documentation.

Use the Video class to play video files, including those that contain audio. Use the Audio class to play audio-only files. You can also use the Audio class to control the audio properties when you play a video file. The SeekPositionFlags enumeration controls seek operations, and the StateFlags enumeration has flags to denote whether the media file is running, paused, or stopped.

Note: The Audio class is primarily designed for very simple playback scenarios, or for use with the Video class. You can also use Microsoft DirectSound to play audio files, which gives you much greater control over the audio playback.

Playing a Video File

To play a video file, start by creating an instance of the Video class. You can specify the file name in the Video constructor as in the following C# code example, or else call the Open method with the file name.

[C#]
using Microsoft.DirectX.AudioVideoPlayback; public class MyVideoPlayer : System.Windows.Forms.Form { /* ... */ private void OpenFile() { try { Video ourVideo = new Video("C:\\Example.avi"); /* ... */ } } /* ... */ }

The Video object throws an exception if you try to open a file that contains no video. Next, specify a parent window in your application to hold the Video object's video window, as follows:

[C#]
ourVideo.Owner = this; // 'this' refers to the application's Form object.

Control playback by calling the Play, Pause, and Stop methods. For example, the following event handler stops playback.

[C#]
private void mnuStop_Click(object sender, System.EventArgs e) { if (ourVideo != null) { ourVideo.Stop(); } }

To set the size of the playback window, set the Size property, which takes a System.Drawing.Size Leave Site object, as follows:

[C#]
ourVideo.Size = new Size(480, 320);

You can get the native video size by examining the DefaultSize property. If the video file contains audio, the Video.Audio property returns an Audio object. You can use this object to set the volume or the stereo balance for the audio. If the file does not contain audio, setting these properties causes an exception. Use a try block around the code, as follows:

[C#]
try { Video.Audio.Volume = 100; }

Playing an Audio File

The Audio object is similar to the Video object, but it supports properties that relate to audio, such as Volume and Balance. To play an audio file, specify the file name in the Audio constructor, as in the following C# code example, or call the Open method with the file name.

[C#]
Audio ourAudio = new Audio("C:\MyAudioFile.wav");


Send comments about this topic to Microsoft. © Microsoft Corporation. All rights reserved.

Feedback? Please provide us with your comments on this topic.
For more help, visit the DirectX Developer Center