views:

97

answers:

1

Unable to understand the difference bettwen GoToPlaylistItem and GoToPlaylistItemOnNextTick, though GoToPlaylistItemOnNextTick clicked on scenarios where GoToPlaylistItem din't work.

If you wonder if there are any differences, Have a look at this Post for a problem solved by using GoToPlaylistItemOnNextTick while it was throwing null exception with GoToPlaylistItem

While I naviaged to the defination I got the following details. Could some one explain?

[ScriptableMember]
public virtual void GoToPlaylistItem(int playlistItemIndex);
public void GoToPlaylistItemOnNextTick(int playlistItemIndex);
+1  A: 

MediaPlayer uses a Timer internally. This timer is created in a protected method called CreatePositionTimer:

protected void CreatePositionTimer(TimeSpan interval)
{
    if (m_timer == null)
    {
        m_timer = new DispatcherTimer();
        m_timer.Interval = interval; // 6 NTSC frames
        m_timer.Tick += new EventHandler(OnTimerTick);
    }
}

The method GoToPlaylistItemOnNextTick simply sets a few internal variables:

public void GoToPlaylistItemOnNextTick(int playlistItemIndex)
{
    if (!m_goToItemOnNextTick) // don't set it if already set
    {
        m_goToItemOnNextTick = true;
        m_goToItemOnNextTickIndex = playlistItemIndex;
    }
}

The next time the timer comes around, OnTimerTick is called, and this checks for the above variables and then calls GoToPlaylistItem:

void OnTimerTick(object sender, EventArgs e)
{
    [...]

    if (m_goToItemOnNextTick)
    {
        m_goToItemOnNextTick = false;
        GoToPlaylistItem(m_goToItemOnNextTickIndex);
    }

    [...]
}

So the difference is that GoToPlaylistItem will go to the next playlist item immediately, while GoToPlaylistItemOnNextTick will do it at the next timer tick. The specific timer it uses is System.Windows.Threading.DispatcherTimer. This ensures that GoToPlaylistItem will be called when the UI thread is idle.

The difference may be significant if you rely on some of the events that MediaPlayer fires, for example StateChanged. If you call GoToPlaylistItem, this event will execute immediately before GoToPlaylistItem returns. If you call GoToPlaylistItemOnNextTick, then the event will only occur later when your current method has finished and the UI thread is idle.

Timwi
@Timwi, Thanks . Good to know what this was happenin?But why GoToPlaylistItem sometimes gives a null reference error while GoToPlaylistItemOnNextTick works. Link to the [Post](http://stackoverflow.com/questions/2873359/null-reference-exception-while-navigating-to-playlistitem "Null reference exception While Navigating to PlayListItem.")
Subhen
His code calls something called `setPlayList()` in the same method. Obviously I can’t know what it does, but if it tries to make changes to the playlist (which is likely), it may be the case that the MediaPlayer needs a chance to update the playlist first before it can jump to an item in it.
Timwi