Dumbo
Dumbo

Reputation: 14102

Playing a sound effect in windows phone 7.1 by tapping a canvas

I am making a simple app using windows phone sdk 7.1, I need to play a short sound effect whenever the user taps a canvas. How can I add this future in the Tap event of the canvas or any other control?

All I know is the Uri to the file in the project folder:

"/TestApp;component/Resources/Untitled.wma"

Upvotes: 0

Views: 473

Answers (4)

George Nikolaides
George Nikolaides

Reputation: 1386

I find the following solution to be best in order to play sound notifications. It also plays .WMA files.

public static void PlayMessageFailedSound()
    {
        var s = Song.FromUri("MessageFailed", new Uri(@"Resources/Alert_nudge.wma", UriKind.Relative));
        FrameworkDispatcher.Update();
        MediaPlayer.Play(s);
    }

Upvotes: 1

Igor Kulman
Igor Kulman

Reputation: 16361

The easiest way is to use the SoundEffectPlayer from PhoneyTools. Declare it like this in your class

SoundEffectPlayer _player = null;

Initialize it with

var resource = Application.GetResourceStream(new Uri("/TestApp;component/Resources/Untitled.wma", UriKind.Relative));
var effect = SoundEffect.FromStream(resource.Stream);
_player = new SoundEffectPlayer(effect);

and then simply call

_player.Play();

Upvotes: 2

Dumbo
Dumbo

Reputation: 14102

Well I figured out the correct way to be:

private void PlayDuuuu()
{
    StreamResourceInfo stream = Application.GetResourceStream(new Uri("/AppName;component/Untitled.wav", UriKind.Relative));
    SoundEffect soundeffect = SoundEffect.FromStream(stream.Stream);
    SoundEffectInstance soundInstance = soundeffect.CreateInstance();
    FrameworkDispatcher.Update();
    soundInstance.Play();
}

Also I found out that the sound file can not be in WMA format...

Upvotes: -1

allocator
allocator

Reputation: 91

You could also add a media element control, set it's source property to you file and simply call the player.Play() in tap event handler

Upvotes: 0

Related Questions