| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5267 人关注过本帖, 1 人收藏
标题:基于axWindowsMediaPlayer的C# 简易音乐播放器示例
只看楼主 加入收藏
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
结帖率:100%
收藏(1)
 问题点数:0 回复次数:0 
基于axWindowsMediaPlayer的C# 简易音乐播放器示例
基于axWindowsMediaPlayer的简易音乐播放器示例,供大家参考。附件请用VS2015打开。
MusicPlayer.zip (635.1 KB)

程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using namespace MusicPlayer
{
    public partial class Form1 : Form
    {
        private WMPLib.IWMPPlaylist initPlaylist;
        ToolTip tipSource, tipAuthor;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.btnAdd.Text = "Add...";
            this.btnDel.Text = "Delete";
            this.btnPlay.Text = "Play";
            this.btnExit.Text = "Exit";
            this.Text = "AxWindowMediaPlayer";
            this.groupBox1.Text = "Currentmedia information";
            this.setLabels(new string[] { "Nothing"});

            this.initPlaylist = this.axWMPlayer.playlistCollection.newPlaylist("oriPlaylist");

            this.tipAuthor = new ToolTip();
            this.tipSource = new ToolTip();

            this.btnDel.Enabled = false;
        }

        private void setLabels(string[] mediaInfo)
        {
            if (mediaInfo.Length != 6)
            {
                this.labelAuthor.Text = "Author: ";
                this.labelDuration.Text = "Duration: ";
                this.labelSize.Text = "Size: ";
                this.labelSource.Text = "Source: ";
                this.labelTitle.Text = "Title: ";
                this.labelType.Text = "Type: ";
            }
            else
            {
                this.labelAuthor.Text = "Author: " + mediaInfo[0];
                this.labelDuration.Text = "Duration: " + mediaInfo[1];
                this.labelSize.Text = "Size: " + mediaInfo[2];
                this.labelSource.Text = "Source: " + mediaInfo[3];
                this.labelTitle.Text = "Title: " + mediaInfo[4];
                this.labelType.Text = "Type: " + mediaInfo[5];
            }           
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.CheckFileExists = true;
            dlg.Filter = "MP3(*.mp3)|*.mp3|WMA(*.wma)|*.wma|WMV(*.wmv)|*.wmv|All files(*.*)|*.*";
            dlg.Multiselect = true;

            if(dlg.ShowDialog()==DialogResult.OK)
            {
                string[] files = dlg.FileNames;

                foreach(string music in files)
                {
                    this.listBox_Media.Items.Add(Path.GetFileName(music));
                    this.initPlaylist.appendItem(this.axWMPlayer.newMedia(music));
                }
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if(this.listBox_Media.SelectedIndex!=-1)
            {
                //WMPLib.IWMPMedia media = this.axWMPlayer.newMedia(this.listBox_Media.Items[this.listBox_Media.SelectedIndex].ToString());
                //this.axWMPlayer.currentPlaylist.removeItem(this.axWMPlayer.newMedia(this.listBox_Media.Items[this.listBox_Media.SelectedIndex].ToString()));
                this.initPlaylist.removeItem(this.initPlaylist.Item[this.listBox_Media.SelectedIndex]);
                this.listBox_Media.Items.RemoveAt(this.listBox_Media.SelectedIndex);

                if(this.listBox_Media.Items.Count==0)
                {
                    this.axWMPlayer.Ctlcontrols.stop();
                }
            }
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            if (this.listBox_Media.Items.Count==0)
            {
                return;
            }

            this.axWMPlayer.currentPlaylist = this.initPlaylist;

            if (this.listBox_Media.SelectedIndex==-1)
            {                             
                this.axWMPlayer.Ctlcontrols.play();
            }
            else
            {
                this.axWMPlayer.Ctlcontrols.playItem(this.axWMPlayer.currentPlaylist.Item[this.listBox_Media.SelectedIndex]);
            }
        }
            

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.axWMPlayer.Ctlcontrols.stop();
            this.axWMPlayer.close();
            this.axWMPlayer.Dispose();
            Application.Exit();
        }

        //Play the double-clicked media,and play the next
        private void listBox_Media_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if(this.listBox_Media.SelectedIndex!=-1)
            {
                Rectangle selectedItemRectangle = this.listBox_Media.GetItemRectangle(this.listBox_Media.SelectedIndex);

                if(selectedItemRectangle.Contains(e.Location))
                {
                    /*this.axWMPlayer.Ctlcontrols.stop();
                    //Make a new playlsit without medias before the selected one
                    string playlistName = DateTime.Now.Millisecond.ToString();
                    WMPLib.IWMPPlaylist newList = this.axWMPlayer.playlistCollection.newPlaylist(playlistName);
                    for(int i=this.listBox_Media.SelectedIndex;i<this.listBox_Media.Items.Count;i++)
                    {
                        newList.appendItem(this.initPlaylist.Item[i]);
                    }

                    this.axWMPlayer.currentPlaylist = newList;
                    this.axWMPlayer.Ctlcontrols.play();*/
                    this.axWMPlayer.currentPlaylist = this.initPlaylist;
                    this.axWMPlayer.Ctlcontrols.playItem(this.axWMPlayer.currentPlaylist.Item[this.listBox_Media.SelectedIndex]);
                }
            }
        }

        private void axWMPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            if(this.axWMPlayer.playState==WMPLib.WMPPlayState.wmppsStopped)
            {
                this.setLabels(new string[] { "Nothing"});

                this.tipAuthor.SetToolTip(this.labelAuthor, string.Empty);
                this.tipSource.SetToolTip(this.labelSource, string.Empty);

                this.Text = "AxWindowMediaPlayer";
            }
            if(this.axWMPlayer.playState==WMPLib.WMPPlayState.wmppsPlaying)
            {
                WMPLib.IWMPMedia currentMedia = this.axWMPlayer.currentMedia;
                string[] info = new string[6];

                info[0] = currentMedia.getItemInfo("Author");
                info[1] = currentMedia.getItemInfo("Duration");
                info[2] = currentMedia.getItemInfo("FileSize");
                info[3] = currentMedia.getItemInfo("sourceURL");
                info[4] = currentMedia.getItemInfo("Title");
                info[5] = currentMedia.getItemInfo("FileType");

                info[1] = this.getMediaTime(info[1]);
                info[2] = string.Format("{0:F2}M",Convert.ToDouble(info[2]) / 1024.0 / 1024);
                this.setLabels(info);

                this.tipAuthor.SetToolTip(this.labelAuthor,info[0]);
                this.tipSource.SetToolTip(this.labelSource,info[3]);

                this.Text = "AxWindowMediaPlayer ----- " + info[4];
            }
        }

        private void listBox_Media_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.listBox_Media.SelectedIndex!=-1)
            {
                this.btnDel.Enabled = true;
            }
            else
            {
                this.btnDel.Enabled = false;
            }
        }

        private string getMediaTime(string timeInSeconds)
        {
            double timedouble = Convert.ToDouble(timeInSeconds);
            int time = Convert.ToInt32(timedouble);

            if(time<60)
            {
                return time.ToString() + " s";
            }
            else
            {
                int mi = time / 60;
                int se = time - mi * 60;
                return mi.ToString() + " min " + se.ToString() + " s";
            }

            //return string.Empty;
        }
    }
}


[此贴子已经被作者于2016-1-29 13:13编辑过]

搜索更多相关主题的帖子: 音乐播放器 color 
2016-01-29 12:36
快速回复:基于axWindowsMediaPlayer的C# 简易音乐播放器示例
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027697 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved