在日常的開發項目時,一般都是利用Windows Media Player播放視頻,最近做一個項目,用Windows Media Player播放視頻時,會遇到播放的時候視頻就變成音頻了文件了,只有聲音沒有影像,展現出音頻的波紋。但是找了很久原因也沒有找到問題的所在。
后來決定換個插件來播放音頻,於是找到了安裝暴風影音之類的插件,但是經過自己的實際使用,發現這類播放器的插件添加
com組件中,在使用的時候會出現卡死的現象,畢竟這類插件都是非常大的,對於使用現場電腦環境不理想的項目還是不能夠使用。
最終找到了利用DirectX 播放視頻,這個使用起來就非常的方便,DirectX 畢竟是電腦自帶的,下面介紹DirectX 播放視頻的實現方式:
必須使用的引用:
using Microsoft.DirectX;
using Microsoft.DirectX.AudioVideoPlayback;
private Video video = null;
/// <summary>
/// 播放視頻
/// </summary>
public void playVideo()
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//獲取視頻路徑
string strFile = openFileDialog1.FileName;
try
{
//判斷視頻文件是否為空
if (this.video != null)
{
//判斷是否已經在播放視頻
if (this.video.Playing)
{
//如果已經播放視頻了,就先關閉了
this.video.Stop();
}
}
//取得視頻文件
this.video = new Video(strFile);
//控制播放視頻窗口的大小(此項目是把視頻放到一個panel中,panPlayOwner是一個panel)
int width = this.panPlayOwner.Width;
int height = this.panPlayOwner.Height;
this.video.Owner = this.panPlayOwner;
this.video.Owner.Width = width;
this.video.Owner.Height = height;
this.video.Play();
}
catch (DirectXException ex)
{
MessageBox.Show(ex.Message);
}
}
}