Audio Playback in Windows Phone 7 Silverlight

Source: Internet
Author: User

When working on a WP7 Silverlight project, you need to implement sound playback. I have referenced many open-source projects and found that they all reference a DLL that belongs to the xNa framework when implementing this function, this was a strange idea at the beginning, so I implemented it in different ways. I also think that the Silverlight framework on WP7 does not have the sound playback function. Considering that the xNa and SL sound playback libraries should be avoided, WP7 developers only encapsulate the sound playback interface in xNa. I took it for granted.

Now let's talk about howProgramUse the xNa interface to play audio. First, we need to add a reference to Microsoft. xNa. Framework. dll in the project.

After adding a referenceCodeUse Using to add the following two namespaces at the top of the file:

 

Using Microsoft. xNa. Framework. Audio;
Using Microsoft. xNa. Framework;

 

This class only supports wavelet files. If you use the audio file format, you need to use a conversion tool to convert it to WAV format. There are a lot of such free tools on the Internet.

 

Add the WAV audio file to the project. We can use the titlecontainer. openstream () method to convert a WAV file into a stream object. Then, you can use the previous Stream object to create a soundeffect object to play the sound. The Code is as follows:

 

Stream stream = titlecontainer. openstream ("sounds/crickets.wav"); soundeffect effect = soundeffect. fromstream (Stream); frameworkdispatcher. Update (); effect. Play ();

In general, a file in the form of waves is much larger than a compressed file. In fact, there is an interface for playing audio files in the Silverlight framework of WP7, Which is mediaelement. Once you have created a mediaelement object, make sure the object has been added to the Silverlight tree. If mediaelement is not declared as a static file, the MP3 file will not be loaded.

In addition, when you deploy a program containing mediaelement to a real device through vs2010 and Zune software, if the connection between the mobile phone and Zune software is not closed before playing the audio file, unfortunately, an error is reported when the application loads the audio file. But running the program on the simulator is normal.

The following code uses mediaelement to demonstrate how to play a sound:

Mainpage. XAML:

 

<Mediaelement X: Name = "mainme" autoplay = "false" mediaopened = "mainme_mediaopened" mediafailed = "mainme_mediafailed"> </mediaelement>

 

Mainpage. XAML. CS:

 

Private void soundbtn_click (Object sender, routedeventargs e) {mainme. Source = new uri ("sounds/mysound.pdf", urikind. Relative); mainme. Position = new timespan (0 );}

 

Private void mainme_mediaopened (Object sender, routedeventargs e) {mainme. Play ();}

 

Here we will not download the specific project file. You can verify it by yourself.

 

Related Article

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.