Question about UWP MediaPlaybackList events

67 views Asked by At

(Update: I suspect now this is a threading/race issue. When I create an event handler on the UI thread instead, I receive different results. I've edited my question to reflect this.)

I have a MediaPlaybackList populated with MediaPlaybackItems created from audio streams. All my MediaPlaybackItems consist of a single track. I'm interested in responding to when the current playback item changes. The docs indicate the right approach is to handle MediaPlaybackList::CurrentItemChanged events.

Here's my approach:

// SomeClass.h
using namespace winrt::Windows::Media::Playback;

class SomeClass
{
public:
    SomeClass();
    ...
    void OnPlaybackItemChanged(MediaPlaybackList const& sender,
                               CurrentMediaPlaybackItemChangedEventArgs const& args);
   ...
private:
    MediaPlayer m_MediaPlayer;
    MediaPlaybackList m_Playlist;
};

// SomeClass.cpp
SomeClass::SomeClass()
{
    m_Playlist.CurrentItemChanged({ this, &SomeClass::OnPlaybackItemChanged });
    m_MediaPlayer.Source(m_Playlist);
}

I append items to the playlist like so:

MediaPlaybackItem item(MediaSource::CreateFromStream(stream, stream.ContentType()));
m_Playlist.Items().Append(item);

My event handlers create a local copy of the args parameter and then break so that I can inspect its value in the debugger:

auto args_copy = args;
; // Breakpoint here

The streams play, and the CurrentItemChanged event fires. However, when I inspect the args.Reason() property from an event handler on the source thread, the only value I receive is InitialItem, apparently on each item change. Yet when I register a handler located on the UI thread, I receive a single InitialItem value, followed by EndOfStream values on each item change (which seems more in line with what one might expect).

For the life of me, I can't figure out why I'm getting different values passed to my event handlers depending on what thread they're on. The debugger is throwing no exceptions, and the event handlers are identical.

Questions:

  1. Why might otherwise identical event handlers get different arguments depending on their thread?
  2. At what points should I expect to see the different args.Reason() values? In particular, should there be an InitialItem event and a EndOfStream event for each item in the playlist? (I.e, does the "initial" of InitialItem refer to individual tracks inside the MediaPlaybackItems?)

Thanks in advance for your time.

0

There are 0 answers