/// <summary> /// Disposes all current resources. /// </summary> private void DisposeResources() { lock (m_mfResourceLock) { // Shutdown current session if (m_mediaSession != null) { m_mediaSession.Shutdown(); } // Clear all references m_currentVideoLink = null; m_currentCaptureDevice = null; m_currentVideoDuration = TimeSpan.Zero; GraphicsHelper.SafeDispose(ref m_displayControl); GraphicsHelper.SafeDispose(ref m_audioStreamVolume); GraphicsHelper.SafeDispose(ref m_mediaSession); m_sessionEventHandler = null; GraphicsHelper.SafeDispose(ref m_videoSourceStream); GraphicsHelper.SafeDispose(ref m_videoSourceStreamNet); } // Apply new state this.IsPaused = false; this.State = MediaPlayerState.NothingToDo; }
/// <summary> /// Opens the given video file and plays it directly. /// </summary> /// <param name="videoLink">The link to the video file.</param> public async Task OpenAndShowVideoFileAsync(ResourceLink videoLink) { // Check for correct state if (this.State != MediaPlayerState.NothingToDo) { throw new InvalidOperationException("Unable to open video file as long as there is another video playing!"); } // Apply new state this.State = MediaPlayerState.Opening; try { // Create media session and a corresponding event listener obect for async events MF.MediaFactory.CreateMediaSession(null, out m_mediaSession); m_sessionEventHandler = MFSessionEventListener.AttachTo(m_mediaSession); m_sessionEventHandler.EndOfPresentation += OnSessionEventHandlerEndOfPresentationReached; // Create source object MF.SourceResolver sourceResolver = new MF.SourceResolver(); MF.ObjectType objType = MF.ObjectType.Invalid; m_videoSourceStreamNet = videoLink.OpenInputStream(); m_videoSourceStream = new MF.ByteStream(m_videoSourceStreamNet); SharpDX.ComObject objSource = sourceResolver.CreateObjectFromStream( m_videoSourceStream, "Dummy." + videoLink.FileExtension, MF.SourceResolverFlags.MediaSource, out objType); using (MF.MediaSource mediaSource = objSource.QueryInterface <MF.MediaSource>()) { GraphicsHelper.SafeDispose(ref objSource); GraphicsHelper.SafeDispose(ref sourceResolver); await ShowVideoAsync(mediaSource); } // Video opened successfully m_currentVideoLink = videoLink; m_currentCaptureDevice = null; this.State = MediaPlayerState.Playing; } catch (Exception) { // Unload all resources in case of an exception DisposeResources(); throw; } }
public async Task ShowCaptureDeviceAsync(CaptureDeviceInfo captureDevice) { // Check for correct state if (this.State != MediaPlayerState.NothingToDo) { throw new InvalidOperationException("Unable to open video file as long as there is another video playing!"); } // Apply new state this.State = MediaPlayerState.Opening; try { // Create media session and a corresponding event listener obect for async events MF.MediaFactory.CreateMediaSession(null, out m_mediaSession); m_sessionEventHandler = MFSessionEventListener.AttachTo(m_mediaSession); m_sessionEventHandler.EndOfPresentation += OnSessionEventHandlerEndOfPresentationReached; // Create the media source using (MF.MediaSource mediaSource = captureDevice.CreateMediaSource()) { // Show the video await ShowVideoAsync(mediaSource); } // Video opened successfully m_currentVideoLink = null; m_currentCaptureDevice = captureDevice; this.State = MediaPlayerState.Playing; } catch (Exception) { // Unload all resources in case of an exception DisposeResources(); throw; } }