在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#多媒体编程 → mp3音乐播放器 示例源码(AxWindowsMediaPlayer)

mp3音乐播放器 示例源码(AxWindowsMediaPlayer)

C#多媒体编程

下载此实例
  • 开发语言:C#
  • 实例大小:12.70M
  • 下载次数:27
  • 浏览次数:581
  • 发布时间:2018-04-24
  • 实例类别:C#多媒体编程
  • 发 布 人:幸福感认识的他
  • 文件格式:.zip
  • 所需积分:5
 相关标签: 音乐 播放

实例介绍

【实例简介】可播放mp3 声音,使用的AxWindowsMediaPlayer控件,附加上数据库即可测试(无需配置连接串)

【实例截图】

from clipboard


需要启用 Windows Media Player 功能,否则会提示【System.Runtime.InteropServices.COMException:“没有注册类】

操作步骤:控制面板>>程序>>启用或者关闭windows 功能>>选中 媒体功能>>Windows Media Player,点击确定即可

from clipboard

【核心代码】


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.Runtime.InteropServices;
using System.Data.SqlClient;

namespace MP3Player
{
    public partial class frmYinYue : Form
    {
        public frmYinYue()
        {
            InitializeComponent();
        }
        //***********************
        private Point mouseOffset;//记录鼠标坐标
        private bool isMouseDown = false;//是否按下鼠标
        bool flag = false;//判断是播放还是打开选择窗口
        static bool MM = true;//记录是否静音
        static bool XX = true;//记录是否暂停
        //***********************

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        #region 移动无边框窗体
        private void pictureBox3_MouseDown(object sender, MouseEventArgs e)
        {
            int xOffset;
            int yOffset;
            if (e.Button == MouseButtons.Left)
            {
                xOffset = -e.X;
                yOffset = -e.Y;
                mouseOffset = new Point(xOffset, yOffset);
                isMouseDown = true;
            }
        }

        private void pictureBox3_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouseOffset.X, mouseOffset.Y);
                Location = mousePos;
            }
        }

        private void pictureBox3_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = false;
            }
        }
        #endregion



        private void pictureBox4_Click(object sender, EventArgs e)//打开播放
        {

            Music();
            axWindowsMediaPlayer1.URL = Tools.Address;
            m = 1;
            lblSongTitle.Text = " 歌曲名称:"   axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");

        }

        public void Music()
        {
            string sql = "select * from Yy";
            if (Tools.CiShu > 0)
            {
                sql  = string.Format(" where Id='{0}'", Tools.CiShu);
            }
            SqlConnection conn = new SqlConnection(DBHelper.ConStr);
            try
            {
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataReader reader = comm.ExecuteReader();
                while (reader.Read())
                {
                    //Tools.Address = reader["Address"].ToString();
                    Tools.Address = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Music/命运交响曲-张智霖.mp3");
                }
                reader.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();

            }
        }
        private void pictureBox5_Click(object sender, EventArgs e)//暂停
        {
            axWindowsMediaPlayer1.Ctlcontrols.pause();
            flag = true;
        }

        private void pictureBox6_Click(object sender, EventArgs e)//停止
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();
            flag = false;
        }



        private void pictureBox7_Click(object sender, EventArgs e)//静音
        {
            if (MM)
            {
                pictureBox7.Image = (Image)Properties.Resources.静音;
                axWindowsMediaPlayer1.settings.mute = true;
                MM = false;
            }
            else
            {
                pictureBox7.Image = (Image)Properties.Resources.增大音量;
                axWindowsMediaPlayer1.settings.mute = false;
                MM = true;
            }
        }
        #region 按钮
        private void pictureBox4_MouseEnter(object sender, EventArgs e)
        {
            pictureBox4.Image = (Image)Properties.Resources.播放;
        }

        private void pictureBox4_MouseLeave(object sender, EventArgs e)
        {
            pictureBox4.Image = (Image)Properties.Resources.播放;
        }

        private void pictureBox5_MouseEnter(object sender, EventArgs e)
        {
            pictureBox5.Image = (Image)Properties.Resources.暂停;
        }

        private void pictureBox5_MouseLeave(object sender, EventArgs e)
        {
            pictureBox5.Image = (Image)Properties.Resources.暂停;
        }

        private void pictureBox6_MouseEnter(object sender, EventArgs e)
        {
            pictureBox6.Image = (Image)Properties.Resources.停止;
        }

        private void pictureBox6_MouseLeave(object sender, EventArgs e)
        {
            pictureBox6.Image = (Image)Properties.Resources.停止;
        }
        int m = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            int i = (int)axWindowsMediaPlayer1.playState;
            switch (i)
            {
                case 1: lblStauts.Text = "状态:停止"; break;
                case 2: lblStauts.Text = "状态:暂停"; break;
                case 3: lblStauts.Text = "状态:播放"; break;
                case 6: lblStauts.Text = "状态:正在缓冲"; break;
                case 9: lblStauts.Text = "状态:正在连接"; break;
                case 10: lblStauts.Text = "状态:准备就绪"; break;
            }
            lbljindu.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString;
            if (m == 1)
            {
                hScrollBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
                hScrollBar1.Minimum = 0;
                hScrollBar1.Value = (int)axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
                hScrollBar2.Value = axWindowsMediaPlayer1.settings.volume;
            }
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.currentPosition = e.NewValue;
        }

        private void hScrollBar2_Scroll(object sender, ScrollEventArgs e)
        {
            axWindowsMediaPlayer1.settings.volume = e.NewValue;
        }

        private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
        {

        }
        #endregion
   


        /// <summary>
        /// 上一首
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox8_Click(object sender, EventArgs e)
        {
            if (i == 1)
            {
                i = 52;
            }
            i--;
            Tools.CiShu = i;
            Music();
            axWindowsMediaPlayer1.URL = Tools.Address;
            m = 1;
            lblSongTitle.Text = " 歌曲名称:"   axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");
        }
        /// <summary>
        /// 下一首
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        int i = 0;
        private void pictureBox9_Click(object sender, EventArgs e)
        {
            
            if (i == 50)
            {
                i = 1;
            }
            i  ;

            Tools.CiShu = i;
            Music();
            axWindowsMediaPlayer1.URL = Tools.Address;
            m = 1;
            lblSongTitle.Text = " 歌曲名称:"   axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {

        }
    }
}


标签: 音乐 播放

实例下载地址

mp3音乐播放器 示例源码(AxWindowsMediaPlayer)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警