首页 新闻 搜索 专区 学院

C#播放器问题求解

0
悬赏园豆:5 [已关闭问题]

我在导入音乐文件时采用了两种方式,第一种是直接选取整个文件夹,然后利用if来判断是否是.mp3格式的。第二种是每次只能导入一首歌曲。(播放函数play()已经弄好了)

用两个listView分别来演示:listMp3和listView1

首先第一种方案(选取整个文件夹):  (成功播放)

private void button1_Click(object sender, EventArgs e)
        {
            listMp3.ContextMenuStrip = contextMenuStrip1;  //实现右键点击弹出选项功能。
            FolderBrowserDialog fbDialog = new FolderBrowserDialog();
            string[] fileEntries;
            //fbDialog.SelectedPath = "D:\\music\\My Musics";

            if (fbDialog.ShowDialog() == DialogResult.OK && fbDialog.SelectedPath.Length > 0)
            {
                int pathCount = fbDialog.SelectedPath.Length;
                string path = fbDialog.SelectedPath;
                fileEntries = Directory.GetFiles(path);
                string fileName;
                for (int i = 0; i < fileEntries.Length; i++)
                {
                    fileName = fileEntries[i];
                    if (fileName.EndsWith(".mp3"))
                    {
                        listMp3.Items.Add(fileName.Substring(pathCount + 1), i);
                        listMp3.Items[listMp3.Items.Count - 1].Name = path;
                        listMp3.Visible = true;
                        listMp3.Items[0].Selected = true;

                    }
                }
            }
        }

 

private void btnplay1_Click(object sender, EventArgs e)
        {
            string sPath = "";
            string sFileName = "";
            string sFile = "";

            try
            {
                    sPath = listMp3.SelectedItems[0].Name;
                    sFileName = listMp3.SelectedItems[0].Text;
                    sFile = sPath + "\\" + sFileName;
            }
            catch
            {
                  string Message = "请引入音乐";
                  string Caption = "未引入音乐";
                  MessageBox.Show(Message, Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }

            if (sPath != "" && sFileName != "")
            {
                  Sound.Play(sFile);
            }
        }

第二种方案(每次只能导入一首歌曲):(播放失败)

private void button2_Click(object sender, EventArgs e)
        {
            listView1.ContextMenuStrip = contextMenuStrip1;    //实现右键点击弹出选项功能。
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                foreach (string s in openFileDialog1.FileNames)
                {
                    listView1.Items.Add(s);
                }
            }
        }

 

private void btnplay2_Click(object sender, EventArgs e)
        {
            string sPath = "";
            string sFileName = "";
            string sFile = "";

            try
            {
                sPath = listView1.SelectedItems[0].Name;
                sFileName = listView1.SelectedItems[0].Text;
                sFile = sPath + "\\" + sFileName;
            }
            catch
            {
                string Message = "请引入音乐";
                string Caption = "未引入音乐";
                MessageBox.Show(Message, Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }

            if (sPath != "" && sFileName != "")
            {
                Sound.Play(sFile);
            }
        }

以上两种方式应该相差不大,但是第二种方案最后那句 if (sPath != "" && sFileName != "")没有通过,是不是路径有问题?怎么改?

谢谢,以上可能一点长,不过还算清楚,请大家帮忙看看。。。

黑夜无痕的主页 黑夜无痕 | 初学一级 | 园豆:190
提问于:2009-08-29 22:12
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册