對於視頻播放器來講,最重要的功能,莫過於播放視頻文件了這就要用到VS自帶的控件——Windows Media Player
下面說一說wmp一些屬性
URL:String——指定媒體位置,本機或網絡地址
uiMode:String——播放器界面模式,可爲Full, Mini, None, Invisible(不計大小寫)
playState:int—— 播放狀態。這個屬性改變時同時引起PlayStateChange事件與StateChange事件。取值範圍爲枚舉型:WMPLib.WMPPlayState,它的成員以下:
wmppsUndefined = 0; //未知狀態
wmppsStopped = 1; //播放中止
wmppsPaused = 2; //播放暫停
wmppsPlaying = 3; //正在播放
wmppsScanForward = 4; //向前搜索
wmppsScanReverse = 5; //向後搜索
wmppsBuffering = 6; //正在緩衝
wmppsWaiting = 7; //正在等待流開始
wmppsMediaEnded = 8; //播放流已結束
wmppsTransitioning = 9; //準備新的媒體文件
wmppsReady = 10; //播放準備就緒
wmppsReconnecting = 11; //嘗試從新鏈接流媒體數據
wmppsLast = 12; //上一次狀態,狀態沒有改變
在PlayStateChange中寫代碼能夠防止播放rmvb等非默認類型的問題(用wmppsReady)。
enableContextMenu:Boolean——啓用/禁用右鍵菜單
fullScreen:boolean——是否全屏顯示windows
播放器基本控制
Ctlcontrols.play; 播放
Ctlcontrols.pause; 暫停
Ctlcontrols.stop; 中止
Ctlcontrols.currentPosition:double; 當前進度
Ctlcontrols.currentPositionString:string; 當前進度,字符串格式。如「00:23」
Ctlcontrols.fastForward; 快進
Ctlcontrols.fastReverse; 快退
Ctlcontrols.next; 下一曲
Ctlcontrols.previous; 上一曲網絡
播放器基本設置[settings] wmp.settings 工具
settings.volume:integer; 音量,0-100
settings.autoStart:Boolean; 是否自動播放
settings.mute:Boolean; 是否靜音
settings.playCount:integer; 播放次數
//順序播放
wmp.settings.setMode(「shuffle」, False)
//隨機播放
wmp.settings.setMode(「shuffle」, True)
//循環播放
wmp.settings.setMode(「loop」, True)oop
當前媒體屬性[currentMedia] wmp.currentMedia 動畫
currentMedia.duration:double; 媒體總長度
currentMedia.durationString:string; 媒體總長度,字符串格式。如「03:24」
currentMedia.getItemInfo(const string); 獲取當前媒體信息」Title」=媒體標題,」Author」=藝術家,」Copyright」=版權信息,」Description」=媒體內容描述,」Duration」=持續時間(秒),」FileSize」=文件大小,」FileType」=文件類型,」sourceURL」=原始地址
currentMedia.setItemInfo(const string); 經過屬性名設置媒體信息
currentMedia.name:string; 同 currentMedia.getItemInfo(「Title」)ui
當前播放列表屬性[currentPlaylist] wmp.currentPlaylist spa
currentPlaylist.count:integer; 當前播放列表所包含媒體數
currentPlaylist.Item[integer]; 獲取或設置指定項目媒體信息,其子屬性同wmp.currentMedia
axWindowsMediaPlayer1.currentMedia.sourceURL; //獲取正在播放的媒體文件的路徑
axWindowsMediaPlayer1.currentMedia.name; //獲取正在播放的媒體文件的名稱
axWindowsMediaPlayer1.Ctlcontrols.Play 播放
axWindowsMediaPlayer1.Ctlcontrols.Stop 中止
axWindowsMediaPlayer1.Ctlcontrols.Pause 暫停
axWindowsMediaPlayer1.Ctlcontrols.PlayCount 文件播放次數
axWindowsMediaPlayer1.Ctlcontrols.AutoRewind 是否循環播放 (無效)
axWindowsMediaPlayer1.Ctlcontrols.Balance 聲道
axWindowsMediaPlayer1.Ctlcontrols.Volume 音量
axWindowsMediaPlayer1.Ctlcontrols.Mute 靜音
axWindowsMediaPlayer1.EnableContextMenu 是否容許在控件上點擊鼠標右鍵時彈出快捷菜單
axWindowsMediaPlayer1.Ctlcontrols.AnimationAtStart 是否在播放前先播放動畫(無效)
axWindowsMediaPlayer1.Ctlcontrols.ShowControls 是否顯示控件工具欄(無效)
axWindowsMediaPlayer1.Ctlcontrols.ShowAudioControls 是否顯示聲音控制按鈕(無效)
axWindowsMediaPlayer1.Ctlcontrols.ShowDisplay 是否顯示數據文件的相關信息(無效)
axWindowsMediaPlayer1.Ctlcontrols.ShowGotoBar 是否顯示Goto欄(無效)
axWindowsMediaPlayer1.Ctlcontrols.ShowPositionControls 是否顯示位置調節按鈕(無效)
axWindowsMediaPlayer1.Ctlcontrols.ShowStatusBar 是否顯示狀態欄(無效)
axWindowsMediaPlayer1.Ctlcontrols.ShowTracker 是否顯示進度條(無效)
axWindowsMediaPlayer1.Ctlcontrols.FastForward 快進
axWindowsMediaPlayer1.Ctlcontrols.FastReverse 快退
axWindowsMediaPlayer1.Ctlcontrols.Rate 快進/快退速率
axWindowsMediaPlayer1.AllowChangeDisplaySize 是否容許自由設置播放圖象大小(無效)
axWindowsMediaPlayer1.DisplaySize 設置播放圖象大小(無效)
1-MpDefaultSize 原始大小
2-MpHalfSize 原始大小的一半
3-MpDoubleSize 原始大小的兩倍
4-MpFullScreen 全屏
5-MpOneSixteenthScreen 屏幕大小的1/16
6-MpOneFourthScreen 屏幕大小的1/4
7-MpOneHalfScreen 屏幕大小的1/2
axWindowsMediaPlayer1.ClickToPlay 是否容許單擊播放窗口啓動Media Player .net
在視頻播放以後,能夠經過以下方式讀取源視頻的寬度和高度,而後設置其還原爲原始的大小.
private void ResizeOriginal()
{
int intWidth = axWindowsMediaPlayer1.currentMedia.imageSourceWidth;
int intHeight = axWindowsMediaPlayer1.currentMedia.imageSourceHeight;
axWindowsMediaPlayer1.Width = intWidth + 2;
axWindowsMediaPlayer1.Height = intHeight + 2;
}code
注意:orm
AxWindowsMediaPlayer1.URL 中URL是表示要播放的文件名,取消了原來的Name屬性.
AxWindowsMediaPlayer1.Ctlcontrols.play()播放,一樣還有Pause,Stop等其餘屬性.
AxWindowsMediaPlayer1.settings.balance表示媒體播放的聲道設置,0表示均衡,-1和1表示左右聲道.
AxWindowsMediaPlayer1.currentMedia.duration 表示要播放的文件的時間長度.可用它獲取文件長度.
AxWindowsMediaPlayer1.Ctlcontrols.currentPosition表示正在播放的文件的當前播放位置,可用這個屬性來對媒體文件進行前進後退等設置.如
AxWindowsMediaPlayer1.Ctlcontrols.currentPosition+1 表示前進1個時間單位.
AxWindowsMediaPlayer1.settings.rate播放速率,通常乘以16後再顯示kbps單位.
注意:在上面程序中,若是在後面加上一個:
msgbox(AxWindowsMediaPlayer1.currentMedia.duration.ToString )
則顯示結果極可能爲0,所以,這時候極可能獲取不到文件的播放時間長度,容易出錯。因此在利用的時候能夠加一個timer控件:
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
EndPoint = AxWindowsMediaPlayer1.currentMedia.duration
If EndPoint = 0 Then Exit Sub ‘可能由於媒體文件的打開須要必定時間,這裏等待媒體文件的打開
msgbox(AxWindowsMediaPlayer1.currentMedia.duration.ToString )
End Sub
此時msgbox便會顯示文件播放長度。
屬性Duration
用於獲取當前多媒體文件的播放的總時間,其值爲數值類型,其使用格式爲:
窗體名.控件名.currentMedia.duration
如:d2 =AxWindowsMediaPlayer1.currentMedia.duration
其中d2是一個整型變量。
1.建立項目
選擇windows窗體應用程序,名稱爲mediaplayer
2.將form1的name屬性改成mediaplayer,text屬性改成視頻播放器。
3.添加menustrip,wmp控件。
4.調整他們之間的相互位置,並設置wmp的anchor屬性爲Top, Bottom, Left, Right以適應窗體調整時控件的大小及位置。
5.向menustrip中加入具體內容:
文件->打開文件->退出;
播放->播放/暫停->中止->快進->快退->上一曲->下一曲;
選項->升高音量->下降音量->靜音;
6.向打開文件中加入代碼:
雙擊打開文件,加入以下代碼
private void 打開文件ToolStripMenuItem_Click(object sender, EventArgs e) { using (OpenFileDialog fileDialog = new OpenFileDialog()) { fileDialog.Filter = "視頻文件(*.avi;*.wmv)|*.avi;*.wmv|(All file(*.*)|*.*"; if (fileDialog.ShowDialog() == DialogResult.OK) { //axWindowsMediaPlayer1.SizeMode = PictureBoxSizeMode.Zoom; playpath = fileDialog.FileName; // 初始化視頻集合 directory = Path.GetDirectoryName(playpath); playArray = player.GetplayCollection(directory); } } axWindowsMediaPlayer1.URL = playpath; }
【注意】: 要先定義變量
string playpath = null; string directory = null; List<<string>string> playArray = null;
和線性表
` public static List<string> GetplayCollection(string path) { string[] playarray = Directory.GetFiles(path); var result = from playstring in playarray where playstring.EndsWith("wmv", StringComparison.OrdinalIgnoreCase) || playstring.EndsWith("avi", StringComparison.OrdinalIgnoreCase) select playstring; return result.ToList(); }
7.接下來,向其餘menustrip項中分別加入代碼以下:
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void 播放/暫停ToolStripMenuItem_Click(object sender, EventArgs e) { if ((int)axWindowsMediaPlayer1.playState ==2) axWindowsMediaPlayer1.Ctlcontrols.play(); else if((int)axWindowsMediaPlayer1.playState==3) axWindowsMediaPlayer1.Ctlcontrols.pause(); } private void 中止ToolStripMenuItem_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.Ctlcontrols.stop(); } private void 快進ToolStripMenuItem_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.Ctlcontrols.fastForward(); } private void 快退ToolStripMenuItem_Click_1(object sender, EventArgs e) { axWindowsMediaPlayer1.Ctlcontrols.fastReverse(); } private void 上一曲ToolStripMenuItem_Click_1(object sender, EventArgs e) { axWindowsMediaPlayer1.Ctlcontrols.previous(); } private void 下一曲ToolStripMenuItem_Click_1(object sender, EventArgs e) { axWindowsMediaPlayer1.Ctlcontrols.next(); } private void 升高音量ToolStripMenuItem1_Click(object sender, EventArgs e) { if (axWindowsMediaPlayer1.settings.volume <= 90) { axWindowsMediaPlayer1.settings.volume += 10; } else { axWindowsMediaPlayer1.settings.volume = 100; } } private void 下降音量ToolStripMenuItem1_Click(object sender, EventArgs e) { if (axWindowsMediaPlayer1.settings.volume >= 10) { axWindowsMediaPlayer1.settings.volume -= 10; } else { axWindowsMediaPlayer1.settings.volume = 0; } } private void 靜音ToolStripMenuItem_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.settings.volume = 0; } private void 屬性ToolStripMenuItem1_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.ShowPropertyPages(); } private void 關於ToolStripMenuItem_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.ShowAboutBox(); }