1. 程式人生 > >c#.NET winform 利用windows media player播放視訊,迴圈播放視訊

c#.NET winform 利用windows media player播放視訊,迴圈播放視訊


c#.NET winform 利用windows media player播放視訊,迴圈播放視訊
2010年06月23日 10:52:00 slimboy123 閱讀數:15647 標籤: windows winform playback integer string session 更多
個人分類: Dot NET技術/C#.NET/ASP.NET/Windows Service/WebService/Socket/XML 平時整理 .NET技術
c#.NET winform 利用windows media player播放視訊,迴圈播放視訊
開發環境:VS2005

原文:http://blog.csdn.net/slimboy123/archive/2010/06/23/5688616.aspx

(轉載請說明出處)

在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 (轉載請說明出處)