InvalidOperationException Is Thrown by PlayToReceiver’s StartAsync Method

November 27, 2012

tags: , ,
no comments

While I was playing around with the new Windows Store Play-To contract, I came across a strange exception when I used the PlayToReceiver class to register my app as a Play-To target. Here is the relevant code:

   1: protected async override void OnNavigatedTo(NavigationEventArgs e)

   2: {

   3:     if (m_playToReceiver != null) 

   4:         return;

   5:  

   6:     m_playToReceiver = new PlayToReceiver

   7:                            {

   8:                                FriendlyName = "Video Receiver Sample",

   9:                                SupportsVideo = true,

  10:                                SupportsImage = false,

  11:                                SupportsAudio = false

  12:                            };

  13:  

  14:     m_playToReceiver.SourceChangeRequested += OnSourceChangeRequested;

  15:     m_playToReceiver.PlayRequested += OnPlayRequested;

  16:     m_playToReceiver.PauseRequested += OnPauseRequested;

  17:     m_playToReceiver.StopRequested += OnStopRequested;

  18:  

  19:     await m_playToReceiver.StartAsync();

  20: }

The exception was thrown by the StartAsync method and was far from being informative.

StartAsync InvalidOperationException

An unexpected time? Really? Can you be vaguer? The MSDN documentation for the StartAsync method did not help either, and there was not an inner exception with more detailed information.

After I scratched my head for a while, I noticed that in all the official Play-To samples (like this one), event handlers are added to all PlayToReceiver’s events, prior to the call to StartAsync. As you can see in the code above, in my elementary sample I wanted to get notifications for the Play, Pause, and Stop events only. At that stage, I was willing to do anything, so I added the missing event handlers and tried again:

   1: protected async override void OnNavigatedTo(NavigationEventArgs e)

   2: {

   3:     if (m_playToReceiver != null) 

   4:         return;

   5:  

   6:     m_playToReceiver = new PlayToReceiver

   7:                            {

   8:                                FriendlyName = "Video Receiver Sample",

   9:                                SupportsVideo = true,

  10:                                SupportsImage = false,

  11:                                SupportsAudio = false

  12:                            };

  13:  

  14:     m_playToReceiver.CurrentTimeChangeRequested += OnCurrentTimeChangeRequested;

  15:     m_playToReceiver.MuteChangeRequested += OnMuteChangeRequested;

  16:     m_playToReceiver.PlayRequested += OnPlayRequested;

  17:     m_playToReceiver.PauseRequested += OnPauseRequested;

  18:     m_playToReceiver.PlaybackRateChangeRequested += OnPlaybackRateChangeRequested;

  19:     m_playToReceiver.SourceChangeRequested += OnSourceChangeRequested;

  20:     m_playToReceiver.StopRequested += OnStopRequested;

  21:     m_playToReceiver.TimeUpdateRequested += OnTimeUpdateRequested;

  22:     m_playToReceiver.VolumeChangeRequested += OnVolumeChangeRequested;

  23:  

  24:     await m_playToReceiver.StartAsync();

  25: }

 To my surprise, adding the missing event handlers solved the problem, and the InvalidOperationException was not thrown again.

After giving the issue some thought, it actually seems reasonable to throw an exception when not all event handlers are attached. After all, the app is used as a media streaming target, and as such, it must respond to all the playback operations that are raised by the source app. failure to do so will provide a poor user experience. That said, the thrown exception could provide a more useful information.

Cross-Posted from http://www.programmingtidbits.com/post/2012/11/25/InvalidOperationException-Is-Thrown-by-PlayToReceivers-StartAsync-Method.aspx

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*