2012-11-29 95 views
1

例如在標籤或文本框中。 這是代碼即時通訊目前正試圖利用DirectShowLib-2005.dll如何在播放視頻文件時播放剩餘時間?

private void button5_Click(object sender, EventArgs e) 
     { 
      f = new WmvAdapter(_videoFile); 
      TimeSpan ts = TimeSpan.FromTicks(f._duration); 
      MessageBox.Show(ts.ToString()); 
      int t = 1; 
      const int WS_CHILD = 0x40000000; 
      const int WS_CLIPCHILDREN = 0x2000000; 
      _videoFile = Options_DB.get_loadedVideo(); 
      FilgraphManager graphManager = new FilgraphManager(); 

      graphManager.RenderFile(_videoFile); 
      videoWindow = (IVideoWindow)graphManager; 
      videoWindow.Owner = (int)pictureBox1.Handle; 
      videoWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN; 
      videoWindow.SetWindowPosition(
       pictureBox1.ClientRectangle.Left, 
       pictureBox1.ClientRectangle.Top, 
       pictureBox1.ClientRectangle.Width, 
       pictureBox1.ClientRectangle.Height); 
      mc = (IMediaControl)graphManager; 
      mc.Run(); 

當我點擊按鈕的文件播放,我看到第一個在MessageBox.Show期間至極告訴我:00:02:47.4800000

所以首先是持續時間是錯誤的,因爲文件播放長度是:00:04:36當我在硬盤上查看文件時。

我的目標是顯示一些progressBar或沒有progressBar現在只是在標籤上剩下的時間向後播放視頻。如果持續時間是00:04:36,所以我想顯示它回去00:04:35 ... 00:04:34等等。

變量_duration很長,我試圖將其轉換爲TimeSpan。 但是,視頻長度與當我在硬盤上查看文件時不一樣。

這是載體作用至極我不只是用它從類WmvAdapter創建:

private void SetupGraph(string file) 
     { 
      ISampleGrabber sampGrabber = null; 
      IBaseFilter capFilter = null; 
      IBaseFilter nullrenderer = null; 

      _filterGraph = (IFilterGraph2)new FilterGraph(); 
      _mediaCtrl = (IMediaControl)_filterGraph; 
      _mediaEvent = (IMediaEvent)_filterGraph; 

      _mSeek = (IMediaSeeking)_filterGraph; 

      var mediaFilt = (IMediaFilter)_filterGraph; 

      try 
      { 
       // Add the video source 
       int hr = _filterGraph.AddSourceFilter(file, "Ds.NET FileFilter", out capFilter); 
       DsError.ThrowExceptionForHR(hr); 

       // Get the SampleGrabber interface 
       sampGrabber = new SampleGrabber() as ISampleGrabber; 
       var baseGrabFlt = sampGrabber as IBaseFilter; 

       ConfigureSampleGrabber(sampGrabber); 

       // Add the frame grabber to the graph 
       hr = _filterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); 
       DsError.ThrowExceptionForHR(hr); 

       // --------------------------------- 
       // Connect the file filter to the sample grabber 

       // Hopefully this will be the video pin, we could check by reading it's mediatype 
       IPin iPinOut = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0); 

       // Get the input pin from the sample grabber 
       IPin iPinIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); 

       hr = _filterGraph.Connect(iPinOut, iPinIn); 
       DsError.ThrowExceptionForHR(hr); 

       // Add the null renderer to the graph 
       nullrenderer = new NullRenderer() as IBaseFilter; 
       hr = _filterGraph.AddFilter(nullrenderer, "Null renderer"); 
       DsError.ThrowExceptionForHR(hr); 

       // --------------------------------- 
       // Connect the sample grabber to the null renderer 

       iPinOut = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0); 
       iPinIn = DsFindPin.ByDirection(nullrenderer, PinDirection.Input, 0); 

       hr = _filterGraph.Connect(iPinOut, iPinIn); 
       DsError.ThrowExceptionForHR(hr); 

       // Turn off the clock. This causes the frames to be sent 
       // thru the graph as fast as possible 
       hr = mediaFilt.SetSyncSource(null); 
       DsError.ThrowExceptionForHR(hr); 

       // Read and cache the image sizes 
       SaveSizeInfo(sampGrabber); 

       //Edit: get the duration 
       hr = _mSeek.GetDuration(out _duration); 
       DsError.ThrowExceptionForHR(hr); 
      } 
      finally 
      { 
       if (capFilter != null) 
       { 
        Marshal.ReleaseComObject(capFilter); 
       } 
       if (sampGrabber != null) 
       { 
        Marshal.ReleaseComObject(sampGrabber); 
       } 
       if (nullrenderer != null) 
       { 
        Marshal.ReleaseComObject(nullrenderer); 
       } 
       GC.Collect(); 
      } 
     } 

它之前,我轉換爲時間跨度是可變_duration期限:1674800000 我嘗試了很多例子和東西,但我無法遠離TimeSpan轉換。

我該怎麼做?

謝謝。

回答

0

這個問題似乎涉及:Determine length of audio file using DirectShow

答案有規定:

GetDuration返回,將需要多長時間才能播放該文件一個64位的整數值。

您需要調用GetTimeFormat方法來找出持續時間所處的單位。最可能的默認值是TIME_FORMAT_MEDIA_TIME,它是微秒的十分之一秒。

在這種情況下,您會將持續時間除以10 * 1000 * 1000以獲得秒數。

如果你想強制單位,你也可以在調用GetDuration之前調用SetTimeFormat。

所以你的情況,我會使用GetTimeFormat()弄清楚的單位和使用將其轉換爲正確的單位爲TimeSpan對象。

+0

GetTimeFormat需要得到(出guid pFormat)那是什麼? –

+0

如果你還沒有找到它,根據你提供的簽名,你可以把它叫做'GetTimeFormat(out pFormat)',其中'pFormat'的類型是'guid'。 – Lunyx