Dino
Dino

Reputation: 309

How to play a sound without creating a Media Element in Xaml (Windows phone 8)?

In My case I want to play a sound file from a C# class file. I refer the examples with create Media Element in the xaml page. I want to play a sound every 5 minutes. This process is behind of my application. I don't have a design page for this.

Upvotes: 1

Views: 1502

Answers (2)

Leo Chapiro
Leo Chapiro

Reputation: 13994

If your sound is a WAV file, try this:

Stream stream = TitleContainer.OpenStream("sounds/bonk.wav");
SoundEffect effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();

Update

Please, take into account that SoundEffect class lives in Microsoft.Xna.Framework.Audio namespace and is part of the XNA Framework Class Library, which is not supported on Windows Phone 8.1 Runtime apps (not Silverlight one). This means that if you are planning to upgrade/port your application to universal either for Windows Phone 8.1 (and upper) or Windows 8.1 (and upper), this answer will not work for you, unfortunately.

Upvotes: 2

Filip
Filip

Reputation: 1864

Simply declare your Media Element in code:

MediaElement element = new MediaElement();

Upvotes: 0

Related Questions