注册 登录
编程论坛 C# 论坛

自编mp3音乐播放器

qq1023569223 发布于 2011-04-11 20:00, 7155 次点击
只有本站会员才能查看附件,请 登录

   
只有本站会员才能查看附件,请 登录

自已花一天时间做的播放器,汗!基本上可以了!但是有的地方不行,比如我那个暂停的功能不行,
菜单中的退出程序不行。我想完善它,所以请各位高手不吝赐教!
要下的朋友请到贴子后面的地方下,后面的功能更好!

[ 本帖最后由 qq1023569223 于 2011-4-21 07:09 编辑 ]
52 回复
#2
a1010692011-04-11 20:29
界面要加载什么东东?怎么打不开?
#3
qq10235692232011-04-11 21:03
不要啊,解压后找到My MusicPlayer.exe双击就可以用!不过要求你的电脑装有.NET FrameWork2.0!
#4
qq10235692232011-04-12 11:00
人气不太好,我自己顶!
#5
Alar302011-04-12 11:05
支持原创哇。。。
#6
qq10235692232011-04-12 11:11
回复 5楼 Alar30
谢谢!
#7
凉粉2011-04-12 11:35
好牛啊  教教我啊
#8
ghm52492112011-04-12 13:25
牛逼
#9
romantic_912011-04-12 14:20
怎么做呢?怎么用visual studio打开呢?
#10
qq10235692232011-04-12 17:53
回复 9楼 romantic_91
打开.sln文件就可以的!
#11
zkmhywgsym2011-04-12 18:40
#12
angelline2011-04-13 12:09
这个怎么弄出来的 源码分享一下
#13
qq10235692232011-04-13 18:03
回复 12楼 angelline
我上传的就是整个工程,你下下来就有了!
#14
zhangyf03032011-04-13 21:44
打不开啊~~~没看到代码....求指教
#15
zhangyf03032011-04-13 21:46
OK了。。。打开了,一开始没找到
#16
a1010692011-04-13 21:51
请教lz,随机播放怎么写啊/?请教lz,随机播放怎么写啊/?
#17
qq10235692232011-04-13 22:05
回复 16楼 a101069
这个还要考虑一下。应该是另外建立一个播放列表,由已存在的列表中随机生成,然后用这个列表来播放!还不知道怎么样新建列表!
#18
ak1102402011-04-13 22:36
求源代码 !可否给我看看!
#19
qq10235692232011-04-13 22:39
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using namespace MusicPlayer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.BeginInit();  //初始化
            axWindowsMediaPlayer1.settings.autoStart = true;  //自动播放
            axWindowsMediaPlayer1.settings.setMode("shuffle", false);  //顺序播放
            axWindowsMediaPlayer1.settings.enableErrorDialogs = true;
            axWindowsMediaPlayer1.settings.balance = 0;
            axWindowsMediaPlayer1.settings.mute = false;
            axWindowsMediaPlayer1.settings.volume = 100;  //声音设为最大
            
//btnBack.Enabled = false;  //声音不对
            
//btnForward.Enabled = false;  //声音不对
            
