在C#.NET windows應(yīng)用程序做視頻播放额嘿,首先要用到com組件中windows media player,當(dāng)然你也可以用其它的梧躺,這里就以windows media player為例似谁。
一、新建windows應(yīng)用程序項(xiàng)目掠哥,添加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);
}
//通過(guò)控件的狀態(tài)改變塞琼,來(lái)實(shí)現(xiàn)視頻循環(huán)播放
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.(轉(zhuǎn)換)
7 Waiting Connection is established, but the server is not sending data. Waiting for session to begin.(暫停)
8 MediaEnded Media item has completed playback. (播放結(jié)束)
9 Transitioning Preparing new media item.
10 Ready Ready to begin playing.(準(zhǔn)備就緒)
11 Reconnecting Reconnecting to stream.(重新連接)
*/
//判斷視頻是否已停止播放
if ((int)axWindowsMediaPlayer1.playState == 1)
{
//停頓2秒鐘再重新播放
System.Threading.Thread.Sleep(2000);
//重新播放
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
}
四、OK 了禁舷,執(zhí)行看效果彪杉。
以上代碼只是實(shí)現(xiàn)了單個(gè)視頻文件循環(huán)播放的效果,當(dāng)然還可以播放一個(gè)視頻列表牵咙,這里就不再贅敘派近。
以下是關(guān)于windows media player的控件詳細(xì)說(shuō)明:
[基本屬性]
URL:string 可以指定媒體位置
enableContextMenu:Boolean 顯示/不顯示播放位置的右鍵菜單
fullScreen:boolean 全屏顯示
stretchToFit:boolean 非全屏狀態(tài)時(shí)是否伸展到最佳大小
uMode:string 播放器的模式,full:有下面的控制條; none:只有播放部份沒有控制條
playState:integer 當(dāng)前控件狀態(tài)霜大,狀態(tài)變化時(shí)會(huì)觸發(fā)OnStatusChange事件
[controls]
可通過(guò)WindowsMediaPlayer.controls對(duì)播放器進(jìn)行控制并取得相關(guān)的一些信息:
controls.play; 播放
controls.stop; 停止
controls.pause; 暫停
controls.currentPosition:Double 當(dāng)前播放進(jìn)度
controls.currentPositionString:string 時(shí)間格式的字符串 “0:32″
[currentMedia]
可以通過(guò)WindowsMediaPlayer.currentMedia取得當(dāng)前媒體的信息
currentMedia.duration Double 總長(zhǎng)度
currentMedia.durationString 時(shí)間格式的字符串 “4:34″
[settings]
可以通過(guò)WindowsMediaPlayer.settings對(duì)播放器進(jìn)行設(shè)置构哺,包括音量和聲道等革答。
settings.volume:integer 音量 (0-100)
settings.balance:integer 聲道战坤,通過(guò)它應(yīng)該可以進(jìn)行立體聲、左聲道残拐、右聲道的控制途茫。
Media Player Player.playState獲取播放狀態(tài)事件
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.(轉(zhuǎn)換)
7 Waiting Connection is established, but the server is not sending data. Waiting for session to begin.(暫停)
8 MediaEnded Media item has completed playback. (播放結(jié)束)
9 Transitioning Preparing new media item.
10 Ready Ready to begin playing.(準(zhǔn)備就緒)
11 Reconnecting Reconnecting to stream.(重新連接)
原文:
http://blog.csdn.net/slimboy123/archive/2010/06/23/5688616.aspx