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.
Preface
Index
Introduction to Computers, the Internet and Visual C#
Introduction to the Visual C# 2005 Express Edition IDE
Introduction to C# Applications
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Exception Handling
Graphical User Interface Concepts: Part 1
Graphical User Interface Concepts: Part 2
Multithreading
Strings, Characters and Regular Expressions
Graphics and Multimedia
Files and Streams
Extensible Markup Language (XML)
Database, SQL and ADO.NET
ASP.NET 2.0, Web Forms and Web Controls
Web Services
Networking: Streams-Based Sockets and Datagrams
Searching and Sorting
Data Structures
Generics
Collections
Appendix A. Operator Precedence Chart
Appendix B. Number Systems
Appendix C. Using the Visual Studio 2005 Debugger
Appendix D. ASCII Character Set
Appendix E. Unicode®
Appendix F. Introduction to XHTML: Part 1
Appendix G. Introduction to XHTML: Part 2
Appendix H. HTML/XHTML Special Characters
Appendix I. HTML/XHTML Colors
Appendix J. ATM Case Study Code
Appendix K. UML 2: Additional Diagram Types
Appendix L. Simple Types
Index