×
Namespaces

Variants
Actions
(Difference between revisions)

How to embed and play a .wav file in Windows Phone 7 app

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Minor subedit)
hamishwillee (Talk | contribs)
m (Hamishwillee -)
Line 1: Line 1:
[[Category:Windows Phone]][[Category:Audio]]
+
[[Category:Windows Phone]][[Category:Audio]][[Category:Code Snippet]]
 
{{Abstract|This article describes how to embed a WAV file into the WP7 app resources and play it.}}
 
{{Abstract|This article describes how to embed a WAV file into the WP7 app resources and play it.}}
  
Line 12: Line 12:
 
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
+
|keywords= StreamResourceInfo, SoundEffect, SoundEffectInstance
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->

Revision as of 07:39, 11 September 2012

This article describes how to embed a WAV file into the WP7 app resources and play it.

WP Metro Icon File.png
WP Metro Icon Multimedia.png
SignpostIcon XAML 40.png
SignpostIcon WP7 70px.png
Article Metadata
Tested with
SDK: Windows Phone 7.1
Compatibility
Platform(s): Windows Phone 7.5
Windows Phone 7.5
Article
Keywords: StreamResourceInfo, SoundEffect, SoundEffectInstance
Created: Vaishali Rawat (05 Sep 2012)
Last edited: hamishwillee (11 Sep 2012)

Getting started

First create a Windows Phone application.

  • Open Visual Studio and select Windows Phone Application from the installed templates. Here I have named the application "Age Calculator".
  • Select Windows Phone 7.1 as the Target Version.
  • Right-click on the “References” in the project and click “Add Reference…”. Find the “Microsoft.Xna.Framework” reference and add it to the project.
    Adding a reference to a project
    Selecting a particular framework reference

Adding the WAV file to project resources

Next add the WAV File to the project resources:

  • Right-click on the project and add new folder. Name the folder - here I have used the name "Sound".
  • Right-click on the Sound folder and select Add existing items. Add the WAV file to this folder.
    AddFile.png
    AddWavFile.png
  • After adding WAV file select the file and right-click and open Properties and change the “Build Action” to “Resource”
    BuildAction.png

Code to play embedded WAV file

  • Add the following namespace to the MainPage.xaml.cs file
    using System.Windows.Resources;
    using Microsoft.Xna.Framework;
  • Code to play the .wav file
    void Play_wavFile(object sender, EventArgs e)
    {
    StreamResourceInfo _stream = Application.GetResourceStream(new Uri("/AgeCalculator;component/Sound/BirthdayTune.wav", UriKind.Relative));
    SoundEffect _soundeffect = SoundEffect.FromStream(_stream.Stream);
    SoundEffectInstance soundInstance = _soundeffect.CreateInstance();
    soundInstance.Play();
    }
  • To play the wav file on a button press
    Private void button1_Click(object sender, RoutedEventArgs e)
    {
    Play_wavFile();
    }
  • Now build the project and run it using F5.
163 page views in the last 30 days.
×