//btnBE.Enabled = false;  //无法暂停和开始
        

            if (File.Exists("listbox.txt"))  //如果存在播放列表,那么加载播放列表
            {
                StreamReader reader = new StreamReader("listbox.txt");
                try
                {
                    while (reader.Peek() != -1)
                    {
                        string filepath = reader.ReadLine();
                        string filename = Path.GetFileName(filepath);
                        listBox1.Items.Add(filename);  //listbox用来显示歌曲名
                        axWindowsMediaPlayer1.currentPlaylist.insertItem(axWindowsMediaPlayer1.currentPlaylist.count, axWindowsMediaPlayer1.newMedia(filepath));
                    }
                    listBox1.SelectedIndex = 0;
                }
                catch (Exception)
                {
                    listBox1.SelectedIndex = -1;
                    MessageBox.Show("加载播放列表失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    reader.Close();
                }
            }

        }

        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)  //打开音乐文件,但不加入到播放列表中
        {
            DialogResult dr = openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                axWindowsMediaPlayer1.URL = openFileDialog1.FileName;
            }
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)  //结束程序,但为什么不起作用?而共享此动作的btnExit却有作用?
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();
            axWindowsMediaPlayer1.close();
            Application.Exit();
        }

        private void AddSingleToolStripMenuItem_Click(object sender, EventArgs e)  //添加单首歌曲到播放列表中,"添加"按钮共享此事件
        {
            DialogResult dr = openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                string filepath = openFileDialog1.FileName;
                string filename = Path.GetFileName(filepath);
                listBox1.Items.Add(filename);

                axWindowsMediaPlayer1.currentPlaylist.insertItem(axWindowsMediaPlayer1.currentPlaylist.count,axWindowsMediaPlayer1.newMedia(filepath));
            }
        }

        private void AddMoreToolStripMenuItem_Click(object sender, EventArgs e)  //添加选中的文件夹中的mp3文件到播放列表中
        {
            DialogResult dr = folderBrowserDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                string[] filepath = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                foreach (string s in filepath)
                {
                    if (Path.GetExtension(s) == ".mp3")
                    {
                        string filename = Path.GetFileName(s);
                        listBox1.Items.Add(filename);

                        axWindowsMediaPlayer1.currentPlaylist.insertItem(axWindowsMediaPlayer1.currentPlaylist.count, axWindowsMediaPlayer1.newMedia(s));
                    }
                }
            }
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)  //播放列表中选中的歌曲
        {
            int j = listBox1.SelectedIndex;
            if(listBox1.Items.Count>0)
            {
                axWindowsMediaPlayer1.Ctlcontrols.playItem(axWindowsMediaPlayer1.currentPlaylist.get_Item(j));
            }
        }

      
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)  //退出程序的动作
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();
            axWindowsMediaPlayer1.close();  //关闭播放器
           
            StreamWriter writer = new StreamWriter("listbox.txt",false,Encoding.Unicode);  //保存播放列表
            for (int i = 0; i < axWindowsMediaPlayer1.currentPlaylist.count; i++)
            {
                writer.WriteLine(axWindowsMediaPlayer1.currentPlaylist.get_Item(i).sourceURL);
            }
            writer.Close();

        }

        private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)  //显示播放状态
        {
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                label1.Text = "正在播放 " + axWindowsMediaPlayer1.currentMedia.sourceURL+"   "+axWindowsMediaPlayer1.currentMedia.durationString;
                string s = axWindowsMediaPlayer1.currentMedia.sourceURL;
                for (int i = 0; i < axWindowsMediaPlayer1.currentPlaylist.count; i++)
                {
                    if (listBox1.Items[i].ToString() == Path.GetFileName(s))
                    {
                        listBox1.SelectedIndex = i;
                        break;
                    }
                }
            }
         
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsBuffering)
            {
                label1.Text = "正在缓冲 " + axWindowsMediaPlayer1.currentMedia.sourceURL;
            }
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPaused)
            {
                label1.Text = "暂停播放 " + axWindowsMediaPlayer1.currentMedia.sourceURL;
            }
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsWaiting)
            {
                label1.Text = "正在等待";
            }
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsStopped)
            {
                label1.Text = "播放停止";
            }
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsReady)
            {
                label1.Text = "准备就绪";
            }
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsScanForward)
            {
                label1.Text = "正在快进 " + axWindowsMediaPlayer1.currentMedia.sourceURL;
            }
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsScanReverse)
            {
                label1.Text = "正在快退 " + axWindowsMediaPlayer1.currentMedia.sourceURL;
            }
        }

        private void btnBE_Click(object sender, EventArgs e)  //暂停/开始,不起作用
        {
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                axWindowsMediaPlayer1.Ctlcontrols.pause();
            }
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPaused)
            {
                axWindowsMediaPlayer1.Ctlcontrols.play();
            }
        }

        private void btnStop_Click(object sender, EventArgs e)  //停止播放
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();
        }

        private void btnSlient_Click(object sender, EventArgs e)  //静音
        {
            if (axWindowsMediaPlayer1.settings.mute == false)
            {
                axWindowsMediaPlayer1.settings.mute = true;
            }
            else
            {
                axWindowsMediaPlayer1.settings.mute = false;
            }
        }

        private void btnBack_Click(object sender, EventArgs e)  //快退,声音不对
        {
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                axWindowsMediaPlayer1.Ctlcontrols.fastReverse();
            }
        }

        private void btnForward_Click(object sender, EventArgs e)  //快进,声音不对
        {
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                axWindowsMediaPlayer1.Ctlcontrols.fastForward();
            }
        }

        private void btnPre_Click(object sender, EventArgs e)  //上一曲
        {
            if (listBox1.SelectedIndex != 0)
            {
                axWindowsMediaPlayer1.Ctlcontrols.previous();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)  //下一曲
        {
            if (listBox1.SelectedIndex != listBox1.Items.Count - 1)
            {
                axWindowsMediaPlayer1.Ctlcontrols.next();
            }
        }

        private void btnPlay_Click(object sender, EventArgs e)  //双击播放列表中选中的歌曲
        {
            if(listBox1.Items.Count>0&&listBox1.SelectedIndex>=0)
            {
                axWindowsMediaPlayer1.Ctlcontrols.playItem(axWindowsMediaPlayer1.currentPlaylist.get_Item(listBox1.SelectedIndex));
            }
        }

        private void btRemove_Click(object sender, EventArgs e)  //将选中的歌曲移出播放列表
        {
            int i = listBox1.SelectedIndex;
            if (listBox1.Items.Count > 0)
            {
                listBox1.Items.RemoveAt(i);
                axWindowsMediaPlayer1.currentPlaylist.removeItem(axWindowsMediaPlayer1.currentPlaylist.get_Item(i));
            }

            if (i == listBox1.Items.Count)
            {
                listBox1.SelectedIndex = listBox1.Items.Count-1;
            }
            else
            {
                listBox1.SelectedIndex = i;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)  //同上,并删除本地的音乐文件
        {
            if (MessageBox.Show("确定要删除文件吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                int i = listBox1.SelectedIndex;
                string s = axWindowsMediaPlayer1.currentPlaylist.get_Item(i).sourceURL;
                if (listBox1.Items.Count > 0)
                {
                    listBox1.Items.RemoveAt(i);
                    axWindowsMediaPlayer1.currentPlaylist.removeItem(axWindowsMediaPlayer1.currentPlaylist.get_Item(i));
                }

                if (i == listBox1.Items.Count)
                {
                    listBox1.SelectedIndex = listBox1.Items.Count - 1;
                }
                else
                {
                    listBox1.SelectedIndex = i;
                }

                try
                {
                    File.Delete(s);
                }
                catch (Exception)
                {
                    MessageBox.Show("删除文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }

        private void delAllToolStripMenuItem_Click(object sender, EventArgs e)  //清空播放列表
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();  //先停止播放器
            listBox1.Items.Clear();  //清空listbox
            axWindowsMediaPlayer1.currentPlaylist.clear();  //清空播放列表
        }
    }
}
#20
angelline2011-04-14 08:14
哇 楼主牛呢 偶要狠努力啦
#21
shitoushifa2011-04-14 10:38
在哪里下载啊!
#22
lmn201009292011-04-14 10:41
在VS工作环境的“工具箱”中单击右键,选择“选择项”->选择“COM组件”标签,到列表中选择“Windows Media Player”组件确定。在当前工具箱选项卡中里面找 Windows Media Player 控件拖到form里面,出来的控件就是AxWindowsMediaPlayerCOM组件axWindowsMediaPlayer。
优点:简单方便。

直接用 DirectShow 播放,复杂(略)

下面的COPY而来。。。。。。

AxWindowsMediaPlayer的用法
属性/方法名: 说明:  
[基本属性]    
URL:String; 指定媒体位置,本机或网络地址  
uiMode:String; 播放器界面模式,可为Full, Mini, None, Invisible  
playState:integer; 播放状态,1=停止,2=暂停,3=播放,6=正在缓冲,9=正在连接,10=准备就绪  
enableContextMenu:Boolean; 启用/禁用右键菜单  
fullScreen:boolean; 是否全屏显示  
[controls] wmp.controls //播放器基本控制  
controls.play; 播放  
controls.pause; 暂停  
controls.stop; 停止  
controls.currentPosition:double; 当前进度  
controls.currentPositionString:string; 当前进度,字符串格式。如“00:23”  
controls.fastForward; 快进  
controls.fastReverse; 快退  
controls.next; 下一曲  
controls.previous; 上一曲  
[settings] wmp.settings //播放器基本设置  
settings.volume:integer; 音量,0-100  
settings.autoStart:Boolean; 是否自动播放  
settings.mute:Boolean; 是否静音  
settings.playCount:integer; 播放次数  
[currentMedia] wmp.currentMedia //当前媒体属性  
currentMedia.duration:double; 媒体总长度  
currentMedia.durationString:string; 媒体总长度,字符串格式。如“03:24”  
currentMedia.getItemInfo(const string); 获取当前媒体信息"Title"=媒体标题,"Author"=艺术家,"Copyright"=版权信息,"Description"=媒体内容描述,"Duration"=持续时间(秒),"FileSize"=文件大小,"FileType"=文件类型,"sourceURL"=原始地址  
currentMedia.setItemInfo(const string); 通过属性名设置媒体信息  
currentMedia.name:string; 同 currentMedia.getItemInfo("Title")  
[currentPlaylist] wmp.currentPlaylist //当前播放列表属性  
currentPlaylist.count:integer; 当前播放列表所包含媒体数  
currentPlaylist.Item[integer]; 获取或设置指定项目媒体信息,其子属性同wmp.currentMedia  
AxWindowsMediaPlayer控件的属性收藏
MediaPlayer1.Play          播放   
MediaPlayer1.Stop          停止   
MediaPlayer1.Pause          暂停   
MediaPlayer1.PlayCount        文件播放次数   
MediaPlayer1.AutoRewind       是否循环播放   
MediaPlayer1.Balance         声道   
MediaPlayer1.Volume         音量   
MediaPlayer1.Mute          静音   
MediaPlayer1.EnableContextMenu    是否允许在控件上点击鼠标右键时弹出快捷菜单   
MediaPlayer1.AnimationAtStart    是否在播放前先播放动画   
MediaPlayer1.ShowControls      是否显示控件工具栏   
MediaPlayer1.ShowAudioControls    是否显示声音控制按钮   
MediaPlayer1.ShowDisplay       是否显示数据文件的相关信息   
MediaPlayer1.ShowGotoBar       是否显示Goto栏   
MediaPlayer1.ShowPositionControls  是否显示位置调节按钮   
MediaPlayer1.ShowStatusBar      是否显示状态栏   
MediaPlayer1.ShowTracker       是否显示进度条   
MediaPlayer1.FastForward       快进   
MediaPlayer1.FastReverse       快退   
MediaPlayer1.Rate          快进/快退速率   
MediaPlayer1.AllowChangeDisplaySize 是否允许自由设置播放图象大小   
MediaPlayer1.DisplaySize       设置播放图象大小   
    1-MpDefaultSize         原始大小   
    2-MpHalfSize           原始大小的一半   
    3-MpDoubleSize          原始大小的两倍   
    4-MpFullScreen          全屏   
    5-MpOneSixteenthScreen      屏幕大小的1/16   
    6-MpOneFourthScreen       屏幕大小的1/4   
    7-MpOneHalfScreen        屏幕大小的1/2   
MediaPlayer1.ClickToPlay       是否允许单击播放窗口启动Media Player
#23
shitoushifa2011-04-14 10:42
怎么打不开来 啊!没有用类
#24
yangdehui2011-04-14 14:17
怎么下不下来啊

[ 本帖最后由 yangdehui 于 2011-4-14 14:29 编辑 ]
#25
qq10235692232011-04-14 16:58
回复 22楼 lmn20100929
谢谢分享!
#26
qq10235692232011-04-15 08:54
实现随机播放!
只有本站会员才能查看附件,请 登录
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using namespace MusicPlayer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string[] musicPath = new string[10000];  //用于保存歌曲目录
        int musicCount = 0;

        private void Form1_Load(object sender, EventArgs e)
        {
            myPlyer.BeginInit();  //初始化
            myPlyer.settings.autoStart = true;  //自动播放
            myPlyer.settings.setMode("shuffle", false);  //顺序播放
            myPlyer.settings.enableErrorDialogs = true;
            myPlyer.settings.balance = 0;
            myPlyer.settings.mute = false;
            myPlyer.settings.volume = 100;  //声音设为最大
            btnBack.Enabled = false;  //声音不对
            btnForward.Enabled = false;  //声音不对
            btnBE.Enabled = false;  //无法暂停和开始
            ExitToolStripMenuItem.Enabled = false;  //无法退出
            ReplayToolStripMenuItem.Enabled = false;  //无法单曲循环
        

            if (File.Exists("listbox.txt"))  //如果存在播放列表,那么加载播放列表
            {
                StreamReader reader = new StreamReader("listbox.txt");
                try
                {
                    while (reader.Peek() != -1)
                    {
                        string filepath = reader.ReadLine();
                        if (File.Exists(filepath))
                        {
                            musicPath[musicCount++] = filepath;

                            string filename = Path.GetFileName(filepath);
                            listBox1.Items.Add(filename);  //listbox用来显示歌曲名
                            myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(filepath));
                        }
                    }
                    listBox1.SelectedIndex = 0;
                }
                catch (Exception)
                {
                    listBox1.SelectedIndex = -1;
                    MessageBox.Show("加载播放列表失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    reader.Close();
                }
            }

        }

        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)  //打开音乐文件,但不加入到播放列表中
        {
            DialogResult dr = openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                myPlyer.URL = openFileDialog1.FileName;
            }
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)  //结束程序,但为什么不起作用?而共享此动作的btnExit却有作用?
        {
            myPlyer.Ctlcontrols.stop();
            myPlyer.close();
            Application.Exit();
        }

        private void AddSingleToolStripMenuItem_Click(object sender, EventArgs e)  //添加单首歌曲到播放列表中,"添加"按钮共享此事件
        {
            DialogResult dr = openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                string filepath = openFileDialog1.FileName;
                string filename = Path.GetFileName(filepath);
                listBox1.Items.Add(filename);

                musicPath[musicCount++] = filepath;

                myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count,myPlyer.newMedia(filepath));
            }
        }

        private void AddMoreToolStripMenuItem_Click(object sender, EventArgs e)  //添加选中的文件夹中的mp3文件到播放列表中
        {
            DialogResult dr = folderBrowserDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                string[] filepath = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                foreach (string s in filepath)
                {
                    if (Path.GetExtension(s) == ".mp3")
                    {
                        string filename = Path.GetFileName(s);
                        listBox1.Items.Add(filename);

                        musicPath[musicCount++] = s;

                        myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(s));
                    }
                }
            }
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)  //播放列表中选中的歌曲,随机播放状态下不起作用
        {
            int j = listBox1.SelectedIndex;
            if(listBox1.Items.Count>0)
            {
                myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(j));
            }
        }

      
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)  //退出程序的动作
        {
            myPlyer.Ctlcontrols.stop();
            myPlyer.close();  //关闭播放器

            StreamWriter writer = new StreamWriter("listbox.txt", false, Encoding.Unicode);  //保存播放列表
            for (int i = 0; i <=musicCount-1; i++)
                {
                    if (musicPath[i] != string.Empty)
                    {
                        writer.WriteLine(musicPath[i]);
                    }
                }
             writer.Close();
        }

        private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)  //显示播放状态
        {
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                label1.Text = "正在播放 " + myPlyer.currentMedia.sourceURL+"   "+myPlyer.currentMedia.durationString;
                string s = myPlyer.currentMedia.sourceURL;
                for (int i = 0; i < myPlyer.currentPlaylist.count; i++)
                {
                    if (listBox1.Items[i].ToString() == Path.GetFileName(s))
                    {
                        listBox1.SelectedIndex = i;
                        break;
                    }
                }
            }
         
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsBuffering)
            {
                label1.Text = "正在缓冲 " + myPlyer.currentMedia.sourceURL;
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPaused)
            {
                label1.Text = "暂停播放 " + myPlyer.currentMedia.sourceURL;
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsWaiting)
            {
                label1.Text = "正在等待";
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsStopped)
            {
                label1.Text = "播放停止";
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsReady)
            {
                label1.Text = "准备就绪";
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsScanForward)
            {
                label1.Text = "正在快进 " + myPlyer.currentMedia.sourceURL;
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsScanReverse)
            {
                label1.Text = "正在快退 " + myPlyer.currentMedia.sourceURL;
            }
        }

        private void btnBE_Click(object sender, EventArgs e)  //暂停/开始,不起作用
        {
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                myPlyer.Ctlcontrols.pause();
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPaused)
            {
                myPlyer.Ctlcontrols.play();
            }
        }

        private void btnStop_Click(object sender, EventArgs e)  //停止播放
        {
            myPlyer.Ctlcontrols.stop();
        }

        private void btnSlient_Click(object sender, EventArgs e)  //静音
        {
            if (myPlyer.settings.mute == false)
            {
                myPlyer.settings.mute = true;
            }
            else
            {
                myPlyer.settings.mute = false;
            }
        }

        private void btnBack_Click(object sender, EventArgs e)  //快退,声音不对
        {
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                myPlyer.Ctlcontrols.fastReverse();
            }
        }

        private void btnForward_Click(object sender, EventArgs e)  //快进,声音不对
        {
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                myPlyer.Ctlcontrols.fastForward();
            }
        }

        private void btnPre_Click(object sender, EventArgs e)  //上一曲
        {
            if (listBox1.SelectedIndex != 0)
            {
                myPlyer.Ctlcontrols.previous();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)  //下一曲
        {
            if (listBox1.SelectedIndex != listBox1.Items.Count - 1)
            {
                myPlyer.Ctlcontrols.next();
            }
        }

        private void btnPlay_Click(object sender, EventArgs e)  //双击播放列表中选中的歌曲
        {
            if(listBox1.Items.Count>0&&listBox1.SelectedIndex>=0)
            {
                myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(listBox1.SelectedIndex));
            }
        }

        private void btRemove_Click(object sender, EventArgs e)  //将选中的歌曲移出播放列表
        {
            int i = listBox1.SelectedIndex;

            for (int j = 0; j < musicCount; j++)
            {
                if (Path.GetFileName(musicPath[j]) == listBox1.Items[i].ToString())
                {
                    musicPath[j] ="";
                }
            }

            if (listBox1.Items.Count > 0)
            {
                listBox1.Items.RemoveAt(i);
                myPlyer.currentPlaylist.removeItem(myPlyer.currentPlaylist.get_Item(i));
            }

            if (i == listBox1.Items.Count)
            {
                listBox1.SelectedIndex = listBox1.Items.Count-1;
            }
            else
            {
                listBox1.SelectedIndex = i;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)  //同上,并删除本地的音乐文件
        {
            if (MessageBox.Show("确定要删除文件吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                int i = listBox1.SelectedIndex;
                string s = myPlyer.currentPlaylist.get_Item(i).sourceURL;

                for (int j = 0; j < musicCount; j++)
                {
                    if (Path.GetFileName(musicPath[j]) == listBox1.Items[i].ToString())
                    {
                        musicPath[j] ="";
                    }
                }

                if (listBox1.Items.Count > 0)
                {
                    listBox1.Items.RemoveAt(i);
                    myPlyer.currentPlaylist.removeItem(myPlyer.currentPlaylist.get_Item(i));
                }

                if (i == listBox1.Items.Count)
                {
                    listBox1.SelectedIndex = listBox1.Items.Count - 1;
                }
                else
                {
                    listBox1.SelectedIndex = i;
                }

                try
                {
                    File.Delete(s);
                }
                catch (Exception)
                {
                    MessageBox.Show("删除文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }

        private void delAllToolStripMenuItem_Click(object sender, EventArgs e)  //清空播放列表
        {
            myPlyer.Ctlcontrols.stop();  //先停止播放器
            listBox1.Items.Clear();  //清空listbox
            myPlyer.currentPlaylist.clear();  //清空播放列表

            for (int j = 0; j < musicCount; j++)
            {
                    musicPath[j] = "";
            }
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.Equals(e.KeyChar, ' '))
            {
                if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
                {
                    myPlyer.Ctlcontrols.pause();
                }
            }
        }

        private void SToolStripMenuItem_Click(object sender, EventArgs e) //顺序播放,默认
        {
            myPlyer.settings.playCount = 1;
            myPlyer.currentPlaylist.clear();

            for(int j=0;j<musicCount;j++)
            {
                if(musicPath[j]!=string.Empty)
                {
                    myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count,myPlyer.newMedia(musicPath[j]));
                }
            }
           
            myPlyer.settings.setMode("shuffle", false);
            myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(0));
        }

        private void ReplayToolStripMenuItem_Click(object sender, EventArgs e)  //单曲循环
        {
            myPlyer.settings.playCount = 1000;
        }

        private void RandomToolStripMenuItem_Click(object sender, EventArgs e)  //随机播放,重新建立当前播放列表
        {
            myPlyer.settings.playCount = 1;
            myPlyer.Ctlcontrols.stop();

            myPlyer.currentPlaylist.clear();

            Random rd = new Random(DateTime.Now.Millisecond);

            for (int i = 0; i < 1000; i++)
            {
                int j = rd.Next(0,musicCount - 1);

                if (musicPath[j] != string.Empty)
                {
                    myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(musicPath[j]));
                }
            }

            myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(0));

        }
    }
}
#27
qq10235692232011-04-15 13:36
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using namespace MusicPlayer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string[] musicPath = new string[10000];  //用于保存歌曲目录
        int musicCount = 0;

        private void Form1_Load(object sender, EventArgs e)
        {
            myPlyer.BeginInit();  //初始化
            myPlyer.settings.autoStart = true;  //自动播放
            myPlyer.settings.setMode("shuffle", false);  //顺序播放
            myPlyer.settings.enableErrorDialogs = true;
            myPlyer.settings.balance = 0;
            myPlyer.settings.mute = false;
            myPlyer.settings.volume = 100;  //声音设为最大
            btnBack.Enabled = false;  //声音不对
            btnForward.Enabled = false;  //声音不对
            btnBE.Enabled = false;  //无法暂停和开始
            ExitToolStripMenuItem.Enabled = false;  //无法退出
            ReplayToolStripMenuItem.Enabled = false;  //无法单曲循环
        

            if (File.Exists("listbox.txt"))  //如果存在播放列表,那么加载播放列表
            {
                StreamReader reader = new StreamReader("listbox.txt");
                try
                {
                    while (reader.Peek() != -1)
                    {
                        string filepath = reader.ReadLine();
                        if (File.Exists(filepath))
                        {
                            musicPath[musicCount++] = filepath;

                            string filename = Path.GetFileName(filepath);
                            listBox1.Items.Add(filename);  //listbox用来显示歌曲名
                            myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(filepath));
                        }
                    }
                    listBox1.SelectedIndex = 0;
                }
                catch (Exception)
                {
                    listBox1.SelectedIndex = -1;
                    MessageBox.Show("加载播放列表失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    reader.Close();
                }
            }

        }

        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)  //打开音乐文件,但不加入到播放列表中
        {
            DialogResult dr = openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                myPlyer.URL = openFileDialog1.FileName;
            }
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)  //结束程序,但为什么不起作用?而共享此动作的btnExit却有作用?
        {
            myPlyer.Ctlcontrols.stop();
            myPlyer.close();
            Application.Exit();
        }

        private void AddSingleToolStripMenuItem_Click(object sender, EventArgs e)  //添加单首歌曲到播放列表中,"添加"按钮共享此事件
        {
            DialogResult dr = openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                string filepath = openFileDialog1.FileName;
                string filename = Path.GetFileName(filepath);
                listBox1.Items.Add(filename);

                musicPath[musicCount++] = filepath;

                myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count,myPlyer.newMedia(filepath));
            }
        }

        private void AddMoreToolStripMenuItem_Click(object sender, EventArgs e)  //添加选中的文件夹中的mp3文件到播放列表中
        {
            DialogResult dr = folderBrowserDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                string[] filepath = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                foreach (string s in filepath)
                {
                    if (Path.GetExtension(s) == ".mp3")
                    {
                        string filename = Path.GetFileName(s);
                        listBox1.Items.Add(filename);

                        musicPath[musicCount++] = s;

                        myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(s));
                    }
                }
            }
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)  //播放列表中选中的歌曲,随机播放状态下不起作用
        {
            int j = listBox1.SelectedIndex;
            if(listBox1.Items.Count>0)
            {
                myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(j));
            }
        }

      
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)  //退出程序的动作
        {
            myPlyer.Ctlcontrols.stop();
            myPlyer.close();  //关闭播放器

            StreamWriter writer = new StreamWriter("listbox.txt", false, Encoding.Unicode);  //保存播放列表
            for (int i = 0; i <=musicCount-1; i++)
                {
                    if (musicPath[i] != string.Empty)
                    {
                        writer.WriteLine(musicPath[i]);
                    }
                }
             writer.Close();
        }

        private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)  //显示播放状态
        {
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                label1.Text = "正在播放 " + myPlyer.currentMedia.sourceURL+"   "+myPlyer.currentMedia.durationString;
                string s = myPlyer.currentMedia.sourceURL;
                for (int i = 0; i < myPlyer.currentPlaylist.count; i++)
                {
                    if (listBox1.Items[i].ToString() == Path.GetFileName(s))
                    {
                        listBox1.SelectedIndex = i;
                        break;
                    }
                }
            }
         
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsBuffering)
            {
                label1.Text = "正在缓冲 " + myPlyer.currentMedia.sourceURL;
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPaused)
            {
                label1.Text = "暂停播放 " + myPlyer.currentMedia.sourceURL;
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsWaiting)
            {
                label1.Text = "正在等待";
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsStopped)
            {
                label1.Text = "播放停止";
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsReady)
            {
                label1.Text = "准备就绪";
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsScanForward)
            {
                label1.Text = "正在快进 " + myPlyer.currentMedia.sourceURL;
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsScanReverse)
            {
                label1.Text = "正在快退 " + myPlyer.currentMedia.sourceURL;
            }
        }

        private void btnBE_Click(object sender, EventArgs e)  //暂停/开始,不起作用
        {
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                myPlyer.Ctlcontrols.pause();
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPaused)
            {
                myPlyer.Ctlcontrols.play();
            }
        }

        private void btnStop_Click(object sender, EventArgs e)  //停止播放
        {
            myPlyer.Ctlcontrols.stop();
        }

        private void btnSlient_Click(object sender, EventArgs e)  //静音
        {
            if (myPlyer.settings.mute == false)
            {
                myPlyer.settings.mute = true;
            }
            else
            {
                myPlyer.settings.mute = false;
            }
        }

        private void btnBack_Click(object sender, EventArgs e)  //快退,声音不对
        {
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                myPlyer.Ctlcontrols.fastReverse();
            }
        }

        private void btnForward_Click(object sender, EventArgs e)  //快进,声音不对
        {
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                myPlyer.Ctlcontrols.fastForward();
            }
        }

        private void btnPre_Click(object sender, EventArgs e)  //上一曲
        {
            if (listBox1.SelectedIndex != 0)
            {
                myPlyer.Ctlcontrols.previous();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)  //下一曲
        {
            if (listBox1.SelectedIndex != listBox1.Items.Count - 1)
            {
                myPlyer.Ctlcontrols.next();
            }
        }

        private void btnPlay_Click(object sender, EventArgs e)  //双击播放列表中选中的歌曲
        {
            if(listBox1.Items.Count>0&&listBox1.SelectedIndex>=0)
            {
                myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(listBox1.SelectedIndex));
            }
        }

        private void btRemove_Click(object sender, EventArgs e)  //将选中的歌曲移出播放列表
        {
            int i = listBox1.SelectedIndex;

            for (int j = 0; j < musicCount; j++)
            {
                if (Path.GetFileName(musicPath[j]) == listBox1.Items[i].ToString())
                {
                    musicPath[j] ="";
                }
            }

            if (listBox1.Items.Count > 0)
            {
                for (int j = 0; j < myPlyer.currentPlaylist.count; j++)
                {
                    if (listBox1.Items[i].ToString() == Path.GetFileName(myPlyer.currentPlaylist.get_Item(j).sourceURL))
                        myPlyer.currentPlaylist.removeItem(myPlyer.currentPlaylist.get_Item(j));
                }
                listBox1.Items.RemoveAt(i);
            }

            if (i == listBox1.Items.Count)
            {
                listBox1.SelectedIndex = listBox1.Items.Count-1;
            }
            else
            {
                listBox1.SelectedIndex = i;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)  //同上,并删除本地的音乐文件
        {
            if (MessageBox.Show("确定要删除文件吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                int i = listBox1.SelectedIndex;
                string s = myPlyer.currentPlaylist.get_Item(i).sourceURL;

                for (int j = 0; j < musicCount; j++)
                {
                    if (Path.GetFileName(musicPath[j]) == listBox1.Items[i].ToString())
                    {
                        musicPath[j] ="";
                    }
                }

                if (listBox1.Items.Count > 0)
                {
                    for (int j = 0; j < myPlyer.currentPlaylist.count; j++)
                    {
                        if (listBox1.Items[i].ToString() == Path.GetFileName(myPlyer.currentPlaylist.get_Item(j).sourceURL))
                            myPlyer.currentPlaylist.removeItem(myPlyer.currentPlaylist.get_Item(j));
                    }
                    listBox1.Items.RemoveAt(i);
                }

                if (i == listBox1.Items.Count)
                {
                    listBox1.SelectedIndex = listBox1.Items.Count - 1;
                }
                else
                {
                    listBox1.SelectedIndex = i;
                }

                try
                {
                    File.Delete(s);
                }
                catch (Exception)
                {
                    MessageBox.Show("删除文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }

        private void delAllToolStripMenuItem_Click(object sender, EventArgs e)  //清空播放列表
        {
            myPlyer.Ctlcontrols.stop();  //先停止播放器
            listBox1.Items.Clear();  //清空listbox
            myPlyer.currentPlaylist.clear();  //清空播放列表

            for (int j = 0; j < musicCount; j++)
            {
                    musicPath[j] = "";
            }
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.Equals(e.KeyChar, ' '))
            {
                if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
                {
                    myPlyer.Ctlcontrols.pause();
                }
            }
        }

        private void SToolStripMenuItem_Click(object sender, EventArgs e) //顺序播放,默认
        {
            myPlyer.settings.playCount = 1;
            myPlyer.currentPlaylist.clear();

            for(int j=0;j<musicCount;j++)
            {
                if(musicPath[j]!=string.Empty)
                {
                    myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count,myPlyer.newMedia(musicPath[j]));
                }
            }
           
            myPlyer.settings.setMode("shuffle", false);
            myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(0));
        }

        private void ReplayToolStripMenuItem_Click(object sender, EventArgs e)  //单曲循环
        {
            myPlyer.settings.playCount = 1000;
        }

        private void RandomToolStripMenuItem_Click(object sender, EventArgs e)  //随机播放,重新建立当前播放列表
        {
            myPlyer.settings.playCount = 1;
            myPlyer.Ctlcontrols.stop();

            myPlyer.currentPlaylist.clear();

            Random rd = new Random(DateTime.Now.Millisecond);

            for (int i = 0; i < 1000; i++)
            {
                int j = rd.Next(0,musicCount - 1);

                if (musicPath[j] != string.Empty)
                {
                    myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(musicPath[j]));
                }
            }

            myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(0));

        }
    }
}
修复了一个BUG
#28
hu6696992011-04-19 15:10
顶起。。
#29
lys1209553502011-04-21 08:26
山东科技大学 飘过
#30
鬼王2011-04-25 17:20
支持原创。
#31
fbzsn2011-04-26 12:24
怎么做呢?怎么用visual studio打开呢? 请指教!
#32
qq10235692232011-04-26 12:27
只有本站会员才能查看附件,请 登录

