Pro-Tip: How To Stop/Start Background Streaming Audio In Windows 8

29 October 2012

In my spare time, I've been working on a little online radio streaming app for Windows 8, based on the sample project from Microsoft. What I very quickly discovered was that calling .Stop() and .Pause() on MediaElements that were reading from streams didn't actually do anything.

My first cut at a solution was to set the MediaElement's source to null, and then recreate the source in lieu of Stop / Start, and it seemed to go okay - except that it subsequently failed certification. The tester notes weren't particularly clear about the exact cause of failure, but my guess was that it took far too long for the MediaElement to restart the radio stream when working in this fashion (especially from the inbuilt Windows 8 Media Controls UI, where I can't display a loading indicator)

A bit of Bing / Google led me to this post which gave me the final solution: while Stop and Start don't really work, PlaybackRate does - so setting MediaElement.PlaybackRate = 0 equates to a stop, and PlaybackRate = 1 as start. Using this instead of resetting the Source meant that my radio stream went far snappier, and fingers crossed I'll have my second Windows 8 application in the marketplace soon!

Just as a sidenote, since we're faking the condition of the MediaElement (it'll always report its state as Playing when you query it once you have it going and stop/start with the PlaybackRate), you'll want to catch the MediaControlPlayPauseTogglePressed event and change the MediaControl.IsPlaying property directly, to indicate "playing" and "not playing".

And as a second sidenote, this is actually the second time my app failed certification - the first time was due to the strange requirement for every application that has network connectivity to have a Privacy Policy...

Tags: MediaElement, radio, streaming, Windows 8

Add a Comment

No Comments