简化版音乐播放器代码

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;using System.Collections...
摘要由CSDN通过智能技术生成
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;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Configuration;
using System.Media;

namespace 音乐播放器
{
    public partial class 音乐播放器 : Form
    {
        public 音乐播放器()
        {
            InitializeComponent();
        }
        #region 窗体加载事件 加载内容
        private void Form1_Load(object sender, EventArgs e)
        {
            #region 设置默认播放列表
            Directory.CreateDirectory(@"d:\cloud");             //建立默认文件夹
            FileInfo fi = new FileInfo(@"d:\cloud\info.list");   //声明文件对象
            if (fi.Exists && fi.Length > 0)                    //判断是否存在默认文件并且文件不为空
            {
                Read(@"d:\cloud\info.list");                    //读取默认路径的文件
            }
           
            #endregion
        }
        #endregion
        #region 调用方法
        #region 把文件添加到列表中
        public static int count = 1;
        //把选中歌曲内容加到列表中方法
        public void SetPlayList(string[] s)
        {
            string strType = "mp3|mp4|wma";
            ArrayList list = new ArrayList();
            //遍历字符串数组s
            foreach (string lis in s)
            {      //从路径lis中截取出歌曲的后缀名
                string str = lis.Substring(lis.LastIndexOf(".") + 1);//取点后边的字符串
                //将后缀名类型以'|'分割成字符串数组
                string[] st = strType.Split('|');
                //将歌曲的后缀名分别与给出的类型配对
                foreach (string stt in st)
                {
                    if (str == stt)
                    {    //声明一个WMPLib.IWMPMedia的对象media,该对象可以添加到播放器的播放清单中
                        WMPLib.IWMPMedia media = this.axWindowsMediaPlayer1.newMedia(lis);
                        //添加到播放器的播放清单中
                        this.axWindowsMediaPlayer1.currentPlaylist.appendItem(media);
                        //按顺序播放清单中的文件
                        this.axWindowsMediaPlayer1.Ctlcontrols.play();
                        //初始化一个ListViewItem类型的实例
                        ListViewItem item = new ListViewItem();
                        string name = Path.GetFileName(lis);
                        string[] sna = name.Split('.');//将歌曲名以“.”分割成名字和后缀名
                        item.Text = "" + count;//取序号
                        //把播放的歌曲信息添加到播放列表中
                        item.SubItems.Add(sna[0]);//取名字部分
                        item.SubItems.Add(lis);//取地址
                        listView1.Items.Add(item);//把每一个子项添加到项中
                        count++;//每添加一项后序号+1
                    }
                }
            }
        }
        #endregion

        /// <summary>
        /// 读取列表
        /// </summary>
        /// <param name="path">用户选择的读取列表路径</param>
        private void Read(string path)
        {
            StreamReader sr = new StreamReader(path);               //声明写读对象
           
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值