解压后,双击MusicPlayer.sln就可以了!我是用VS2005做的,版本支持方面可能有问题!
#33
松小子2011-05-13 14:49
你好,你的播放器我下来看了,很好,可不可以交流下,我写想自己写个播放器,
加我QQ:297321054
期待你哦。版主。。
#34
Snow_Pu2011-05-13 15:49
好牛~我又有了动力~
#35
yinniannian2011-05-13 21:27
大虾,你会用speech  sdk吗?求赐教449795473
#36
yinniannian2011-05-13 21:43
大虾,您懂speech sdk吗?求赐教449795473
#37
qq10235692232011-05-15 10:09
不懂!
#38
zhangfengtab2011-05-15 15:26
真的很不错  
决定试一下
#39
天行圣君2011-05-15 16:08
支持。。。
#40
qq10235692232011-05-15 17:24
看到那么多朋友关注,倍感高兴!在此贴出我完善的最终的代码!
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using namespace MusicPlayer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string[] musicPath = new string[10000];  //用于保存歌曲目录
        int musicCount = 0;

        private void Form1_Load(object sender, EventArgs e)
        {
            myPlyer.BeginInit();  //初始化
            myPlyer.settings.autoStart = true;  //自动播放
            myPlyer.settings.setMode("shuffle", false);  //顺序播放
            myPlyer.settings.enableErrorDialogs = true;
            myPlyer.settings.balance = 0;
            myPlyer.settings.mute = false;
            myPlyer.settings.volume = 100;  //声音设为最大
            btnBack.Enabled = false;  //声音不对
            btnForward.Enabled = false;  //声音不对
            btnBE.Enabled = false;  //无法暂停和开始
            ReplayToolStripMenuItem.Enabled = false;  //无法单曲循环
        

            if (File.Exists("listbox.txt"))  //如果存在播放列表,那么加载播放列表
            {
                StreamReader reader = new StreamReader("listbox.txt");
                try
                {
                    while (reader.Peek() != -1)
                    {
                        string filepath = reader.ReadLine();
                        if (File.Exists(filepath))
                        {
                            musicPath[musicCount++] = filepath;

                            string filename = Path.GetFileName(filepath);
                            listBox1.Items.Add(filename);  //listbox用来显示歌曲名
                            myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(filepath));
                        }
                    }
                    listBox1.SelectedIndex = 0;
                }
                catch (Exception)
                {
                    listBox1.SelectedIndex = -1;
                    MessageBox.Show("加载播放列表失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    reader.Close();
                }
            }

        }

        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)  //打开音乐文件,但不加入到播放列表中
        {
            DialogResult dr = openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                myPlyer.URL = openFileDialog1.FileName;
            }
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)  //结束程序,但为什么不起作用?而共享此动作的btnExit却有作用?
        {
            myPlyer.Ctlcontrols.stop();
            myPlyer.close();
            Application.Exit();
        }

        private void AddSingleToolStripMenuItem_Click(object sender, EventArgs e)  //添加单首歌曲到播放列表中,"添加"按钮共享此事件
        {
            DialogResult dr = openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                string filepath = openFileDialog1.FileName;
                string filename = Path.GetFileName(filepath);
                listBox1.Items.Add(filename);

                musicPath[musicCount++] = filepath;

                myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count,myPlyer.newMedia(filepath));
            }
        }

        private void AddMoreToolStripMenuItem_Click(object sender, EventArgs e)  //添加选中的文件夹中的mp3文件到播放列表中
        {
            DialogResult dr = folderBrowserDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                string[] filepath = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                foreach (string s in filepath)
                {
                    if (Path.GetExtension(s) == ".mp3")
                    {
                        string filename = Path.GetFileName(s);
                        listBox1.Items.Add(filename);

                        musicPath[musicCount++] = s;

                        myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(s));
                    }
                }
            }
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)  //播放列表中选中的歌曲,随机播放状态下不起作用
        {
            int j = listBox1.SelectedIndex;
            if(listBox1.Items.Count>0)
            {
                myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(j));
            }
        }

      
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)  //退出程序的动作
        {
            myPlyer.Ctlcontrols.stop();
            myPlyer.close();  //关闭播放器

            StreamWriter writer = new StreamWriter("listbox.txt", false, Encoding.Unicode);  //保存播放列表
            for (int i = 0; i <=musicCount-1; i++)
                {
                    if (musicPath[i] != string.Empty)
                    {
                        writer.WriteLine(musicPath[i]);
                    }
                }
             writer.Close();
        }

        private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)  //显示播放状态
        {
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                label1.Text = "正在播放 " + myPlyer.currentMedia.sourceURL+"   "+myPlyer.currentMedia.durationString;
                string s = myPlyer.currentMedia.sourceURL;
                for (int i = 0; i < myPlyer.currentPlaylist.count; i++)
                {
                    if (listBox1.Items[i].ToString() == Path.GetFileName(s))
                    {
                        listBox1.SelectedIndex = i;
                        break;
                    }
                }
            }
         
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsBuffering)
            {
                label1.Text = "正在缓冲 " + myPlyer.currentMedia.sourceURL;
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPaused)
            {
                label1.Text = "暂停播放 " + myPlyer.currentMedia.sourceURL;
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsWaiting)
            {
                label1.Text = "正在等待";
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsStopped)
            {
                label1.Text = "播放停止";
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsReady)
            {
                label1.Text = "准备就绪";
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsScanForward)
            {
                label1.Text = "正在快进 " + myPlyer.currentMedia.sourceURL;
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsScanReverse)
            {
                label1.Text = "正在快退 " + myPlyer.currentMedia.sourceURL;
            }
        }

        private void btnBE_Click(object sender, EventArgs e)  //暂停/开始,不起作用
        {
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                myPlyer.Ctlcontrols.pause();
            }
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPaused)
            {
                myPlyer.Ctlcontrols.play();
            }
        }

        private void btnStop_Click(object sender, EventArgs e)  //停止播放
        {
            myPlyer.Ctlcontrols.stop();
        }

        private void btnSlient_Click(object sender, EventArgs e)  //静音
        {
            if (myPlyer.settings.mute == false)
            {
                myPlyer.settings.mute = true;
            }
            else
            {
                myPlyer.settings.mute = false;
            }
        }

        private void btnBack_Click(object sender, EventArgs e)  //快退,声音不对
        {
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                myPlyer.Ctlcontrols.fastReverse();
            }
        }

        private void btnForward_Click(object sender, EventArgs e)  //快进,声音不对
        {
            if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                myPlyer.Ctlcontrols.fastForward();
            }
        }

        private void btnPre_Click(object sender, EventArgs e)  //上一曲
        {
            if (listBox1.SelectedIndex != 0)
            {
                myPlyer.Ctlcontrols.previous();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)  //下一曲
        {
            if (listBox1.SelectedIndex != listBox1.Items.Count - 1)
            {
                myPlyer.Ctlcontrols.next();
            }
        }

        private void btnPlay_Click(object sender, EventArgs e)  //双击播放列表中选中的歌曲
        {
            if(listBox1.Items.Count>0&&listBox1.SelectedIndex>=0)
            {
                myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(listBox1.SelectedIndex));
            }
        }

        private void btRemove_Click(object sender, EventArgs e)  //将选中的歌曲移出播放列表
        {
            int i = listBox1.SelectedIndex;

            for (int j = 0; j < musicCount; j++)
            {
                if (Path.GetFileName(musicPath[j]) == listBox1.Items[i].ToString())
                {
                    musicPath[j] ="";
                }
            }

            if (listBox1.Items.Count > 0)
            {
                for (int j = 0; j < myPlyer.currentPlaylist.count; j++)
                {
                    if (listBox1.Items[i].ToString() == Path.GetFileName(myPlyer.currentPlaylist.get_Item(j).sourceURL))
                        myPlyer.currentPlaylist.removeItem(myPlyer.currentPlaylist.get_Item(j));
                }
                listBox1.Items.RemoveAt(i);
            }

            if (i == listBox1.Items.Count)
            {
                listBox1.SelectedIndex = listBox1.Items.Count-1;
            }
            else
            {
                listBox1.SelectedIndex = i;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)  //同上,并删除本地的音乐文件
        {
            if (MessageBox.Show("确定要删除文件吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                int i = listBox1.SelectedIndex;
                string s = myPlyer.currentPlaylist.get_Item(i).sourceURL;

                for (int j = 0; j < musicCount; j++)
                {
                    if (Path.GetFileName(musicPath[j]) == listBox1.Items[i].ToString())
                    {
                        musicPath[j] ="";
                    }
                }

                if (listBox1.Items.Count > 0)
                {
                    for (int j = 0; j < myPlyer.currentPlaylist.count; j++)
                    {
                        if (listBox1.Items[i].ToString() == Path.GetFileName(myPlyer.currentPlaylist.get_Item(j).sourceURL))
                            myPlyer.currentPlaylist.removeItem(myPlyer.currentPlaylist.get_Item(j));
                    }
                    listBox1.Items.RemoveAt(i);
                }

                if (i == listBox1.Items.Count)
                {
                    listBox1.SelectedIndex = listBox1.Items.Count - 1;
                }
                else
                {
                    listBox1.SelectedIndex = i;
                }

                try
                {
                    File.Delete(s);
                }
                catch (Exception)
                {
                    MessageBox.Show("删除文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }

        private void delAllToolStripMenuItem_Click(object sender, EventArgs e)  //清空播放列表
        {
            myPlyer.Ctlcontrols.stop();  //先停止播放器
            listBox1.Items.Clear();  //清空listbox
            myPlyer.currentPlaylist.clear();  //清空播放列表

            for (int j = 0; j < musicCount; j++)
            {
                    musicPath[j] = "";
            }
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.Equals(e.KeyChar, ' '))
            {
                if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying)
                {
                    myPlyer.Ctlcontrols.pause();
                }
            }
        }

        private void SToolStripMenuItem_Click(object sender, EventArgs e) //顺序播放,默认
        {
            myPlyer.settings.playCount = 1;
            myPlyer.currentPlaylist.clear();

            for(int j=0;j<musicCount;j++)
            {
                if(musicPath[j]!=string.Empty)
                {
                    myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count,myPlyer.newMedia(musicPath[j]));
                }
            }
           
            myPlyer.settings.setMode("shuffle", false);
            myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(0));
        }

        private void ReplayToolStripMenuItem_Click(object sender, EventArgs e)  //单曲循环
        {
            myPlyer.settings.playCount = 1000;
        }

        private void RandomToolStripMenuItem_Click(object sender, EventArgs e)  //随机播放,重新建立当前播放列表
        {
            myPlyer.settings.playCount = 1;
            myPlyer.Ctlcontrols.stop();

            myPlyer.currentPlaylist.clear();

            Random rd = new Random(DateTime.Now.Millisecond);

            for (int i = 0; i < 1000; i++)
            {
                int j = rd.Next(0,musicCount - 1);

                if (musicPath[j] != string.Empty)
                {
                    myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(musicPath[j]));
                }
            }

            myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(0));

        }

        private void ExitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            myPlyer.Ctlcontrols.stop();
            myPlyer.close();
            Application.Exit();
        }
    }
}

