C# 调用各种播放器代码大全

来源:互联网 发布:笑傲江湖片尾曲知乎 编辑:程序博客网 时间:2024/06/10 23:32

1、CD、VCD音频播放

首先需要添加Wiindows Media Player组件,方法:

 (1)“工具箱”右键“选择项”。

  (2)在弹出的“选择工具箱项”对话框中选择“COM组件”选项卡。

  (3)在COM组件列表中选择Windows Media Player,单击确定,ok。

事例代码:

        private void 选取文件_Click(object sender, EventArgs e)
        {
            this.optFile.ShowDialog();
            this.axWindowsMediaPlayer1.newMedia(this.optFile.FileName);
        }

        private void 播放文件_Click(object sender, EventArgs e)
        {
            this.axWindowsMediaPlayer1.URL = this.optFile.FileName;
        }

        private void 停止_Click(object sender, EventArgs e)
        {
            this.axWindowsMediaPlayer1.close();
        }

      暂停: this.axWindowsMediaPlayer1.Ctlcontrols.pause();

      继续播放:this.axWindowsMediaPlayer1.Ctlcontrols.play();

     获取歌曲信息:

      private WMPLib.WindowsMediaPlayerClass c;
        private WMPLib.IWMPMedia m;

        private void ButInfo_Click(object sender, EventArgs e)
        {
            if (this.optFile.FileName != "optFile")
            {
                c = new WMPLib.WindowsMediaPlayerClass();
                m = c.newMedia(this.optFile.FileName);
                MessageBox.Show("歌手名:" + m.getItemInfo("Author") + "/r/n" + "歌  名:" + m.getItemInfo("Title"));
            }
        }

2、MP3 、WAV播放

1)带记忆功能的MP3 播放器

   具有带记忆功能只需要在程序退出时,将当前用户选择的文件保存到ini文件中就可以了。

 如:在打开文件的同时写入到ini文件中:

        string strpath;
        public Form1()  //构造函数
        {
            InitializeComponent();
            strpath = System.Environment.CurrentDirectory;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.FileName = "";
            this.openFileDialog1.ShowDialog();
            StreamWriter s = new StreamWriter(strpath + "//HyList.ini", true);
            s.WriteLine(openFileDialog1.FileName);
            s.Flush();           
            s.Close();
            ShowWindows(openFileDialog1.FileName);  //调用函数
        }

       public void ShowWindows(string fileName)
        {
            this.listBox1.Items.Add(fileName);
        }

   在打开窗体的时候加载ini文件中的数据:

   private void Form1_Load(object sender, EventArgs e)
        {
            string str = Application.StartupPath;
            StreamReader sr = new StreamReader(str + "//HyList.ini");
            while (sr.Peek() >= 0)
            {
                string strk=sr.ReadLine();
                if (strk!="")
                    listBox1.Items.Add(strk);   
            }
            sr.Close();
        }

  播放选中文件:

    private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count > 0)
            {
                string strPath = listBox1.SelectedItem.ToString();

                ShowPlay(strPath);
            }          
        }

2)、自动播放的MP3播放器

  (1)自动添加播放列表:

    private void button1_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            if (this.folderBrowserDialog1.ShowDialog()==DialogResult.OK)
            {
                DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
                GetAllFiles(dir); //调用函数
            }
        }

    public void GetAllFiles(DirectoryInfo dir)
        {
            this.listBox1.Items.Clear();
            FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回表示某个目录中所有文件和子目录的强类型System.IO.FileSystemInfo项的数组
            foreach (FileSystemInfo i in fileinfo)
            {
                if (i is DirectoryInfo)
                {
                    GetAllFiles((DirectoryInfo)i);
                }
                else
                {
                    string str = i.FullName;
                    int b = str.LastIndexOf("//");
                    string strbbb = str.Substring(b + 1);
                    if (strbbb.Substring(strbbb.Length - 3) == "mp3")
                    {
                        this.listBox1.Items.Add(str.Substring(b + 1));
                        //添加列表
                        WC = new WMPLib.WindowsMediaPlayerClass();
                        MC = WC.newMedia(str);
                        this.axWindowsMediaPlayer1.currentPlaylist.appendItem(MC);
                    }
                }
            }
        }

 播放所有文件:

      private void button2_Click(object sender, EventArgs e)
        {

            if (MC != null)
                this.axWindowsMediaPlayer1.Ctlcontrols.play();
            else
                MessageBox.Show("请添加文件列表");
        }

3、播放动画

1)播放Flash动画

 首先需要计算机中有Flash插件,添加过程如下:

  (1)选择“工具箱”,单击鼠标右键,在弹出的快捷菜单中选择“选择项”。

  (2)弹出“选择工具箱项”对话框,选择“COM组件”选择卡。

  (3)在COM组件列表中,单击[浏览]按钮,在对话框中选择“//SYSTEM32/Macromed/Flash/SWFLASH.OCX”.

  打开文件事件:

       try
            {
                openFileDialog1.Filter = "Flash文件(*.swf)|*.swf|所有文件(*.*)|*.*";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string MyFileName = openFileDialog1.FileName;
                    this.axShockwaveFlash1.Movie = MyFileName;
                }
            }
            catch (Exception ey)
            {
                MessageBox.Show(ey.Message);
            }

    //播放
        private void button6_Click(object sender, EventArgs e)
        {
            this.axShockwaveFlash1.Rewind();
            this.axShockwaveFlash1.Play();
        }

     //第一帧
        private void button3_Click(object sender, EventArgs e)
        {
          this.axShockwaveFlash1.Rewind();
        }

     //上一帧
        private void button4_Click(object sender, EventArgs e)
        {
          this.axShockwaveFlash1.Back();  
        }

       //下一帧
        private void button5_Click(object sender, EventArgs e)
        {
          this.axShockwaveFlash1.Forward();
        }

       //停止
        private void button2_Click(object sender, EventArgs e)
        {
          this.axShockwaveFlash1.Stop();
        }

2)播放制作AVI播放器

  需添加“COM组件”中“Microsoft Animation Control,Version 6.0”

主要方法:  open()方法,eg. this.axAnimation1.Open(Application.StartupPath + "//clock.avi");

                   stop()方法,

                    Play()方法,播放文件。参数this.axAnimation1.Play(播放次数, 播放开始帧, 播放结束帧);

 4、播放Gif动画

        Bitmap bitmap = new Bitmap(Application.StartupPath+"//1.gif");
        bool current = false;

       播放事件:

        private void button1_Click(object sender, EventArgs e)
        {
            PlayImage();
            ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));//播放
        }

       public void PlayImage()
        {
            if (!current)
            {
                ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));
                current = true;
            }
        }

  停止播放:

     private void button2_Click(object sender, EventArgs e)
        {
            ImageAnimator.StopAnimate(bitmap, new EventHandler(this.OnFrameChanged));//停止
        }

原创粉丝点击