bool _canStop(object o)
        {
            MediaSegmentViewmodel segment = PlayWholeClip ? SelectedSegment : null;
            IMedia media = LoadedMedia;

            if (media == null)
            {
                return(false);
            }
            TimeSpan duration = PlayWholeClip ? media.Duration : (segment == null ? media.Duration : segment.Duration);

            return(duration.Ticks >= _preview.FormatDescription.FrameTicks);
        }
 private void _mediaSegments_SegmentAdded(object sender, MediaSegmentEventArgs e)
 {
     Application.Current.Dispatcher.BeginInvoke((Action) delegate()
     {
         IPersistentMedia media = _loadedMedia as IPersistentMedia;
         if (media != null && sender == media.MediaSegments)
         {
             var newVM = new MediaSegmentViewmodel(media, e.Segment);
             _mediaSegments.Add(newVM);
             if (e.Segment == _lastAddedSegment)
             {
                 SelectedSegment = newVM;
             }
         }
     });
 }
        private void CreateCommands()
        {
            CommandPause = new UICommand()
            {
                ExecuteDelegate = o =>
                {
                    if (LoadedMedia == null)
                    {
                        LoadedMedia = _media ?? _event?.Media;
                    }
                    else
                    if (_preview.PreviewIsPlaying)
                    {
                        _preview.PreviewPause();
                    }
                    else
                    {
                        _preview.PreviewPosition = 0;
                        NotifyPropertyChanged(nameof(Position));
                    }
                },
                CanExecuteDelegate = o =>
                {
                    IMedia media = Media ?? (Event != null ? Event.Media : null);
                    return((LoadedMedia != null && LoadedMedia.MediaStatus == TMediaStatus.Available) ||
                           _canLoad(media));
                }
            };
            CommandPlay = new UICommand()
            {
                ExecuteDelegate = o =>
                {
                    IMedia loadedMedia = LoadedMedia;
                    if (loadedMedia != null)
                    {
                        if (_preview.PreviewIsPlaying)
                        {
                            _preview.PreviewPause();
                        }
                        else
                        {
                            _preview.PreviewPlay();
                        }
                    }
                    else
                    {
                        CommandPause.Execute(null);
                        if (LoadedMedia != null)
                        {
                            _preview.PreviewPlay();
                        }
                    }
                },
                CanExecuteDelegate = o =>
                {
                    IMedia media = Media ?? (Event != null ? Event.Media : null);
                    return((LoadedMedia != null && LoadedMedia.MediaStatus == TMediaStatus.Available) ||
                           _canLoad(media));
                }
            };
            CommandStop = new UICommand()
            {
                ExecuteDelegate    = o => _mediaUnload(),
                CanExecuteDelegate = _canStop
            };
            CommandSeek = new UICommand()
            {
                ExecuteDelegate = param =>
                {
                    if (_preview.PreviewIsPlaying)
                    {
                        _preview.PreviewPause();
                    }
                    long seekFrames;
                    switch ((string)param)
                    {
                    case "fframe":
                        seekFrames = 1;
                        break;

                    case "rframe":
                        seekFrames = -1;
                        break;

                    case "fsecond":
                        seekFrames = FramesPerSecond;
                        break;

                    case "rsecond":
                        seekFrames = -FramesPerSecond;
                        break;

                    default:
                        seekFrames = 0;
                        break;
                    }
                    _preview.PreviewPosition = _preview.PreviewPosition + seekFrames;
                    NotifyPropertyChanged(nameof(Position));
                },
                CanExecuteDelegate = _canStop
            };

            CommandCopyToTcIn = new UICommand()
            {
                ExecuteDelegate    = o => TcIn = Position,
                CanExecuteDelegate = _canStop
            };

            CommandCopyToTcOut = new UICommand()
            {
                ExecuteDelegate    = o => TcOut = Position,
                CanExecuteDelegate = _canStop
            };

            CommandSaveSegment = new UICommand()
            {
                ExecuteDelegate = o =>
                {
                    MediaSegmentViewmodel msVm  = _selectedSegment;
                    IPersistentMedia      media = LoadedMedia as IPersistentMedia;
                    if (msVm == null)
                    {
                        _lastAddedSegment = media.MediaSegments.Add(TcIn, TcOut, SelectedSegmentName);
                        _lastAddedSegment.Save();
                    }
                    else
                    {
                        msVm.TcIn        = TcIn;
                        msVm.TcOut       = TcOut;
                        msVm.SegmentName = SelectedSegmentName;
                        msVm.Save();
                    }
                },
                CanExecuteDelegate = o =>
                {
                    var ss = SelectedSegment;
                    return(LoadedMedia != null &&
                           ((ss == null && !string.IsNullOrEmpty(SelectedSegmentName)) ||
                            (ss != null && (ss.IsModified || SelectedSegmentName != ss.SegmentName || TcIn != ss.TcIn || TcOut != ss.TcOut))));
                }
            };
            CommandDeleteSegment = new UICommand()
            {
                ExecuteDelegate = o =>
                {
                    MediaSegmentViewmodel msVm = _selectedSegment;
                    if (msVm != null)
                    {
                        msVm.MediaSegment.Delete();
                    }
                },
                CanExecuteDelegate = o => _selectedSegment != null
            };
            CommandNewSegment = new UICommand()
            {
                ExecuteDelegate = o =>
                {
                    var media = LoadedMedia as IPersistentMedia;
                    if (media != null)
                    {
                        _lastAddedSegment = media.MediaSegments.Add(TcIn, TcOut, Common.Properties.Resources._title_NewSegment);
                    }
                },
            };
            CommandSetSegmentNameFocus = new UICommand()
            {
                ExecuteDelegate = o =>
                {
                    IsSegmentNameFocused = true;
                    NotifyPropertyChanged(nameof(IsSegmentNameFocused));
                },
            };
        }