×
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
Vaishali Rawat (Talk | contribs)
(Vaishali Rawat - - Code Behind)
hamishwillee (Talk | contribs)
m (Hamishwillee -)
 
(15 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Draft]][[Category:Windows Phone]]
+
[[Category:Audio on Windows Phone]][[Category:Files/Data on Windows Phone]][[Category:XAML]][[Category:Code Snippet]]
{{Abstract|This article describes how to embed a WAV file into the WP7 app and play it later.}}
+
{{Abstract|This article describes how to embed a WAV file into the WP7 app resources and play it.}}
  
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
Line 7: Line 7:
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|sdk= Windows Phone 7.1
 
|sdk= Windows Phone 7.1
|platform= Windows Phone 7.5  
+
|platform= Windows Phone 7.5
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->  
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|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]] -->
|translated-from-title= <!-- Title only -->  
+
|translated-from-title= <!-- Title only -->
 
|translated-from-id= <!-- Id of translated revision -->
 
|translated-from-id= <!-- Id of translated revision -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|review-by= <!-- After re-review: [[User:username]] -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 
|creationdate= 20120905
 
|creationdate= 20120905
|author= [[Vaishali Rawat]]
+
|author= [[User:Vaishali Rawat]]
 
}}
 
}}
  
== Getting Started ==
+
== Getting started ==
* First let us create a Windows Phone Application. To create it, open Visual Studio and select Windows Phone Application from the installed templates.
+
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.
 
* 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.
 
* Right-click on the “References” in the project and click “Add Reference…”. Find the “Microsoft.Xna.Framework” reference and add it to the project.
[[File:AddReference.png|none]]
+
*: [[File:AddReference.png|none|frame|Adding a reference to a project|433x324px]]
[[File:xnaFramework.png|none]]
+
*: [[File:xnaFramework.png|none|frame|Selecting a particular framework reference|460x390px]]
  
== Embedding WAV File ==
+
== Adding the WAV file to project resources ==
* Let's  Add WAV File.To do this right-click on the project and add new folder. Give desired name to this folder. Here, I am naming it as Sound.
+
Next add the WAV File to the project resources:
* Right-click on the Sound folder and add existing items. Add WAV file to this folder.  
+
* Right-click on the project and add new folder. Name the folder - here I have used the name "Sound".
[[File:AddWavFile.png|none]]
+
* Right-click on the Sound folder and select ''Add existing items''. Add the WAV file to this folder.  
 +
*: [[File:AddFile.png|none]]
 +
*: [[File:AddWavFile.png|none]]
 
* After adding WAV file select the file and right-click and open Properties and change the “Build Action” to “Resource”
 
* After adding WAV file select the file and right-click and open Properties and change the “Build Action” to “Resource”
[[File:BuildAction.png|none]]
+
*: [[File:BuildAction.png|none]]
  
== Code Behind ==
+
== Code to play embedded WAV file==
* Add the following namespace to the MainPage.xaml.cs file
+
* Add the following namespace to the '''MainPage.xaml.cs''' file
<code csharp>
+
*: <code csharp>
using System.Windows.Resources;
+
using System.Windows.Resources;
  using Microsoft.Xna.Framework;
+
using Microsoft.Xna.Framework;
 
</code >
 
</code >
 
 
* Code to play the .wav file  
 
* Code to play the .wav file  
<code>
+
*: <code csharp>
 
void Play_wavFile(object sender, EventArgs e)
 
void Play_wavFile(object sender, EventArgs e)
 
  {
 
  {
               StreamResourceInfo _stream = Application.GetResourceStream(new Uri("/MyProjectName;component/Sound/BirthdayTune.wav", UriKind.Relative));
+
               StreamResourceInfo _stream = Application.GetResourceStream(new Uri("/AgeCalculator;component/Sound/BirthdayTune.wav", UriKind.Relative));
                    SoundEffect _soundeffect = SoundEffect.FromStream(_stream.Stream);
+
              SoundEffect _soundeffect = SoundEffect.FromStream(_stream.Stream);
                    SoundEffectInstance soundInstance = _soundeffect.CreateInstance();
+
              SoundEffectInstance soundInstance = _soundeffect.CreateInstance();
                    soundInstance.Play();
+
              FrameworkDispatcher.Update();
 +
              soundInstance.Play();
 
}
 
}
 
</code>
 
</code>
+
* To play the wav file on a button press
To play the wav file on a button press
+
*: <code csharp>
<code>
+
 
Private void button1_Click(object sender, RoutedEventArgs e)
 
Private void button1_Click(object sender, RoutedEventArgs e)
 
   {
 
   {
Line 65: Line 67:
 
</code>
 
</code>
  
* Now build the project and run it using F5.
+
* Now build the project and run it using F5.  
 +
 
 +
On Button click, .wav sound file start playing.[[Category:Windows Phone 7.5]]

Latest revision as of 03:53, 29 June 2013

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 (29 Jun 2013)

[edit] 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

[edit] 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

[edit] 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();
    FrameworkDispatcher.Update();
    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.

On Button click, .wav sound file start playing.

This page was last modified on 29 June 2013, at 03:53.
201 page views in the last 30 days.
×