基本信息
源码名称:C# 简易播放器工具源码(可用于年会节目)
源码大小:0.70M
文件格式:.zip
开发语言:C#
更新时间:2019-04-14
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

1. 添加标签,然后在标签下 添加音乐


using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;	//Path类用到
//using System.Media;    //SoundPlayer命名空间
//using System.Runtime.InteropServices;


namespace MyMusic
{
    public partial class MusicMainFrom : Form
    {

        //List<string> listsongs = new List<string>();   //用来存储音乐文件的全路径
        Dictionary<string, string> songPath = new Dictionary<string, string>();
        //SoundPlayer sp = new SoundPlayer();
        OpenFileDialog ofd = new OpenFileDialog();
        List<ListBox> listBoxs = new List<ListBox>();
        

        public MusicMainFrom()
        {
            InitializeComponent();
            lbErr.Text = "";
        }
        /**
         * 添加一个标签页
         * */
        private void BtnNewTab_Click(object sender, EventArgs e)
        {
            AddControls();
            txName.Text = "";
        }
        /**
         * 添加歌曲到播放列表
         * */
        private void BtnAdd_Click(object sender, EventArgs e)
        {
            FileDirectory();
        }
        
        /**
         * 下一曲
         **/
        private void BtnNext_Click(object sender, EventArgs e)
        {
            int index = listBoxs[tcCentent.SelectedIndex].SelectedIndex; //获得当前选中歌曲的索引
            index  ;
            SongIndex(index);


        }
        /**
         * 上一曲
         * */
        private void BtnPre_Click(object sender, EventArgs e)
        {
            int index = listBoxs[tcCentent.SelectedIndex].SelectedIndex; //获得当前选中歌曲的索引
            index--;

            SongIndex(index);
        }

        /**
         * 播放
         * */
        private void BtnPlay_Click(object sender, EventArgs e)
        {
            int index = listBoxs[tcCentent.SelectedIndex].SelectedIndex; //获得当前选中歌曲的索引
            SongIndex(index);
        }

        private void BtnStop_Click(object sender, EventArgs e)
        {
            string songName = Convert.ToString(listBoxs[tcCentent.SelectedIndex].SelectedItem);
            //sp.SoundLocation = songPath[songName];
            //sp.Stop();
            axWindowsMedia.Ctlcontrols.stop();


        }
        //移除对应的listBoxs的对应歌曲/视频
        private void BtnDelListBox_Click(object sender, EventArgs e)
        {
            int index = listBoxs[tcCentent.SelectedIndex].SelectedIndex;
            if (index != -1){
                lbErr.Text = "";
                listBoxs[tcCentent.SelectedIndex].Items.RemoveAt(listBoxs[tcCentent.SelectedIndex].SelectedIndex);
            }
            else{
                lbErr.Text = "请选择歌曲/视频再删除";
            }
            
        }

        //选择文件并把路径记录在listBoxs上
        private void FileDirectory()
        {
            ofd.Title = "请选择音乐/视频文件";      //打开对话框的标题
            ofd.InitialDirectory = @"F:\music-cut";    //设置打开对话框的初始设置目录
            ofd.Multiselect = true; //设置多选
            ofd.Filter = @"音乐文件|*.mp3|音乐文件|*.wav|视频文件|*.mp4|所有文件|*.*";    //设置文件格式筛选
            ofd.ShowDialog();   //显示打开对话框
            
            string[] pa_th = ofd.FileNames;       //获得在文件夹中选择的所有文件的全路径
            
            if (null != pa_th && pa_th.Length != 0)
            {
                for (int i = 0; i < pa_th.Length; i  )
                {
                    if(songPath.ContainsKey(Path.GetFileName(pa_th[i])))
                    {
                        return;
                    }
                    listBoxs[tcCentent.SelectedIndex].Items.Add(Path.GetFileName(pa_th[i]));  //将音乐文件的文件名加载到listBox中
                    songPath.Add(Path.GetFileName(pa_th[i]), pa_th[i]);    //将音乐文件的全路径存储到泛型集合中
                }
            }
        }
        //歌曲下标
        private void SongIndex(int index)
        {
            if(listBoxs[tcCentent.SelectedIndex].Items.Count == 0)
            {
                lbErr.Text = "请添加歌曲/视频";
                return;
            }   
            if (index == listBoxs[tcCentent.SelectedIndex].Items.Count)
            {
                index = 0;
            }
            lbErr.Text = "";
            listBoxs[tcCentent.SelectedIndex].SelectedIndex = index; //将改变后的索引重新赋值给我当前选中项的索引
            string songName = Convert.ToString(listBoxs[tcCentent.SelectedIndex].SelectedItem);
            //sp.SoundLocation = songPath[songName];
            //sp.Play();
            if (songPath.ContainsKey(songName))
            {
                lbErr.Text = "";
                axWindowsMedia.URL = songPath[songName];
            }
            else
            {
                lbErr.Text = "请选择歌曲/视频";
            }
            
        }

        //添加控件
        private void AddControls()
        {
            TabPage page = new TabPage
            {
                Name = txName.Text,
                Text = txName.Text
            };
            page.TabIndex = page.TabIndex   1;
            
            Button button = new Button();
            button.SetBounds(520, 10, 126, 25);
            button.Text = "添加音乐";
            button.Click  = new EventHandler(BtnAdd_Click);
            page.Controls.Add(button);

            Button button2 = new Button();
            button2.SetBounds(656, 10, 126, 25);
            button2.Text = "删除音乐";
            button2.Click  = new EventHandler(BtnDelListBox_Click);
            page.Controls.Add(button2);

            Button button3 = new Button();
            button3.SetBounds(520, 55, 100, 25);
            button3.Text = "播放";
            button3.Click  = new EventHandler(BtnPlay_Click);
            page.Controls.Add(button3);

            Button button4 = new Button();
            button4.SetBounds(640, 55, 100, 25);
            button4.Text = "停止";
            button4.Click  = new EventHandler(BtnStop_Click);
            page.Controls.Add(button4);

            Button button5 = new Button();
            button5.SetBounds(520, 90, 100, 25);
            button5.Text = "上一曲";
            button5.Click  = new EventHandler(BtnPre_Click);
            page.Controls.Add(button5);

            Button button6 = new Button();
            button6.SetBounds(640, 90, 100, 25);
            button6.Text = "下一曲";
            button6.Click  = new EventHandler(BtnNext_Click);
            page.Controls.Add(button6);

            ListBox listBox = new ListBox();
            listBox.SetBounds(10, 10, 500, 200);
            page.Controls.Add(listBox);
            listBoxs.Add(listBox);

            this.tcCentent.Controls.Add(page);
        }

        /**
         * 删除标签页
         * */
        private void BtnDelTab_Click(object sender, EventArgs e)
        {
            listBoxs.RemoveAt(tcCentent.SelectedIndex);
            tcCentent.Controls.RemoveAt(tcCentent.SelectedIndex);
        }
    }
}