Windows Media Player
The Windows Media Player control enables an application to play video and sound in many multimedia formats. These include MPEG (Motion Pictures Experts Group) audio and video, AVI (audio-video interleave) video, WAV (Windows wave-file format) audio and MIDI (Musical Instrument Digital Interface) audio. Users can find pre-existing audio and video on the Internet, or they can create their own files, using available sound and graphics packages.
The application in Fig. 17.27 demonstrates the Windows Media Player control. To use this control, you must add the control to the Toolbox. First select Tools > Choose Toolbox Items... to display the Choose Toolbox Items dialog. Click the COM components tab, then scroll down and select the option Windows Media Player. Click the OK button to dismiss the dialog. The Windows Media Player control now appears at the bottom of the Toolbox.
Figure 17.27. Windows Media Player demonstration.
1 // Fig. 17.27: MediaPlayerTest.cs
2 // Windows Media Player control used to play media files.
3 using System;
4 using System.Windows.Forms;
5
6 public partial class MediaPlayer : Form
7 {
8 // default constructor
9 public MediaPlayer()
10 {
11 InitializeComponent();
12 } // end constructor
13
14 // open new media file in Windows Media Player
15 private void openItem_Click( object sender, EventArgs e )
16 {
17 openMediaFileDialog.ShowDialog();
18
19 // load and play the media clip
20 player.URL = openMediaFileDialog.FileName;
21 } // end method openItem_Click
22
23 // exit program when exit menu item is clicked
24 private void exitItem_Click( object sender, EventArgs e )
25 {
26 Application.Exit();
27 } // end method exitItem_Click
28 } // end class MediaPlayer
|
The Windows Media Player control provides several buttons that allow the user to play the current file, pause, stop, play the previous file, rewind, forward and play the next file. The control also includes a volume control and trackbars to select a specific position in the media file.
Our application provides a File menu containing the Open and Exit menu items. When a user chooses Open from the File menu, event handler openItem_Click (lines 1521) executes. An OpenFileDialog box displays (line 17) to allow the user to select a file. The program then sets the URL property of the player (the Windows Media Player control object of type AxMediaPlayer) to the name of the file chosen by the user. The URL property specifies the file that Windows Media Player is currently using.
The exitItem_Click event handler (lines 2427) executes when the user selects Exit from the File menu. This event handler simply calls Application.Exit to terminate the application. We provide sample audio and video files in the directory that contains this example.