请问在WinForm环境下,如何添加背景音乐?有人说用wmp控件,可是我添加了com引用,又重新生成解决方案以后,依然没有找到这个wmp控件,请问这是怎么回事?

解决方案 »

  1.   

    1:使用控件Microsoft   MutileMedia   Control   Version   6.0,可在Vs.net中添加此控件.
       取名为player.   
          '播放背景音乐   
                                            player.FileName   =   Application.StartupPath "\Music\GameMain.mid"   
                                             player.Command   =   "Open"   
                                           player.Command   =   "Play"
    这种方法我测试过就可以播放wma和wmv格式的音乐文件,而且在播放wmv的时候会新建窗口
    2.你可以用 directx 9 的托管DirectX.AudioVideoPlayback.DLL里面有个类命,你可以在C:的Microsoft.net的文件夹里面找到这个文件,用这个类Audio,用来播放音乐很方便,
    Audio v=Audio(".mp3");
    v.ending +=new .....//添加事件处理函数!在他的事件处理函数里可以写
    v.open("声音文件名!")
    v.play();
    详细的可以自己处理一下就行了!  
      

  2.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.InteropServices;
    namespace 播放声音
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.ComponentModel.IContainer components;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Timer timer1; const string FILE_NAME = @"..\..\music\background.WAV";
    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(40, 72);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "播放音乐1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(152, 72);
    this.button2.Name = "button2";
    this.button2.TabIndex = 1;
    this.button2.Text = "播放音乐2";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // timer1
    // 
    this.timer1.Interval = 5000;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(192)), ((System.Byte)(0)));
    this.ClientSize = new System.Drawing.Size(272, 174);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Opacity = 0.5;
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } internal class Helpers1 
    {
    [Flags]
    public enum PlaySoundFlags : int 
    {
    SND_SYNC = 0x0000,  /* play synchronously (default) */ //同步
    SND_ASYNC = 0x0001,  /* play asynchronously */ //异步
    SND_NODEFAULT = 0x0002,  /* silence (!default) if sound not found */
    SND_MEMORY = 0x0004,  /* pszSound points to a memory file */
    SND_LOOP = 0x0008,  /* loop the sound until next sndPlaySound */
    SND_NOSTOP = 0x0010,  /* don't stop any currently playing sound */
    SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
    SND_ALIAS = 0x00010000, /* name is a registry alias */
    SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
    SND_FILENAME = 0x00020000, /* name is file name */
    SND_RESOURCE = 0x00040004  /* name is resource name or atom */
    } [DllImport("winmm")]
    public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags );
    }
    internal class Helpers2 
    {
    [Flags]
    public enum PlaySoundFlags : int 
    {
    SND_SYNC = 0x0000,  /* play synchronously (default) */ //同步
    SND_ASYNC = 0x0001,  /* play asynchronously */ //异步
    SND_NODEFAULT = 0x0002,  /* silence (!default) if sound not found */
    SND_MEMORY = 0x0004,  /* pszSound points to a memory file */
    SND_LOOP = 0x0008,  /* loop the sound until next sndPlaySound */
    SND_NOSTOP = 0x0010,  /* don't stop any currently playing sound */
    SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
    SND_ALIAS = 0x00010000, /* name is a registry alias */
    SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
    SND_FILENAME = 0x00020000, /* name is file name */
    SND_RESOURCE = 0x00040004  /* name is resource name or atom */
    } [DllImport("winmm")]
    public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags );
    } public class Sound 
    {
    public static void Play( string strFileName,int sign)
    {
    //Helpers1.PlaySound( strFileName, IntPtr.Zero, Helpers1.PlaySoundFlags.SND_FILENAME | Helpers1.PlaySoundFlags.SND_ASYNC );
    //Helpers1 Helpers1=new Helpers1();
    if(sign==1)
    Helpers1.PlaySound( strFileName, IntPtr.Zero, Helpers1.PlaySoundFlags.SND_FILENAME | Helpers1.PlaySoundFlags.SND_ASYNC |  Helpers1.PlaySoundFlags.SND_NODEFAULT);
    else
    Helpers2.PlaySound( strFileName, IntPtr.Zero, Helpers2.PlaySoundFlags.SND_FILENAME | Helpers2.PlaySoundFlags.SND_ASYNC);
    }
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
    //Sound sound=new Sound();
    Sound.Play(FILE_NAME,1);
    } private void button2_Click(object sender, System.EventArgs e)
    {
    //Sound sound=new Sound();
    Sound.Play(@"..\..\music\front.WAV",2);
    } private void timer1_Tick(object sender, System.EventArgs e)
    {
    //Helpers1.PlaySound(@"F:\c#\智能象棋游戏\sound\jiangjun.WAV", IntPtr.Zero, Helpers1.PlaySoundFlags.SND_FILENAME | Helpers1.PlaySoundFlags.SND_SYNC);
    } private void Form1_Load(object sender, System.EventArgs e)
    {
                Sound.Play(FILE_NAME, 1);
    }
    }}