C# DirectShow 播放多媒體

來源:互聯網
上載者:User

要使 C# 代碼引用 COM 物件和介面,需要在 C# 組建中包含 COM 介面的 .NET 架構定義。完成此操作的最簡單方法是使用 TlbImp.exe(類型庫匯入程式),它是一個包括在 .NET 架構 SDK 中的命令列工具。TlbImp 將 COM 類別型庫轉換為 .NET 架構中繼資料,從而有效地建立一個可以從任何託管語言調用的託管封裝。用 TlbImp 建立的 .NET 架構中繼資料可以通過 /R 編譯器選項包括在 C# 組建中。如果使用 Visual Studio 開發環境,則只需添加對 COM 類別型庫的引用,將為您自動完成此轉換。
例如,我們要播放目前的目錄下的demo.avi檔案,需要用到包含在位於 Windows 系統目錄中的 Quartz.dll 中的媒體播放機。(c:\winnt\system32\quartz.dll)。可在命令列中運行TlbImp檔案(D:\ Microsoft Visual Studio .NET\FrameworkSDK\Bin\Tlbimp.exe)
tlbimp c:\winnt\system32\quartz.dll /out:QuartzTypeLib.dll
請注意,得到的 DLL 需要命名為 QuartzTypeLib,以便 .NET 架構可以在運行時正確載入包含類型。
產生程式時使用 C# 編譯器選項 /R 以包含 QuartzTypeLib.DLL 檔案;如果使用 Visual Studio 開發環境,直接添加引用即可(using QuartzTypeLib)。
然後就可以使用此程式顯示影片了。
具體編寫代碼時,用到了RenderFile 和 Run 方法。例:
private void menuItemOpen_Click(object sender, System.EventArgs e)
{
FilgraphManager m_FilGraphManager = null;
IBasicAudio m_BasicAudio = null;
IVideoWindow m_VideoWindow = null;
IMediaEvent m_MediaEvent = null;
IMediaEventEx m_MediaEventEx = null;
IMediaPosition m_MediaPosition = null;
IMediaControl m_MediaControl = null;
OpenFileDialog  OpenDialog = new OpenFileDialog();
OpenDialog.Filter = "Media Files|*.mpg;*.avi;*.wma;*.mov;*.wav;*.mp2;*.mp3|All Files|*.*";                //本例用對話方塊讀入要顯示的影片檔案名稱
if (DialogResult.OK == OpenDialog.ShowDialog())
{
m_FilGraphManager = new FilgraphManager();              
m_FilGraphManager.RenderFile(OpenDialog.FileName);
m_BasicAudio = m_FilGraphManager as IBasicAudio ;
try
{
m_VideoWindow = m_FilGraphManager as IVideoWindow;
m_VideoWindow.Owner = (int) panel1.Handle;
m_VideoWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN;
//此設定可以不顯示播放器的title,使播放器像嵌在表單中。
//可設定 private const int WS_CHILD = 0x40000000;
//      private const int WS_CLIPCHILDREN = 0x2000000;
m_VideoWindow.SetWindowPosition(panel1.ClientRectangle.Left,
panel1.ClientRectangle.Top,
panel1.ClientRectangle.Width,
panel1.ClientRectangle.Height);
// 在panel1中顯示,要求影片可隨panel1大小而變化。
}
catch (Exception)
{
m_VideoWindow = null;
}

m_MediaEvent = m_FilGraphManager as IMediaEvent;
m_MediaEventEx = m_FilGraphManager as IMediaEventEx;
m_MediaEventEx.SetNotifyWindow((int) this.Handle,WM_GRAPHNOTIFY, 0);
m_MediaPosition = m_FilGraphManager as IMediaPosition;
m_MediaControl = m_FilGraphManager as IMediaControl;
this.Text = "DirectShow - [" + OpenDialog.FileName + "]";
m_MediaControl.Run();
}
}

也可以加入pause,stop命令來控制影片的播放。
m_MediaControl.Pause()
m_MediaControl.Stop()

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.