Menu

Need Help to build Graph in XNA Framework

eclere
2009-09-09
2012-10-29
  • eclere

    eclere - 2009-09-09

    Hello,

    i have a sample to play Videos with DirectShow in the XNA Framework. Code:

    /// <summary>
    /// This method sets up the DirectShow filter graph and obtains the interfaces necessary to control playback
    /// for VideoTextures created from video files. This method works for .avi, .mpeg, and .wmv files.
    /// </summary>
    /// <param name="videoFile">The .avi, .mpeg, or .wmv video file.</param>
    private void SetupGraph(string videoFile)
    {
    try
    {
    int hr;

                // 1. Start building the graph, using FilterGraph and CaptureGraphBuilder2
                IGraphBuilder graphBuilder = (IGraphBuilder)new FilterGraph();
                ICaptureGraphBuilder2 builder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
                hr = builder.SetFiltergraph(graphBuilder);
                DsError.ThrowExceptionForHR(hr);
    
                // 2. Add the source filter for the video file input.
                IBaseFilter sourceFilter = null;
                hr = graphBuilder.AddSourceFilter(videoFile, videoFile, out sourceFilter);
                DsError.ThrowExceptionForHR(hr);
    
                // 3. Get the SampleGrabber interface, configure it, and add it to the graph.
                ISampleGrabber sampGrabber = (ISampleGrabber)new SampleGrabber();
                ConfigureSampleGrabber(sampGrabber);
                hr = graphBuilder.AddFilter((IBaseFilter)sampGrabber, &quot;SampleGrabber&quot;);
                DsError.ThrowExceptionForHR(hr);
    
    
                // 4. Add the null renderer (since we don't want to render in a seperate window.)
                IBaseFilter nullRenderer = (IBaseFilter)new NullRenderer();
                hr = graphBuilder.AddFilter(nullRenderer, &quot;Null Renderer&quot;);
                DsError.ThrowExceptionForHR(hr);
    
                // 5. Render the stream.  The way the stream is rendered depends on its type.
                switch (this.vidType)
                {
                    case VideoType.AVI:
                        hr = builder.RenderStream(null, null, sourceFilter, (IBaseFilter)sampGrabber, nullRenderer);
                        break;
                    case VideoType.MPEG:
                        hr = builder.RenderStream(null, null, sourceFilter, (IBaseFilter)sampGrabber, nullRenderer);
                        break;
                    case VideoType.WMV:
                        hr = builder.RenderStream(null, MediaType.Video, sourceFilter, (IBaseFilter)sampGrabber, nullRenderer);
                        break;
                    default:
                        throw new Exception(&quot;Unsupported Video type: &quot; + this.vidType.ToString());
                }
                DsError.ThrowExceptionForHR(hr);
    
                // 6. Now that everthing is configured and set up, save the width, height, stride information for use later.
                SaveSizeInfo(sampGrabber);
    
                // 7. Obtain the interfaces that we will use to control the execution of the filter graph.
                this.mediaControl = graphBuilder as IMediaControl;
                this.mediaEventEx = graphBuilder as IMediaEventEx;
                this.mediaSeeking = graphBuilder as IMediaSeeking;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
        }
    

    Now i want enable the sound, but i dont really know what i have to do. Can anybody help?

    Best Regards,
    Thorsten

     
    • eclere

      eclere - 2009-09-10

      Hi Peter,

      thanks. It works fine:-)

      Thorsten

       
    • Peter_B

      Peter_B - 2009-09-10
       

Log in to post a comment.