C#视频播放器

对于视频播放器来说,最重要的功能,莫过于播放视频文件了这就要用到VS自带的控件——Windows Media Player

windows media player

  • 将Windows Media Player(以下简称wmp)控件添加到工具箱中 
    1.选择工具箱项 
    2.选择com组件项 

    3.找到wmp,将其前面打钩即可在工具箱中找到wmp
  • 添加wmp至已创建好的窗体中
  • 下面说一说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——是否全屏显示

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

  • 当前播放列表属性[currentPlaylist] wmp.currentPlaylist

    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

    在视频播放之后,可以通过如下方式读取源视频的宽度和高度,然后设置其还原为原始的大小. 
    private void ResizeOriginal() 

    int intWidth = axWindowsMediaPlayer1.currentMedia.imageSourceWidth; 
    int intHeight = axWindowsMediaPlayer1.currentMedia.imageSourceHeight; 
    axWindowsMediaPlayer1.Width = intWidth + 2; 
    axWindowsMediaPlayer1.Height = intHeight + 2; 
    }

  • 注意:

    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便会显示文件播放长度。

  • Ctlcontrols属性 
    Ctlcontrols属性是AxWindowsMediaPlayer的一个重要属性, 此控件中有许多常用成员。 
    (1) 方法play 
    用于播放多媒体文件,其格式为: 
    窗体名.控件名.Ctlcontrols.play() 
    如: AxWindowsMediaPlayer1.Ctlcontrols.play() ‘此处缺省窗体名是Me 
    (2) 方法pause 
    用于暂停正在播放的多媒体文件,其格式为: 
    窗体名.控件名.Ctlcontrols.pause() 
    如: AxWindowsMediaPlayer1.Ctlcontrols.pause() 
    (3) 方法stop 
    用于停止正在播放的多媒体文件,其格式为: 
    窗体名.控件名.Ctlcontrols.stop() 
    如: AxWindowsMediaPlayer1.Ctlcontrols.stop() 
    (4) 方法fastforward 
    用于将正在播放的多媒体文件快进,其格式为: 
    窗体名.控件名.Ctlcontrols.fastforward() 
    如: AxWindowsMediaPlayer1.Ctlcontrols.forward() 
    (5) 方法fastreverse 
    窗体名.控件名.Ctlcontrols.fastreverse() 
    如: AxWindowsMediaPlayer1.Ctlcontrols.fastreverse()
  • 属性CurrentPosition 
    用于获取多媒体文件当前的播放进度,其值是数值类型,使用格式为: 
    窗体名.控件名.Ctlcontrols.currentPosition 
    d1 =AxWindowsMediaPlayer1.Ctlcontrols.currentPosition 
    其中d1 是一个整型变量。
  • 属性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;
        }

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
     【注意】:
要先定义变量
         string playpath = null;
        string directory = null;
        List<<string>string> playArray = null;

   
   
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

和线性表

`        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();
        }
`
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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();
        }


   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
0
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值