本帖最后由 a359702653 于 2011-04-28 20:50:06 编辑

解决方案 »

  1.   

    首先创建一个Music.cs的类using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;namespace WinFormPlaySound
    {
        internal class Music
        {
            [DllImport("winmm.dll")]
            public static extern long PlaySound(String fileName, long a, long b);
            [DllImport("winmm.dll")]
            public static extern long mciSendString(string lpstrCommand, string lpstrReturnString, long length, long hwndcallback);
            /// <summary>
            /// 播放音乐文件(重复)
            /// </summary>
            /// <param name="p_FileName">音乐文件名称</param>
            public static void PlayMusic_Repeat(string p_FileName)
            {
                try
                {
                    mciSendString(@"close temp_music", " ", 0, 0);
                    mciSendString(@"open " + p_FileName + " alias temp_music", " ", 0, 0);
                    mciSendString(@"play temp_music repeat", " ", 0, 0);
                }
                catch
                { }
            }
            /// <summary>
            /// 播放音乐文件
            /// </summary>
            /// <param name="p_FileName">音乐文件名称</param>
            public static void PlayMusic(string p_FileName)
            {
                try
                {
                    mciSendString(@"close temp_music", " ", 0, 0);
                    mciSendString(@"open " + p_FileName + " alias temp_music", " ", 0, 0);
                    mciSendString(@"play temp_music", " ", 0, 0);
                }
                catch
                { }
            }
            /// <summary>
            /// 停止当前音乐播放
            /// </summary>
            /// <param name="p_FileName">音乐文件名称</param>
            public static void StopMusic(string p_FileName)
            {
                try
                {
                    mciSendString(@"close " + p_FileName, " ", 0, 0);
                }
                catch { }
            }
        }
    }然后在窗体Load事件中调用播放方法即可 private void Form1_Load(object sender, EventArgs e)
            {
                //在所需要播放音乐的地方添加如下(按需要添加):
                Music.PlayMusic_Repeat("E:\\MP3\\爱的主打歌.mp3");//循环播放文件
                //Music.PlayMusic("E:\\MP3\\爱的主打歌.mp3");//播放文件(不循环)
                Music.StopMusic("E:\\MP3\\爱的主打歌.mp3");//停止播放文件
            }
      

  2.   

    还有一种编码上简单一点的方法:
    先在工具箱空白处右键点击,选“选择项目...”,然后到“COM组件”这个页面,找到Windows Media Player,选中,这样工具箱里就多了个播放器,然后拖放控件到form上,代码就两行:
    string soundFileName = "foo.mp3";
    axWindowsMediaPlayer1.URL = soundFileName;
    这样就会自动播放
      

  3.   

    需要用到 com组建 或者 网上早些 开源的 音乐解码组建. 
    自己做不现实的
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace ImagesClient
    {
        public partial class frmVideo : Form
        {
            public frmVideo()
            {
                InitializeComponent();
                instance = this;
            }
            private string strVideoPath="";
            private static frmVideo instance = null;
            public static int iOpen = -1;
            public static frmVideo Instance
            {
                set
                {
                }
                get
                {
                    if (instance == null)
                    {
                        iOpen = 0;
                        new frmVideo();
                    }
                    else
                    {
                        iOpen = 1;
                    }
                    return instance;
                }
            }        private static frmMain f;
            public frmMain _frmMain
            {
                get
                {                
                    return f;            }
                set
                {
                    f = value;
                }
            }        private int iFrame(double db)
            {
                int iF = 0;
                double dVideo = this.AxWmp.currentMedia.duration;
                //每秒帧数
                //int sF = 20;
                //string str = db.ToString();            //if (str.IndexOf(".") == -1)
                //{
                //    return iF;
                //}
                //小数点前
                //int  iB = System.Convert.ToInt32(str.Substring(0, str.IndexOf(".")));
                //int iB = (int)Math.Floor(db);            ////小数点后三位            
                //int iA =System.Convert.ToInt32(str.Substring(str.IndexOf(".") + 1));            Decimal de = (decimal)((maxAvi * db) / dVideo);
                //iF = iB * sF + System.Convert.ToInt32(Math.Round(de));
                iF =  System.Convert.ToInt32(Math.Round(de));            return iF;
            }        private void btnPlay_Click(object sender, EventArgs e)
            {
                if (strVideoPath != "")
                {
                    if (AxWmp.playState == WMPLib.WMPPlayState.wmppsPlaying)
                    {
                        this.AxWmp.Ctlcontrols.pause();
                        btnPlay.Text = "播放";
                        timerPlay.Enabled = false;
                        double dob = this.AxWmp.Ctlcontrols.currentPosition;
                        //获得帧数
                        int iF = iFrame(dob);
                        //初始化                                
                        Bitmap bmp = avi.GetFrame(iF);
                        f.bitMap = bmp;
                        f.imgInit();
                    }
                    else
                    {
                        AxWmp.Ctlcontrols.play();
                        btnPlay.Text = "暂停";
                        timerPlay.Enabled = true;
                    }
                }
            }        private void btnClose_Click(object sender, EventArgs e)
            {
                this.Close();
                
            }        private void frmVideo_Load(object sender, EventArgs e)
            {        }        private void frmVideo_FormClosed(object sender, FormClosedEventArgs e)
            {
                instance = null;
                
                f.btnState(frmMain.enm.two);
            }
            AviFile avi;
            int maxAvi;
            private void btnOpen_Click(object sender, EventArgs e)
            {
                this.odlgFile.Filter = "媒体文件|*.mp3;*.wav;*.wma;*.mid;*.avi;*.mpg;*.asf;*.wmv;*.rmvb";
                this.odlgFile.Multiselect = false;
                this.odlgFile.InitialDirectory = Application.StartupPath + @"\video\";
                this.odlgFile.FileName = "";
                if (odlgFile.ShowDialog() == DialogResult.OK)
                {
                    strVideoPath = odlgFile.FileName;
                    AxWmp.URL = strVideoPath;
                    if (AxWmp.playState != WMPLib.WMPPlayState.wmppsPlaying)
                    {
                        btnPlay.Text = "暂停";
                        timerPlay.Enabled = true;                    avi = new AviFile(strVideoPath);
                        maxAvi=avi.GetMaxFrameCount();   
                    }
                }
            }        private void tkbMove_Scroll(object sender, EventArgs e)
            {
                this.AxWmp.Ctlcontrols.currentPosition = (double)this.tkbMove.Value;
            }
            private void btnStop_Click(object sender, EventArgs e)
            {
                if (strVideoPath != "")
                {
                    //停止检测播放状态
                    this.timerPlay.Enabled = false;
                    this.AxWmp.Ctlcontrols.stop();
                    btnPlay.Text = "播放";
                    InfoClear();
                }
            }        void ShowSongInfo()
            {
                //wmppsStopped 停止状态,wmppsUndefined 位置状态
                if (this.AxWmp.playState != WMPLib.WMPPlayState.wmppsStopped && this.AxWmp.playState != WMPLib.WMPPlayState.wmppsUndefined)
                {
                    //获取名称
                    //this.txtName.Text = this.AxWmp.currentMedia.name;
                    //媒体总长度
                    this.tkbMove.Maximum = (int)this.AxWmp.currentMedia.duration;
                    //媒体当前长度
                    this.tkbMove.Value = (int)this.AxWmp.Ctlcontrols.currentPosition;
                    //显示当前时间
                    this.txtTime.Text = this.AxWmp.Ctlcontrols.currentPositionString + '|' + this.AxWmp.currentMedia.durationString;             }
                if (this.AxWmp.playState == WMPLib.WMPPlayState.wmppsStopped)
                {
                    this.timerPlay.Enabled = false;
                    btnPlay.Text = "播放";                
                }
            }
            //信息重置
            void InfoClear()
            {            
                this.tkbMove.Maximum = 0;
                this.tkbMove.Value = 0;
                this.txtTime.Text = "00:00|00:00";            
            }
            private void timerPlay_Tick(object sender, EventArgs e)
            {
                ShowSongInfo();
            }
        }
    }