Sound of Music-. NET Framework 2.0

Source: Internet
Author: User

Based on http://msdn.microsoft.com/en-us/magazine/cc161162.aspxand msdn

You strongly want to add an exciting "Funky Cold Medina" to the pop-up screen of your application (to make those of you not after 80 understand, I will explain that this is the song of Tone Loc ). Before Visual Studio 2005, adding the simplest tunes and system sounds to your applications was a big challenge. However, Microsoft. NET Framework 2.0 adds many new classes and namespaces to help you implement these functions. Let's take a look at one of them: System. Media namespace.

First, we start with a simple beep in 1960s. You can simply use the SystemSound class. The SystemSound class has five common attributes: Asterisk, Beep, Exclamation, Hand, and Question. Each attribute returns a type of system sound and exposes a Play method. Therefore, to make the computer sound, use System. Media. SystemSounds. Beep. Play. (That's it, But I tried it and at least I couldn't make a sound in Win7... It should be the reason why the API is too old ...)

Want to play a more complex sound? In system.media, you can find a soundplayerclass to control the playback of .wav files. SoundPlayer supports playing files/URLs/streams or an embedded WAV resource.

To play a file, the first step is to load it. If you are playing from a stream or URL, You need to explicitly load the file. If you Play embedded resources or files, the file will be loaded using the Play method. Like many other. NET aspects, you need to have two options: Synchronous loading or asynchronous loading. It depends on your application needs.

If you load a wav file and it will be played immediately, and you are not sure that the file will be loaded when you call the Play method, you should choose the synchronization method. This method definitely prevents other further actions of your application on the main thread and locks the user interface during the loading phase.

Asynchronous mode is more effective for loading a slow resource, such as opening a URL or having a large file. You should call the asynchronous loading method early in your program execution. Unlike the synchronous method, the asynchronous method allows the application code to continue executing when your file is loaded. After the file is loaded, you will receive the LoadCompleted event, and then you can perform additional processing or other operations you want. In addition, you can view the IsLoadCompleted attribute at any time to determine the loading status. The disadvantage of this method is that getting events requires more code to make your code a little more complex.

Asynchronous example:

 

SoundPlayer player = new SoundPlayer ();
Player. SoundLocation = @ "F: \ th.wav ";
Player. LoadAsync ();
Player. Play ();

If you want to play it cyclically:
Player. PlayLooping ();

However, it is difficult to fully meet your needs to play wav files. If you want to play a wav file, you need to jump out of the. NET Framework. One option is to use Windows Media Player (you can use Media Player ). After installing the SDK and adding references to the WMP library, you can access more mainstream file formats, including mp3 and wma.

Playing files with WMP is also very simple:

Wmpplayer. URL = @ "F :\\ miko.mp3 ";
Wmpplayer. controls. play ();
Console. Read ();

In addition, the following two events are useful:

Player. PlayStateChange + =
New WMPLib. _ WMPOCXEvents_PlayStateChangeEventHandler (Player_PlayStateChange );
Player. MediaError + =
New WMPLib. _ WMPOCXEvents_MediaErrorEventHandler (Player_MediaError );

Private void Player_PlayStateChange (int NewState)
{
If (WMPLib. WMPPlayState) NewState = WMPLib. WMPPlayState. wmppsStopped)
{
This. Close ();
// Because there is no direct playloop method, you need to change it to wmpplayer. controls. play () for loop playback ();
}
}

Private void Player_MediaError (object pMediaObject)
{
MessageBox. Show ("Cannot play media file .");
This. Close ();
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.