C# axWindowsMediaPlayer制作播放器

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VideoGoogle
{
    //选择菜单中的“工具”--“自定义工具箱”,打开“自定义工具箱”窗口,在“COM 组件”中选择“Windows Media Player”播放器和
    //“Shockwave Flash Object”flash播放器
    
    public partial class Form1 : Form
    {
        private static string PathBase = System.AppDomain.CurrentDomain.BaseDirectory;//目录

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //this.axWindowsMediaPlayer1.uiMode = "none";//右键axWindowsMediaPlayer1--属性--常规--控件布局--选择模式--None
            this.tbarVolume.Minimum = 0;                                               //設定音量調整Bar最小值為最小音量值(0)
            this.tbarVolume.Maximum = 100;                                             //設定音量調整Bar最大值為最大音量值(100)
            this.tbarVolume.Value = this.axWindowsMediaPlayer1.settings.volume;
        }

        //打开
        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            //设置为可以打开多个文件
            openFileDialog.Multiselect = true;
            //设置打开文件格式
            openFileDialog.Filter = "Mp3文件|*.mp3|Wav文件|*.wav|Wma文件|*.wma|Wmv文件|*.wmv|所有格式|*.*";
            //判断是否单击确定按钮
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //建立播放列表,名字为aa
                axWindowsMediaPlayer1.currentPlaylist = axWindowsMediaPlayer1.newPlaylist("aa", "");
                //遍历打开的集合
                foreach (string fn in openFileDialog.FileNames)
                {
                    //添加播放列表
                    axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(fn));
                }
            }
            //播放
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }

        //播放视频
        private void button1_Click(object sender, EventArgs e)
        {
            string Path = PathBase + "航架2.mpg";
            this.axWindowsMediaPlayer1.URL = Path;
            this.axWindowsMediaPlayer1.Ctlcontrols.play();  
        }

        //播放MP3
        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "mp3文件|*.mp3";
            if (DialogResult.OK == openFile.ShowDialog())
            {
                axWindowsMediaPlayer1.URL = openFile.FileName;
            }
        }

        //设置
        private void button6_Click(object sender, EventArgs e)
        {
            string Duration = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("Duration");//持续时间(秒)
            string Title = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");//媒体标题
            string Author = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("Author");//艺术家
            string Copyright = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("Copyright");//版权信息
            string Description = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("Description");//媒体内容描述
            string FileSize = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("FileSize");//文件大小
            string FileType = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("FileType");//文件类型
            string sourceURL = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("sourceURL");//原始地址
            MessageBox.Show("持续时间:"+Duration+"||"+"媒体标题:"+Title+"||"+"艺术家:"+Author+"||"+"版权信息:"+Copyright+"||"+"媒体内容描述:"+Description+"||"+"文件大小:"+FileSize+"||"+"文件类型:"+FileType+"||"+"原始地址:"+sourceURL);

            string name = this.axWindowsMediaPlayer1.currentMedia.name;
            string url = this.axWindowsMediaPlayer1.currentMedia.sourceURL;

            //设置
            //this.axWindowsMediaPlayer1.currentMedia.setItemInfo("Title", "航架");     

            this.axWindowsMediaPlayer1.settings.balance = 0;//表示媒体播放的声道设置,0表示均衡,-1和1表示左右声道
            double rate = this.axWindowsMediaPlayer1.settings.rate;//播放速率,一般乘以16后再显示kbps单位.
            //this.axWindowsMediaPlayer1.currentMedia.setItemInfo("rate", "2");
        }

        //打开Flash
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofDialog = new OpenFileDialog();
            ofDialog.AddExtension = true;
            ofDialog.CheckFileExists = true;
            ofDialog.CheckPathExists = true;
            ofDialog.Filter = "Flash文件(*.swf)|*.swf|所有文件(*.*)|*.*"; ;// "swf 文件 (*.swf)|*.swf|所有文件 (*.*)|*.*";
            ofDialog.DefaultExt = "mp3";
            if (ofDialog.ShowDialog() == DialogResult.OK)
            {
                this.axShockwaveFlash1.Movie = ofDialog.FileName;
            }

            //==========================================
            //openFileDialog1.Filter = "Flash文件(*.swf)|*.swf|所有文件(*.*)|*.*";
            //if (openFileDialog1.ShowDialog() == DialogResult.OK)
            //{
            //    string MyFileName = openFileDialog1.FileName;
            //    this.axShockwaveFlash1.Movie = MyFileName;
            //}
        }

        //Flash
        private void button3_Click(object sender, EventArgs e)
        {
            this.axShockwaveFlash1.Stop();//暂停播放
            this.axShockwaveFlash1.Rewind();//播放第一帧
            this.axShockwaveFlash1.Back();//播放上一帧
            this.axShockwaveFlash1.Forward();//开始播放
            this.axShockwaveFlash1.Rewind();//播放下一帧
            this.axShockwaveFlash1.Play();//播放下一帧
        }

        //play
        private void playToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }

        //pause
        private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.pause();
        }

        //stop
        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();
        }

        //open
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            //设置为可以打开多个文件
            openFileDialog.Multiselect = true;
            //设置打开文件格式
            openFileDialog.Filter = "Mp3文件|*.mp3|Wav文件|*.wav|Wma文件|*.wma|Wmv文件|*.wmv|所有格式|*.*";
            //判断是否单击确定按钮
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //建立播放列表,名字为aa
                axWindowsMediaPlayer1.currentPlaylist = axWindowsMediaPlayer1.newPlaylist("aa", "");
                //遍历打开的集合
                foreach (string fn in openFileDialog.FileNames)
                {
                    //添加播放列表
                    axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(fn));
                }
            }
            //播放
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }

        //exit
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        //上一个
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.previous();
        }

        //播放
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }

        //下一个
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.next();
        }

        //暂停
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.pause();
        }

        //停止
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();
        }

        //进度
        private void timer1_Tick(object sender, EventArgs e)
        {
            double total=0.0d;
            if (this.axWindowsMediaPlayer1.currentMedia != null)
            {
                total = Math.Ceiling(this.axWindowsMediaPlayer1.currentMedia.duration);

                this.tbarPlayLoaction.Maximum = (int)this.axWindowsMediaPlayer1.currentMedia.duration;//設定撥放位置調整Bar最大值                    
            }
            double cur = Math.Floor(this.axWindowsMediaPlayer1.Ctlcontrols.currentPosition);
            if (total != 0)
            {
                this.progressBar1.Value = (int)(cur / total *100);
                this.label1.Text = cur.ToString()+"s" + "/" + total.ToString()+"s";

                this.tbarPlayLoaction.Value = (int)(cur / total * total);
            }

            //---------------------------------
            ////循环播放
            //if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsStopped)
            //{
            //    axWindowsMediaPlayer1.Ctlcontrols.play();
            //}
        }

        //快进
        private void button7_Click(object sender, EventArgs e)
        {
            this.axWindowsMediaPlayer1.Ctlcontrols.fastForward();
        }

        //快退
        private void button8_Click(object sender, EventArgs e)
        {
            this.axWindowsMediaPlayer1.Ctlcontrols.fastReverse();
        }

        //自动播放
        private void autoStartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.autoStartToolStripMenuItem.Checked)
            {
                this.axWindowsMediaPlayer1.settings.autoStart = true;
            }
        }

        //播放
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            this.axWindowsMediaPlayer1.Ctlcontrols.play();
        }

        //暂停
        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            this.axWindowsMediaPlayer1.Ctlcontrols.pause();
        }

        //显示当前音量
        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            int volume=this.axWindowsMediaPlayer1.settings.volume;
            MessageBox.Show("音量:"+volume.ToString());

            //this.axWindowsMediaPlayer1.currentMedia.setItemInfo("volume", "60");     
        }

        //改变音量大小
        private void tbarVolume_Scroll(object sender, EventArgs e)
        {
            this.axWindowsMediaPlayer1.settings.volume = this.tbarVolume.Value;      //改變音量大小
        }

        //音量打下++
        private void button9_Click(object sender, EventArgs e)
        {
            this.axWindowsMediaPlayer1.settings.volume += 1;       //音量大小+1
            int volume = this.axWindowsMediaPlayer1.settings.volume;
            this.tbarVolume.Value = volume;
        }

        //音量大小--
        private void button10_Click(object sender, EventArgs e)
        {
            this.axWindowsMediaPlayer1.settings.volume -= 1;       //音量大小-1
            int volume = this.axWindowsMediaPlayer1.settings.volume;
            this.tbarVolume.Value = volume;
        }

        //播放位置
        private void tbarPlayLoaction_Scroll(object sender, EventArgs e)
        {
            this.axWindowsMediaPlayer1.Ctlcontrols.currentPosition = tbarPlayLoaction.Value;
        }

        //循环播放
        private void loopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.loopToolStripMenuItem.Checked)
            {
                ////第一种:
                //if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsStopped)
                //{
                //    axWindowsMediaPlayer1.Ctlcontrols.play();
                //}

                ////第二种:
                axWindowsMediaPlayer1.settings.setMode("loop", true);
            }
        }

 
    

        
    }
}

原文地址:https://www.cnblogs.com/IT-haidong/p/4730997.html