C # Playing Sound and flash I. Playing Sound in C #
Create a C # Windows Form Project (Windows application) and define two menu buttons (menuItem1, menuItem2 ).
Select "Custom Toolbox (Add/Remove toolbox items)" in "Tools" in the menu. In the custom Toolbox window, click to expand the "COM component" item, select "Window Media Player. After confirmation, the "Windows Media Player" item will appear in the "toolbox", and then drag it to the Form to resize it, the system automatically adds a reference to this dll in "Reference". AxMediaPlayer is the Namespace and class we use.
Set some attributes of this control in the property bar. For convenience, here I set AutoStart to true (in fact, the default value is true), as long as FileName is set (the file is opened ), the file is automatically played. The complete code is as follows:
Private void menuItem1_Click (object sender, System. EventArgs e)
{
OpenFileDialog ofDialog = new OpenFileDialog ();
OfDialog. AddExtension = true;
OfDialog. CheckFileExists = true;
OfDialog. CheckPathExists = true;
// The next sentence must be in single line
OfDialog. Filter = "VCD file (*. dat) | *. dat | Audio file (*. avi) | *. avi
| WAV file (*. wav) | *. wav | MP3 file (*. mp3) | *. mp3 | all files (*. *) | *.*";
OfDialog. DefaultExt = "*. mp3 ";
If (ofDialog. ShowDialog () = DialogResult. OK)
{
// Version 2003: this. axMediaPlayer1.FileName = ofDialog. FileName;
This. axMediaPlayer1.URL = ofDialog. FileName; // usage 2005
}
}
Here we use a Microsoft player. You can also try the Winamp control. If you only need to play the sound without displaying it, you only need to set the Visible attribute of the AxMediaPlayer to false.
Ii. Playing Flash Animation
The principle of playing a Flash animation is similar to that of playing a sound, and the dll of Flash is also directly referenced. However, this dll cannot be directly found in the "COM component" window and needs to be manually added, select "Tools"> "Custom toolkit" from the menu, open the "Custom toolkit" window, click "Browse" in "COM components", and select "c: \ WINNT (WINDOWS) \ system32 \ MacromedFlashswflash. ocx control. After confirmation, we can see "FlashFactory" and "ShockwaveFlash" in the toolbox. We need to use "ShockwaveFlash" and drag it to the new Form, then you can set some attributes.
The following is a simple operation code to open the swf file and play it back:
Private void menuItem2_Click (object sender, System. EventArgs e)
{
OpenFileDialog ofDialog = new OpenFileDialog ();
OfDialog. AddExtension = true;
OfDialog. CheckFileExists = true;
OfDialog. CheckPathExists = true;
OfDialog. Filter = "swf file (*. swf) | *. swf | all files (*. *) | *.*";
OfDialog. DefaultExt = "mp3 ";
If (ofDialog. ShowDialog () = DialogResult. OK)
{
This. axShockwaveFlash1.Movie = ofDialog. FileName;
This. axShockwaveFlash1.Play ();
}
}