#41
hsy20082112011-05-16 09:18
那个开始暂停 你可以在外面设置个全局bool变量
//
bool flag = false;//判断是播放还是暂停
//在开始/暂停事件里
 if (!flag)
            {
                axWindowsMediaPlayer1.Ctlcontrols.play();
                flag = true;
            }
            else
            {
                axWindowsMediaPlayer1.Ctlcontrols.pause();
                flag = false;
            }
这就解决了
#42
Snow_Pu2011-05-16 10:30
我添加media player的引用之后  怎么在设计中加入media player的界面 网上我没有查到 求赐教
只有本站会员才能查看附件,请 登录
#43
qq10235692232011-05-16 10:39
在工具箱中对着任意一个控件点右键,点击“选择项",在弹出的对话框中切换到COM选项卡,找到Windows Media Player,打勾确定,工具箱中出现WMP控件,就可以用了!
#44
Snow_Pu2011-05-16 11:17
回复 43楼 qq1023569223
学会了 谢谢
#45
zhwTesting2011-05-17 21:27
谢谢分享
#46
moridiansha2011-05-18 06:52
我这的开始和暂停按钮没用,是不是我用2010的关系?
#47
qq10235692232011-05-18 19:21
我的那个也是没用,不知道什么原因,哎!
#48
赵博凡2012-03-07 20:40
在哪下载呢?
#49
心若定水2012-03-10 18:53
佩服佩服
#50
白色恋习曲2012-06-20 11:54
不错呀
#51
konghui32012-08-03 15:32
非常好。
12