private void requestNavigateEvent(object sender, RoutedEventArgs e) { RequestNavigateEventArgs args = e as RequestNavigateEventArgs; Uri uri = args.Uri; if (uri.IsFile) { String location = null; location = HttpUtility.UrlDecode(uri.AbsolutePath); if (MediaFormatConvert.isVideoFile(location)) { NameValueCollection values = HttpUtility.ParseQueryString(uri.Query); String[] formats = { @"h'h'm'm's's'", @"m'm's's'", @"s's'" }; String timeOffsetString = values["t"]; TimeSpan time = new TimeSpan(); if (timeOffsetString != null) { try { bool success = TimeSpan.TryParseExact(timeOffsetString, formats, null, out time); } catch (Exception ex) { Logger.Log.Error("Error parsing timestring: " + timeOffsetString + " for " + location, ex); } } MediaItem item = MediaItemFactory.create(location); Shell.ShellViewModel.navigateToVideoView(item, (int)time.TotalSeconds); } e.Handled = true; } }
public VideoViewModel(IEventAggregator eventAggregator) { EventAggregator = eventAggregator; VideoSettings = ServiceLocator.Current.GetInstance(typeof(VideoSettingsViewModel)) as VideoSettingsViewModel; VideoSettings.SettingsChanged += VideoSettings_SettingsChanged; IsInitialized = false; isInitializedSignal = new SemaphoreSlim(0, 1); CurrentItem = new VideoAudioPair(null, null); OpenAndPlayCommand = new AsyncCommand <VideoAudioPair>(async item => { await isInitializedSignal.WaitAsync(); isInitializedSignal.Release(); try { CurrentItem = item; String videoLocation = null; String videoFormatName = null; if (item.Video != null) { videoLocation = item.Video.Location; if (item.Video is MediaStreamedItem) { if (item.Video.Metadata != null) { videoFormatName = MediaFormatConvert.mimeTypeToExtension(item.Video.Metadata.MimeType); } } } String audioLocation = null; String audioFormatName = null; if (item.Audio != null) { audioLocation = item.Audio.Location; if (item.Audio is MediaStreamedItem && item.Audio.Metadata != null) { audioFormatName = MediaFormatConvert.mimeTypeToExtension(item.Audio.Metadata.MimeType); } } CloseCommand.IsExecutable = true; IsLoading = true; await VideoPlayer.openAndPlay(videoLocation, videoFormatName, audioLocation, audioFormatName); } catch (OperationCanceledException) { CloseCommand.IsExecutable = false; throw; } catch (Exception e) { CloseCommand.IsExecutable = false; MessageBox.Show("Error opening " + item.Video.Location + "\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); throw; } finally { IsLoading = false; } EventAggregator.GetEvent <TitleChangedEvent>().Publish(CurrentItem.IsEmpty ? null : CurrentItem.Name); }); PlayCommand = new AsyncCommand(async() => { if (VideoState == VideoPlayerControl.VideoState.CLOSED && !CurrentItem.IsEmpty) { await openAndPlay(CurrentItem); } else if (VideoState == VideoPlayerControl.VideoState.OPEN || VideoState == VideoPlayerControl.VideoState.PAUSED) { VideoPlayer.play(); } }, false); PauseCommand = new Command(() => { VideoPlayer.pause(); }, false); CloseCommand = new AsyncCommand(async() => { await VideoPlayer.close(); }, false); SeekCommand = new AsyncCommand <double>(async(pos) => { await VideoPlayer.seek(pos); }, false); StepForwardCommand = new AsyncCommand(async() => { StepForwardCommand.IsExecutable = false; double timeStamp = VideoPlayer.getFrameSeekTimeStamp() + Settings.Default.VideoStepDurationSeconds; await VideoPlayer.seek(timeStamp, VideoLib.VideoPlayer.SeekKeyframeMode.SEEK_FORWARDS); StepForwardCommand.IsExecutable = true; }, false); StepBackwardCommand = new AsyncCommand(async() => { StepBackwardCommand.IsExecutable = false; double timeStamp = VideoPlayer.getFrameSeekTimeStamp() - Settings.Default.VideoStepDurationSeconds; await VideoPlayer.seek(timeStamp, VideoLib.VideoPlayer.SeekKeyframeMode.SEEK_BACKWARDS); StepBackwardCommand.IsExecutable = true; }, false); FrameByFrameCommand = new Command(() => { VideoPlayer.displayNextFrame(); }, false); CutVideoCommand = new Command(() => { String outputPath; if (FileUtils.isUrl(VideoPlayer.VideoLocation)) { outputPath = MediaFileWatcher.Instance.Path; } else { outputPath = FileUtils.getPathWithoutFileName(VideoPlayer.VideoLocation); } VideoTranscodeView videoTranscode = new VideoTranscodeView(); videoTranscode.ViewModel.Items.Add(CurrentItem); videoTranscode.ViewModel.Title = "Cut Video"; videoTranscode.ViewModel.IconUri = "/MediaViewer;component/Resources/Icons/videocut.ico"; videoTranscode.ViewModel.OutputPath = outputPath; videoTranscode.ViewModel.IsTimeRangeEnabled = IsTimeRangeEnabled; videoTranscode.ViewModel.StartTimeRange = startTimeRangeTimeStamp; videoTranscode.ViewModel.EndTimeRange = endTimeRangeTimeStamp; String extension = Path.GetExtension(VideoPlayer.VideoLocation).ToLower().TrimStart('.'); foreach (ContainerFormats format in Enum.GetValues(typeof(ContainerFormats))) { if (format.ToString().ToLower().Equals(extension)) { videoTranscode.ViewModel.ContainerFormat = format; } } videoTranscode.ShowDialog(); }, false); ScreenShotCommand = new Command(() => { try { String screenShotName = FileUtils.removeIllegalCharsFromFileName(CurrentItem.Video.Name, " "); screenShotName += "." + "jpg"; String path = null; switch (Settings.Default.VideoScreenShotSaveMode) { case MediaViewer.Infrastructure.Constants.SaveLocation.Current: { path = MediaFileWatcher.Instance.Path; break; } case MediaViewer.Infrastructure.Constants.SaveLocation.Ask: { DirectoryPickerView directoryPicker = new DirectoryPickerView(); DirectoryPickerViewModel vm = (DirectoryPickerViewModel)directoryPicker.DataContext; directoryPicker.Title = "Screenshot Output Directory"; vm.SelectedPath = Settings.Default.VideoScreenShotLocation; vm.PathHistory = Settings.Default.VideoScreenShotLocationHistory; if (directoryPicker.ShowDialog() == true) { path = vm.SelectedPath; } else { return; } break; } case MediaViewer.Infrastructure.Constants.SaveLocation.Fixed: { path = Settings.Default.VideoScreenShotLocation; break; } default: break; } String fullPath = FileUtils.getUniqueFileName(path + "\\" + screenShotName); VideoPlayer.createScreenShot(fullPath, VideoSettings.VideoScreenShotTimeOffset); } catch (Exception e) { MessageBox.Show("Error creating screenshot.\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }, false); SetLeftMarkerCommand = new Command(() => { if (IsTimeRangeEnabled == false) { IsTimeRangeEnabled = true; } // get the exact time of the current audio or video frame, positionseconds is potentially inaccurate StartTimeRange = VideoPlayer.PositionSeconds; startTimeRangeTimeStamp = VideoPlayer.getFrameSeekTimeStamp(); }, false); SetRightMarkerCommand = new Command(() => { if (IsTimeRangeEnabled == false) { IsTimeRangeEnabled = true; StartTimeRange = VideoPlayer.PositionSeconds; startTimeRangeTimeStamp = VideoPlayer.getFrameSeekTimeStamp(); } else { EndTimeRange = VideoPlayer.PositionSeconds; endTimeRangeTimeStamp = VideoPlayer.getFrameSeekTimeStamp(); } }, false); OpenLocationCommand = new Command(async() => { VideoOpenLocationView openLocation = new VideoOpenLocationView(); bool?success = openLocation.ShowDialog(); if (success == true) { MediaItem video = null; MediaItem audio = null; if (!String.IsNullOrWhiteSpace(openLocation.ViewModel.VideoLocation)) { video = MediaItemFactory.create(openLocation.ViewModel.VideoLocation); MiscUtils.insertIntoHistoryCollection(Settings.Default.VideoLocationHistory, openLocation.ViewModel.VideoLocation); } if (!String.IsNullOrWhiteSpace(openLocation.ViewModel.AudioLocation)) { audio = MediaItemFactory.create(openLocation.ViewModel.AudioLocation); MiscUtils.insertIntoHistoryCollection(Settings.Default.AudioLocationHistory, openLocation.ViewModel.AudioLocation); } await openAndPlay(new VideoAudioPair(video, audio)); } }); HasAudio = true; VideoState = VideoPlayerControl.VideoState.CLOSED; IsTimeRangeEnabled = false; StartTimeRange = 0; EndTimeRange = 0; startTimeRangeTimeStamp = 0; endTimeRangeTimeStamp = 0; }
public void OnImportsSatisfied() { ShellViewModel = new ShellViewModel(MediaFileWatcher.Instance, RegionManager, EventAggregator); DataContext = ShellViewModel; this.RegionManager.RegisterViewWithRegion(RegionNames.MainNavigationToolBarRegion, typeof(ImageNavigationItemView)); this.RegionManager.RegisterViewWithRegion(RegionNames.MainNavigationToolBarRegion, typeof(VideoNavigationItemView)); this.RegionManager.RegisterViewWithRegion(RegionNames.MainNavigationToolBarRegion, typeof(MediaFileBrowserNavigationItemView)); this.RegionManager.RegisterViewWithRegion(RegionNames.MainNavigationToolBarRegion, typeof(SettingsNavigationItemView)); EventAggregator.GetEvent <TitleChangedEvent>().Subscribe((title) => { if (!String.IsNullOrEmpty(title)) { this.Title = "MediaViewer - " + title; } else { this.Title = "MediaViewer"; } }, ThreadOption.UIThread); EventAggregator.GetEvent <ToggleFullScreenEvent>().Subscribe((isFullscreen) => { if (isFullscreen) { prevWindowState = WindowState; WindowState = System.Windows.WindowState.Maximized; WindowStyle = System.Windows.WindowStyle.None; toolBarPanel.Visibility = Visibility.Collapsed; } else { WindowState = prevWindowState; WindowStyle = System.Windows.WindowStyle.SingleBorderWindow; toolBarPanel.Visibility = Visibility.Visible; } }, ThreadOption.UIThread); // initialize several settings ServiceLocator.Current.GetInstance(typeof(DbSettingsViewModel)); ServiceLocator.Current.GetInstance(typeof(AboutViewModel)); try { String location = App.Args.Count() > 0 ? FileUtils.getProperFilePathCapitalization(App.Args[0]) : ""; if (MediaViewer.Model.Utils.MediaFormatConvert.isImageFile(location)) { MediaFileWatcher.Instance.Path = FileUtils.getPathWithoutFileName(location); MediaItem item = MediaItemFactory.create(location); ShellViewModel.navigateToImageView(item); } else if (MediaFormatConvert.isVideoFile(location)) { MediaFileWatcher.Instance.Path = FileUtils.getPathWithoutFileName(location); MediaItem item = MediaItemFactory.create(location); ShellViewModel.navigateToVideoView(item); } else { ShellViewModel.navigateToMediaFileBrowser(); } } catch (Exception e) { Logger.Log.Error("Error in command line argument: " + App.Args[0], e); ShellViewModel.navigateToMediaFileBrowser(); } }