在C#.NET windows應用程序做視頻播放,首先要用到com組件中windows media player,當然你也可以用其它的,這里就以windows media player為例。
一、新建windows應用程序項目,添加vedioForm窗體
二、在com組件中找到windows media player,添加引用
三、代碼如下:
public partial class VedioForm : Form
{
private AxWMPLib.AxWindowsMediaPlayer axWindowsMediaPlayer1;
public VedioForm()
{
InitializeComponent();
InitVedio();
}
private void VedioForm_Load(object sender, EventArgs e)
{
InitVedioUrl();
InitEvent();
}
//初始化播放控件
private void InitVedio()
{
this.axWindowsMediaPlayer1 = new AxWMPLib.AxWindowsMediaPlayer();
this.axWindowsMediaPlayer1.Enabled = true;
this.axWindowsMediaPlayer1.Location = new System.Drawing.Point(0, 400);
this.axWindowsMediaPlayer1.Name = "axWindowsMediaPlayer1";
this.axWindowsMediaPlayer1.Size = new System.Drawing.Size(800, 500);
this.axWindowsMediaPlayer1.TabIndex = 2;
this.Controls.Add(this.axWindowsMediaPlayer1);
}
//初始化播放控件的視頻文件地址
protected void InitVedioUrl()
{
this.axWindowsMediaPlayer1.URL = @"D:/Vedio/default.wmv";
}
protected void InitEvent()
{
axWindowsMediaPlayer1.StatusChange += new EventHandler(axWindowsMediaPlayer1_StatusChange);
}
//通過控件的狀態改變,來實現視頻循環播放
protected void axWindowsMediaPlayer1_StatusChange(object sender, EventArgs e)
{
/*? 0 Undefined Windows Media Player is in an undefined state.(未定義)
1 Stopped Playback of the current media item is stopped.(停止)
2 Paused Playback of the current media item is paused. When a media item is paused, resuming playback begins from the same location.(停留)
3 Playing The current media item is playing.(播放)
4 ScanForward The current media item is fast forwarding.
5 ScanReverse The current media item is fast rewinding.
6 Buffering The current media item is getting additional data from the server.(轉換)
7 Waiting Connection is established, but the server is not sending data. Waiting for session to begin.(暫停)
8 MediaEnded Media item has completed playback. (播放結束)
9 Transitioning Preparing new media item.
10 Ready Ready to begin playing.(準備就緒)
11 Reconnecting Reconnecting to stream.(重新連接)
*/
//判斷視頻是否已停止播放
if ((int)axWindowsMediaPlayer1.playState == 1)
{
//停頓2秒鐘再重新播放
System.Threading.Thread.Sleep(2000);
//重新播放
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
}
四、OK 了,執行看效果。
以上代碼只是實現了單個視頻文件循環播放的效果,當然還可以播放一個視頻列表,這里就不再贅敘。
以下是關于windows media player的控件詳細說明:
[基本屬性]
URL:string 可以指定媒體位置
enableContextMenu:Boolean 顯示/不顯示播放位置的右鍵菜單
fullScreen:boolean 全屏顯示
stretchToFit:boolean 非全屏狀態時是否伸展到最佳大小
uMode:string 播放器的模式,full:有下面的控制條; none:只有播放部份沒有控制條
playState:integer 當前控件狀態,狀態變化時會觸發OnStatusChange事件
[controls]
可通過WindowsMediaPlayer.controls對播放器進行控制并取得相關的一些信息:
controls.play; 播放
controls.stop; 停止
controls.pause; 暫停
controls.currentPosition:Double 當前播放進度
controls.currentPositionString:string 時間格式的字符串 “0:32″
[currentMedia]
可以通過WindowsMediaPlayer.currentMedia取得當前媒體的信息
currentMedia.duration Double 總長度
currentMedia.durationString 時間格式的字符串 “4:34″
[settings]
可以通過WindowsMediaPlayer.settings對播放器進行設置,包括音量和聲道等。
settings.volume:integer 音量 (0-100)
settings.balance:integer 聲道,通過它應該可以進行立體聲、左聲道、右聲道的控制。
Media Player Player.playState獲取播放狀態事件
Value State Description
0 Undefined Windows Media Player is in an undefined state.(未定義)
1 Stopped Playback of the current media item is stopped.(停止)
2 Paused Playback of the current media item is paused. When a media item is paused, resuming playback begins from the same location.(停留)
3 Playing The current media item is playing.(播放)
4 ScanForward The current media item is fast forwarding.
5 ScanReverse The current media item is fast rewinding.
6 Buffering The current media item is getting additional data from the server.(轉換)
7 Waiting Connection is established, but the server is not sending data. Waiting for session to begin.(暫停)
8 MediaEnded Media item has completed playback. (播放結束)
9 Transitioning Preparing new media item.
10 Ready Ready to begin playing.(準備就緒)
11 Reconnecting Reconnecting to stream.(重新連接)
原文:
http://blog.csdn.net/slimboy123/archive/2010/06/23/5688616.aspx