- CComPtr<IGraphBuilder> m_pPlaybackGraph;
- // Create a graph to play the requested file
- hr = m_pPlaybackGraph.CoCreateInstance(CLSID_FilterGraph);
- ASSERT(m_pPlaybackGraph);
- // Render the media file
- hr = m_pPlaybackGraph->RenderFile(Filename,NULL);
- ASSERT(FAILED(hr));
- // Set video window to be a child window of the screen and
- // position it properly.
- CComQIPtr< IVideoWindow, &IID_IVideoWindow > pVW(m_pPlaybackGraph);
- //hWnd is window handle.
- hr = pVW->put_Owner((OAHWND)hWnd);
- ASSERT(FAILED(hr));
- // If the selected file has no video component, fail gracefully
- // and still play the audio portion
- if (SUCCEEDED(hr))
- {
- // The video window must have the WS_CHILD style
- hr = pVW->put_WindowStyle(WS_CHILD);
- // Read coordinates of video container window
- RECT rc;
- GetWindowRect(hWnd,&rc);
- long width = rc.right - rc.left;
- long height = rc.bottom - rc.top;
- // Ignore the video's original size and stretch to fit bounding rectangle
- hr = pVW->SetWindowPosition(rc.left, rc.top, width, height);
- ASSERT(FAILED(hr));
- }
- // Start running the graph
- CComQIPtr< IMediaControl, &IID_IMediaControl > m_pPlaybackControl(m_pPlaybackGraph);
- hr = m_pPlaybackControl->Run();
- ASSERT(FAILED(hr));
- // If there is no current graph, nothing needs to be done
- if (!m_pPlaybackGraph)
- return;
- // Get media control and media seeking interfaces
- CComQIPtr< IMediaControl, &IID_IMediaControl > pMediaControl(m_pPlaybackGraph);
- CComQIPtr< IMediaSeeking, &IID_IMediaSeeking > pMediaSeeking(m_pPlaybackGraph);
- // Stop the graph
- hr = pMediaControl->Stop();
- if(FAILED(hr))
- {
- AfxMessageBox(TEXT("Could not stop graph"));
- return;
- }
- // Reset to first frame of movie (if the clip supports seeking)
- LONGLONG pos=0;
- hr = pMediaSeeking->SetPositions(&pos, AM_SEEKING_AbsolutePositioning ,
- NULL, AM_SEEKING_NoPositioning);
- // Display the first frame of the movie.
- hr = pMediaControl->Pause();
- if(FAILED(hr))
- {
- AfxMessageBox(TEXT("Could not pause graph"));
- return;
- }