/// <summary> /// Finishes pending operations, closes the loaded script, releases resources /// and resets the runtime context. /// </summary> public void CloseScript() { if (InternalContext.IsVideoPlaying) { _playVideoTaskCancellationContext.SetCloseScript(); } else { CloseScriptCore(); ScriptClosed?.Invoke(this, new EventArgs()); } }
/// <summary> /// Starts asynchronous video playback to source and preview Direct3D surfaces beginning at runtime context <see cref="IScriptVideoContext.FrameNumber"/>. /// </summary> /// <returns>A <see cref="Task"/> object representing the asynchronous operation.</returns> public async Task StartVideoPlayback() { _playVideoTaskCancellationContext.Reset(); InternalContext.IsVideoPlaying = true; VideoPlaybackStarted?.Invoke(this, new EventArgs()); IProgress <int> frameChangeProgress = new Progress <int>(OnFrameChanged); try { _ = await Task.Run(() => PlayVideo(frameChangeProgress)); if (!_playVideoTaskCancellationContext.IsCancellationRequested || _playVideoTaskCancellationContext.CancellationReason == PlayVideoTaskCancellationReason.StopRequested) { SeekFrame(0); } } catch (Exception ex) { // TODO: Is the service method the right place to catch the exception and notify the UI? Could this be handled in the View/ViewModel(s) calling this method? InternalContext.SystemDialogService.ShowErrorDialog("An exception occurred during video playback", "Script video service", ex); } PlayVideoTaskCancellationReason playVideoTaskCancellationReason = _playVideoTaskCancellationContext.CancellationReason; _playVideoTaskCancellationContext.Reset(); InternalContext.IsVideoPlaying = false; VideoPlaybackStopped?.Invoke(this, new EventArgs()); if (playVideoTaskCancellationReason == PlayVideoTaskCancellationReason.CloseScriptRequested) { CloseScriptCore(); ScriptClosed?.Invoke(this, new EventArgs()); } }