private void HandlePropertyChange(MediaCenterEnvironment env, MediaExperience exp, MediaTransport transport, string property) { PlayState state; long positionTicks = 0; // If another application is playing the content, such as the WMC autoplay handler, we will // not have permission to access Transport properties // But we can look at MediaExperience.MediaType to determine if something is playing try { state = transport.PlayState; positionTicks = transport.Position.Ticks; } catch (InvalidOperationException) { Logger.ReportVerbose("HandlePropertyChange was not able to access MediaTransport. Defaulting values."); state = exp.MediaType == Microsoft.MediaCenter.Extensibility.MediaType.Unknown ? Microsoft.MediaCenter.PlayState.Undefined : Microsoft.MediaCenter.PlayState.Playing; } bool playstateChanged = state != _CurrentPlayState; _CurrentPlayState = state; // Determine if playback has stopped. Per MSDN documentation, Finished is no longer used with Windows 7 bool isStopped = state == Microsoft.MediaCenter.PlayState.Finished || state == Microsoft.MediaCenter.PlayState.Stopped || state == Microsoft.MediaCenter.PlayState.Undefined; // Don't get tripped up at the initial state of Stopped with position 0 if (!_HasStartedPlaying) { if (!isStopped) { Logger.ReportVerbose("HandlePropertyChange has recognized that playback has started"); _HasStartedPlaying = true; IsStreaming = Playable.CurrentFile.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || Playable.CurrentFile.StartsWith("https://", StringComparison.OrdinalIgnoreCase); if (Playable.HasMediaItems) { Application.CurrentInstance.CurrentlyPlayingItemId = lastItemId = Playable.CurrentMedia.Id; Application.CurrentInstance.ReportPlaybackStart(Playable.CurrentMedia.ApiId, IsStreaming); } } else { return; } } // protect against really agressive calls if (property == "Position") { Application.CurrentInstance.CurrentlyPlayingItem.CurrentPlaybackPosition = positionTicks; var diff = (DateTime.Now - _LastTransportUpdateTime).TotalMilliseconds; // Only cancel out Position reports if (diff < 5000 && diff >= 0) { return; } } _LastTransportUpdateTime = DateTime.Now; // Get metadata from player MediaMetadata metadata = exp.MediaMetadata; string metadataTitle = PlaybackControllerHelper.GetTitleOfCurrentlyPlayingMedia(metadata); long metadataDuration = PlaybackControllerHelper.GetDurationOfCurrentlyPlayingMedia(metadata); PlaybackStateEventArgs eventArgs = GetCurrentPlaybackState(metadataTitle, metadataDuration, positionTicks); // Only fire the progress handler while playback is still active, because once playback stops position will be reset to 0 OnProgress(eventArgs); if (eventArgs.Item != null && eventArgs.Item.HasMediaItems && eventArgs.Item.CurrentMedia.Id != lastItemId) { // started playing a new item - update Application.CurrentInstance.CurrentlyPlayingItemId = lastItemId = eventArgs.Item.MediaItems.ElementAt(eventArgs.CurrentMediaIndex).Id; } Application.CurrentInstance.ShowNowPlaying = eventArgs.Item == null || eventArgs.Item.ShowNowPlayingView; if (playstateChanged) { FirePropertyChanged("IsPaused"); if (state == Microsoft.MediaCenter.PlayState.Paused) { // allow screensavers/sleep Helper.AllowSleep(); } else if (state == Microsoft.MediaCenter.PlayState.Playing || state == Microsoft.MediaCenter.PlayState.Buffering) { // disallow again Helper.PreventSleep(); } // Get the title from the PlayableItem, if it's available. Otherwise use MediaMetadata string title = eventArgs.Item == null ? metadataTitle : (eventArgs.Item.HasMediaItems ? eventArgs.Item.MediaItems.ElementAt(eventArgs.CurrentMediaIndex).Name : eventArgs.Item.Files.ElementAt(eventArgs.CurrentFileIndex)); Logger.ReportVerbose("Playstate changed to {0} for {1}, PositionTicks:{2}, Playlist Index:{3}", state, title, positionTicks, eventArgs.CurrentFileIndex); //Logger.ReportVerbose("Refresh rate is {0}", DisplayUtil.GetCurrentRefreshRate()); PlayStateChanged(); Logger.ReportVerbose("Back from PlayStateChanged"); } if (isStopped) { Logger.ReportVerbose("Calling HandleStopedState"); HandleStoppedState(env, exp, transport, eventArgs); } }
private void SkipTimerExpired(object sender, EventArgs args) { Application.UIDeferredInvokeIfRequired(() => { if (SkipAmount != 0) { lock (SkipTimer) { if (SkipAmount != 0) { Logger.ReportVerbose("================ Skipping {0} seconds", SkipAmount); Application.CurrentInstance.RecentUserInput = true; var current = Application.MediaExperience.Transport.Position.Ticks; var duration = CurrentFileDurationTicks > 0 ? CurrentFileDurationTicks : PlaybackControllerHelper.GetDurationOfCurrentlyPlayingMedia(Application.MediaExperience.MediaMetadata); if (duration == 0) { // last ditch get from our metadata var playable = GetCurrentPlayableItem(); duration = playable != null ? playable.CurrentMedia.RuntimeTicks : long.MaxValue; } var pos = SkipAmount > 0 ? Math.Min(current + TimeSpan.FromSeconds(SkipAmount).Ticks, duration) : Math.Max(current - TimeSpan.FromSeconds(-SkipAmount).Ticks, 0); SkipAmount = 0; Seek(pos); } } } }); }