protected void StartPlayback() { HResult hr = m_pSession.Start(Guid.Empty, new PropVariant()); if (hr == HResult.S_OK) { mPlayPauseBtn.IsEnabled = true; mImageBtn.Source = new BitmapImage(new Uri("pack://application:,,,/WPFMediaFoundationPlayer;component/Images/pause.png", UriKind.Absolute)); mIsPlaying = true; mPresentationClock = null; m_pSession.GetClock(out mPresentationClock); mTickTimer.Start(); mIsSeek = false; } MFError.ThrowExceptionForHR(hr); //m_pSession. }
////////////////////////////////////////////////////////////////////////// // Name: CPlayer::Initialize // Description: // Intializes Media Foundation // Creates a media session // Creates a sequencer source // Creates a presentation clock // Creates an audio renderer // Starts the event queue // ///////////////////////////////////////////////////////////////////////// public HResult Initialize() { Debug.WriteLine("\nCPlayer::Initialize"); HResult hr = 0; try { IMFClock pClock; // Initialize Media Foundation. hr = MFExtern.MFStartup(0x10070, MFStartup.Full); MFError.ThrowExceptionForHR(hr); // Create the media session. hr = MFExtern.MFCreateMediaSession(null, out m_pMediaSession); MFError.ThrowExceptionForHR(hr); // Start the event queue. hr = m_pMediaSession.BeginGetEvent(this, null); MFError.ThrowExceptionForHR(hr); // Create a sequencer Source. hr = MFExtern.MFCreateSequencerSource(null, out m_pSequencerSource); MFError.ThrowExceptionForHR(hr); //setup clock hr = m_pMediaSession.GetClock(out pClock); MFError.ThrowExceptionForHR(hr); m_pPresentationClock = (IMFPresentationClock)pClock; // Create an IMFActivate object for the audio renderer. hr = MFExtern.MFCreateAudioRendererActivate(out m_pAudioRendererActivate); MFError.ThrowExceptionForHR(hr); //Set the player state to Initialized m_State = PlayerState.Initialized; // Notify the app that the player is initialized. PostMessage(m_hWnd, Form1.WM_NOTIFY_APP, new IntPtr((int)PlayerEvent.Initialized), new IntPtr((int)m_State)); } catch (Exception e) { hr = (HResult)Marshal.GetHRForException(e); } //Clean up. return(hr); }
/// <summary> /// Processes the media session event. /// </summary> private void ProcessEvent() { while (m_Session != null) { try { m_Session.GetEvent(1, out IMFMediaEvent _event);//requests events and returns immediately _event.GetType(out MediaEventType eventtype); switch (eventtype) { case MediaEventType.MESessionEnded: PlaybackState = PlaybackState.Stopped; PlaybackStopped?.Invoke(this, new StoppedEventArgs()); break; case MediaEventType.MESessionTopologyStatus: //topology loaded Guid guidManager = typeof(IAudioSessionManager).GUID; (new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator). GetDefaultAudioEndpoint(CoreAudioApi.DataFlow.Render, CoreAudioApi.Role.Multimedia, out IMMDevice endoint); endoint.Activate(ref guidManager, ClsCtx.ALL, IntPtr.Zero, out object _manager); IAudioSessionManager manager = _manager as IAudioSessionManager; manager.GetSimpleAudioVolume(Guid.Empty, 0, out m_volume); m_Session.GetClock(out m_clock); Guid guid_ratecontrol = typeof(IMFRateControl).GUID; Guid MF_RATE_CONTROL_SERVICE = Guid.Parse("866fa297-b802-4bf8-9dc9-5e3b6a9f53c9"); MediaFoundationInterop.MFGetService(m_Session, ref MF_RATE_CONTROL_SERVICE, ref guid_ratecontrol, out object _control); //gets rate control m_rate = _control as IMFRateControl; Prepared = true; break; } _event = null; } catch (COMException e) { if (e.HResult == MediaFoundationErrors.MF_E_NO_EVENTS_AVAILABLE) { continue; } else { throw e; } } catch (ThreadAbortException) { break; } } }