您的位置:首页 > 其它

初试WindowsMediaPlayer

2012-07-03 00:54 344 查看
用WindowsMediaPlayer控件写了一个小播放器,只是一个小尝试

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

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

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



附:

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);
        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: