用WindowsMediaPlayer控件写个WinForm播放器

用WindowsMediaPlayer控件写了一个小播放器,只是一个小尝试

首先要添加Windows Media Player到工具箱

右击工具箱->选择项(I)... -> 显示"选择工具箱项" -> COM组件 -> Windows Media Player   wmp.dll 添加

然后拖了一个 Windows Media Player控件、两个按钮、一个ListBox。

用WindowsMediaPlayer控件写个WinForm播放器

附:

        private void button1_Click(object sender, EventArgs e)

        {

            AddMusicToListBox(listBox1);

        }



        private void AddMusicToListBox(ListBox lb)

        {

            string[] files = GetMusicFiles();

            if (files != null && files.Length > 0)

            {

                foreach (string file in files)

                {

                    if (!lb.Items.Contains(file))

                    {

                        lb.Items.Add(file);

                    }

                }

            }

        }



        private string[] GetMusicFiles()

        {

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Multiselect = true;//设置 选择多个文件

            ofd.InitialDirectory = @"G:\MUSIC";

            ofd.Filter = "(MP3文件)|*.mp3";

            if (ofd.ShowDialog() == DialogResult.OK)

            {

                return ofd.FileNames;

            }

            else

            {

                return null;

            }       

        }



        private void listBox1_DoubleClick(object sender, EventArgs e)

        {

            axWindowsMediaPlayer1.currentPlaylist.clear();

            //MessageBox.Show(listBox1.SelectedItem.ToString());

            WMPLib.IWMPMedia song = axWindowsMediaPlayer1.newMedia(listBox1.SelectedItem.ToString());

            axWindowsMediaPlayer1.currentPlaylist.appendItem(song);

            axWindowsMediaPlayer1.Ctlcontrols.play();

        }



        private void button2_Click(object sender, EventArgs e)

        {

            RemoveMusicFromListBox(listBox1);

        }



        private void RemoveMusicFromListBox(ListBox lb)

        {

            lb.Items.Remove(lb.SelectedItem);

        }

你可能感兴趣的:(mediaplayer)