C#播放器控件的常用方法介紹


右擊工具箱->選擇項(I)... -> 顯示"選擇工具箱項" -> COM組件 -> Windows Media Player   wmp.dll 添加

[基本屬性]  
URL:String; 指定媒體位置,本機或網絡地址
uiMode:String; 播放器界面模式,可為Full, Mini, None, Invisible(不計大小寫)
playState:integer; 播放狀態。這個屬性改變時同時引發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;         是否全屏顯示

  //播放器基本控制
  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)

[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")

 

基本設置實例:

axWindowsMediaPlayer1.windowlessVideo = false;   //設為false后雙擊屏幕可以全屏
axWindowsMediaPlayer1.fullScreen = true; //設播放器全屏播放

axWindowsMediaPlayer1.URL = @"mms://192.168.0.102/vod/jingwei.wma";//播放資源

axWindowsMediaPlayer1.Ctlcontrols.play();        //播放
axWindowsMediaPlayer1.Ctlcontrols.stop();        //停止
axWindowsMediaPlayer1.Ctlcontrols.pause();       //暫停

axWindowsMediaPlayer1.settings.autoStart = true;     //自動播放

axWindowsMediaPlayer1.settings.mute = false;         //靜音
axWindowsMediaPlayer1.settings.volume = 100;      // 音量 int 0 ~ 100   100 是最大音量

axWindowsMediaPlayer1.currentMedia.duration.ToString();//影片長度
axWindowsMediaPlayer1.Ctlcontrols.currentPosition = 30; //當前的播放位置 double

axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");//標題
axWindowsMediaPlayer1.currentMedia.getItemInfo("Author");//作者

 

 

 

全屏控制實例代碼:

using System.IO;
using WMPLib; 

public videoPlay()
        {
            InitializeComponent();

            //全屏設置及隱藏鼠標
            this.WindowState = FormWindowState.Maximized;
            this.FormBorderStyle = FormBorderStyle.None;
            //Cursor.Hide();
            //播放器全屏
            Rectangle screenSize = System.Windows.Forms.SystemInformation.VirtualScreen;//獲取屏幕的寬和高
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Size = new System.Drawing.Size(screenSize.Width,screenSize.Height);
            this.axWindowsMediaPlayer1.Location = new System.Drawing.Point(0, 0);
            this.axWindowsMediaPlayer1.Size = new System.Drawing.Size(screenSize.Width, screenSize.Height);
            //播放器設置
            axWindowsMediaPlayer1.uiMode = "None";//播放器樣式
            axWindowsMediaPlayer1.stretchToFit = true;//非全屏狀態時是否伸展到最佳大小 
            axWindowsMediaPlayer1.enableContextMenu = false;//禁用播放器右鍵菜單
       

        }

        private IWMPPlaylist videoList;//創建播放列表
        private bool ifLoop = true;//視頻是否循環

        //設置是否循環播放
        public bool IfLoop
        {
            get { return ifLoop; }
            set { ifLoop = value; }
        }

        //播放狀態改變時發生
        private void axWindowsMediaPlayer1_StatusChange(object sender, EventArgs e)
        {
            //判斷視頻是否已停止播放  
            if ((int)axWindowsMediaPlayer1.playState == 1)
            {
                //停頓2秒鍾再重新播放  
                //System.Threading.Thread.Sleep(2000);
                //重新播放  
                //axWindowsMediaPlayer1.Ctlcontrols.play();
            }
        }
        //播放
        public void videoStart()
        {
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }
        //列表播放
        public void videoListStart()
        {
            videoPlayList();//重新獲取播放列表
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }
        //暫停
        public void videoPause()
        {
            axWindowsMediaPlayer1.Ctlcontrols.pause();
        }
        //重播
        public void videoReplay()
        {
            videoStop();
            videoStart();
        }
        //列表重播
        public void videoListReplay()
        {
            axWindowsMediaPlayer1.currentPlaylist = videoList;//重新載入播放列表
            videoStart();
        }
        //停止播放
        public void videoStop() 
        {            
            //axWindowsMediaPlayer1.currentPlaylist.clear();//清除列表
            axWindowsMediaPlayer1.Ctlcontrols.stop();
        }
        //視頻靜音
        public void videoMute(bool t)
        {
            axWindowsMediaPlayer1.settings.mute = t;
        }
        //播放下一個視頻
        public void videoNext()
        {
            //判斷當前所播放的視頻是否是列表的最后一個
            if (axWindowsMediaPlayer1.currentMedia.name == axWindowsMediaPlayer1.currentPlaylist.Item[axWindowsMediaPlayer1.currentPlaylist.count - 1].name)
            {
            }
            else
            {
                axWindowsMediaPlayer1.Ctlcontrols.next();//播放下一個
            }
        }
        //播放上一個媒體
        public void videoPrevious()
        {  //判斷當前所播放的視頻是否是列表的第一個
            if (axWindowsMediaPlayer1.currentMedia.name == axWindowsMediaPlayer1.currentPlaylist.Item[0].name)
            {
            }
            else
            {
                axWindowsMediaPlayer1.Ctlcontrols.previous();//播放上一個
            }
        }

        //獲取播放類表及初始化
        public void videoPlayList() 
        {
            videoList = axWindowsMediaPlayer1.playlistCollection.newPlaylist("one");//創建播放列表
            string path = @".\data\video";//媒體路徑
            DirectoryInfo dir = new DirectoryInfo(path);
            foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
            {
                if (fsi is FileInfo)
                {
                    FileInfo fi = (FileInfo)fsi;
                    videoList.appendItem(axWindowsMediaPlayer1.newMedia(fi.FullName));
                }
            }
            axWindowsMediaPlayer1.currentPlaylist = videoList;//查找到視頻、播放類表
            axWindowsMediaPlayer1.settings.setMode("loop", ifLoop);//設置類表循環播放
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM