MediaElement' s methods (Play, Pause, Stop) in Windows 8 do not work for Live Streaming - StudentGuru Larissas - Student Groups - StudentGuru

MediaElement' s methods (Play, Pause, Stop) in Windows 8 do not work for Live Streaming

Recently I started developing an application for a Radio Station. After downloading a few samples I started familiarizing myself with the MediaElements methods and properties which as expected are mainly Start(), Pause() and Stop().

The samples demonstrated MediaElement playing local mp3 files. And everything was working perfectly right up to the moment I changed the Source!

The MediaElement looked something like this

<MediaElement Visibility="Visible" Name="mediaplayer" AudioCategory="BackgroundCapableMedia" AutoPlay="False" RealTimePlayback="True" AudioDeviceType="Multimedia" Source="link"/>

When I changed the Source to the radio station’ s media server I started the app and MediaElement loading the source just fine and streaming started! But then I tried to hit pause! To my surprise Pause did not work and neither did Stop! But wait how can that be….? I tried then just a minute ago and everything was fine.

After a lot of searching I discovered that since the stream is live you cannot Pause or Stop the stream. Instead you have to change the PlaybackRate! You can set the MediaElement’ s PlaybackRate property to zero to simulate the Pause or Stop methods and to one to simulate the Play method!

Sample usage:

 private void play_Tapped(object sender, TappedRoutedEventArgs e)
            mediaplayer.PlaybackRate = 1;

        private void pause_Tapped(object sender, TappedRoutedEventArgs e)
            mediaplayer.PlaybackRate = 0;

I hope this was useful and saved you from a lot of trouble and time! Happy coding!