在资源中加入了音频文件 MSG.WAV  ,如何用SoundPlayer播放该文件。
在MSDN中给的例子是
private void playSoundFromResource()
{
    SoundPlayer sndPing = new SoundPlayer(SoundRes.GetType(), "Ping.wav");
    sndPing.Play();
}
SoundPlayer 没有这种构造函数。
哪位仁兄做过,告诉小弟,小弟万分感激。在线等待,这个东西很着急。

解决方案 »

  1.   

    看错了。
    是using System.Media;
    2.0下面的
      

  2.   

    音频文件在资源中,如何用SoundPlayer 播放。
    能不能给个具体点的例子呢?
      

  3.   

    yaoshuwen()
    能不能给个具体点的例子呢?小弟谢谢了
      

  4.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Media;
    using System.Windows.Forms;namespace SoundApiExample
    {
        public class SoundTestForm : System.Windows.Forms.Form
        {
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.TextBox filepathTextbox;        
            private System.Windows.Forms.Button playOnceSyncButton;
            private System.Windows.Forms.Button playOnceAsyncButton;
            private System.Windows.Forms.Button playLoopAsyncButton;
            private System.Windows.Forms.Button selectFileButton;
            
            private System.Windows.Forms.Button stopButton;
            private System.Windows.Forms.StatusBar statusBar;
            private System.Windows.Forms.Button loadSyncButton;
            private System.Windows.Forms.Button loadAsyncButton;        
            private SoundPlayer player;        public SoundTestForm()
            {
                // Initialize Forms Designer generated code.
                InitializeComponent();

                // Disable playback controls until a valid .wav file 
                // is selected.
                EnablePlaybackControls(false);            // Set up the status bar and other controls.
                InitializeControls();            // Set up the SoundPlayer object.
                InitializeSound();
            }        // Sets up the status bar and other controls.
            private void InitializeControls()
            {
                // Set up the status bar.
                StatusBarPanel panel = new StatusBarPanel();
                panel.BorderStyle = StatusBarPanelBorderStyle.Sunken;
                panel.Text = "Ready.";
                panel.AutoSize = StatusBarPanelAutoSize.Spring;
                this.statusBar.ShowPanels = true;
                this.statusBar.Panels.Add(panel);
            }        // Sets up the SoundPlayer object.
            private void InitializeSound()
            {
                // Create an instance of the SoundPlayer class.
                player = new SoundPlayer();            // Listen for the LoadCompleted event.
                player.LoadCompleted += new AsyncCompletedEventHandler(player_LoadCompleted);            // Listen for the SoundLocationChanged event.
                player.SoundLocationChanged += new EventHandler(player_LocationChanged);
            }        private void selectFileButton_Click(object sender, 
                System.EventArgs e)
            {
                // Create a new OpenFileDialog.
                OpenFileDialog dlg = new OpenFileDialog();            // Make sure the dialog checks for existence of the 
                // selected file.
                dlg.CheckFileExists = true;            // Allow selection of .wav files only.
                dlg.Filter = "WAV files (*.wav)|*.wav";
                dlg.DefaultExt = ".wav";            // Activate the file selection dialog.
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    // Get the selected file's path from the dialog.
                    this.filepathTextbox.Text = dlg.FileName;                // Assign the selected file's path to 
                    // the SoundPlayer object.  
                    player.SoundLocation = filepathTextbox.Text;
                }
            }        // Convenience method for setting message text in 
            // the status bar.
            private void ReportStatus(string statusMessage)
            {
                // If the caller passed in a message...
                if ((statusMessage != null) && (statusMessage != String.Empty))
                {
                    // ...post the caller's message to the status bar.
                    this.statusBar.Panels[0].Text = statusMessage;
                }
            }
            
            // Enables and disables play controls.
            private void EnablePlaybackControls(bool enabled)
            {   
                this.playOnceSyncButton.Enabled = enabled;
                this.playOnceAsyncButton.Enabled = enabled;
                this.playLoopAsyncButton.Enabled = enabled;
                this.stopButton.Enabled = enabled;
            }
        
            private void filepathTextbox_TextChanged(object sender, 
                EventArgs e)
            {
                // Disable playback controls until the new .wav is loaded.
                EnablePlaybackControls(false);
            }        private void loadSyncButton_Click(object sender, 
                System.EventArgs e)
            {   
                // Disable playback controls until the .wav is 
                // successfully loaded. The LoadCompleted event 
                // handler will enable them.
                EnablePlaybackControls(false);            try
                {
                    // Assign the selected file's path to 
                    // the SoundPlayer object.  
                    player.SoundLocation = filepathTextbox.Text;                // Load the .wav file.
                    player.Load();
                }
                catch (Exception ex)
                {
                    ReportStatus(ex.Message);
                }
            }        private void loadAsyncButton_Click(System.Object sender, 
                System.EventArgs e)
            {
                // Disable playback controls until the .wav is 
                // successfully loaded. The LoadCompleted event 
                // handler will enable them.
                EnablePlaybackControls(false);            try
                {
                    // Assign the selected file's path to 
                    // the SoundPlayer object.  
                    player.SoundLocation = this.filepathTextbox.Text;                // Load the .wav file.
                    player.LoadAsync();
                }
                catch (Exception ex)
                {
                    ReportStatus(ex.Message);
                }
            }        // Synchronously plays the selected .wav file once.
            // If the file is large, UI response will be visibly 
            // affected.
            private void playOnceSyncButton_Click(object sender, 
                System.EventArgs e)
            {
                ReportStatus("Playing .wav file synchronously.");
                player.PlaySync();
                ReportStatus("Finished playing .wav file synchronously.");
            }        // Asynchronously plays the selected .wav file once.
            private void playOnceAsyncButton_Click(object sender, 
                System.EventArgs e)
            {
                ReportStatus("Playing .wav file asynchronously.");
                player.Play();
            }        // Asynchronously plays the selected .wav file until the user
            // clicks the Stop button.
            private void playLoopAsyncButton_Click(object sender, 
                System.EventArgs e)
            {
                ReportStatus("Looping .wav file asynchronously.");
                player.PlayLooping();
            }        // Stops the currently playing .wav file, if any.
            private void stopButton_Click(System.Object sender,
                System.EventArgs e)
            {
                player.Stop();
                ReportStatus("Stopped by user.");
            }        // Handler for the LoadCompleted event.
            private void player_LoadCompleted(object sender, 
                AsyncCompletedEventArgs e)
            {   
                string message = String.Format("LoadCompleted: {0}", 
                    this.filepathTextbox.Text);
                ReportStatus(message);
                EnablePlaybackControls(true);
            }        // Handler for the SoundLocationChanged event.
            private void player_LocationChanged(object sender, EventArgs e)
            {   
                string message = String.Format("SoundLocationChanged: {0}", 
                    player.SoundLocation);
                ReportStatus(message);
            }
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Media;namespace LinkPair
    {
        public class GameMusic
        {
            /// <summary>
            /// WAV文件路径
            /// </summary>
            string strMusicPath;        /// <summary>
            /// 是否循环播放
            /// </summary>
            bool isLoop;        /// <summary>
            /// 获取或设置WAV文件路径
            /// </summary>
            public string MusicPath
            {
                get
                {
                    return this.strMusicPath;
                }
                set
                {
                    this.strMusicPath = value;
                }
            }        /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="musicPath">WAV文件路径</param>
            public GameMusic(string musicPath, bool isLoop)
            {
                this.strMusicPath = musicPath;
                this.isLoop = isLoop;
            }        /// <summary>
            /// 播放WAV文件
            /// </summary>
            /// <returns></returns>
            public bool PlayWav()
    {
                SoundPlayer sndPing = new SoundPlayer(strMusicPath);            if (this.isLoop)
                {
                    sndPing.PlayLooping();
                }
                else
                {
                    sndPing.Play();
                }            return true;
    }
        }
    }
      

  6.   

    曾经写的播放wav文件的类,只要调用PlayWav方法即可播放
      

  7.   

    yaoshuwen(), MicroSoftor(此位置不做任何商业活动)
    谢谢你们二位,不过你们可能没有理解我的意思。我是要播放资源中的WAV文件。直接指定路径的我知道怎么做。就是不知道怎么播放资源中的文件。
    我是这样将WAV加入资源的。右击工程-->查看属性页面-->选择资源标签-->然后点击加入资源-->选择现有文档-->加入WAV文件。
      

  8.   

    把wav文件加到资源里后  会自动生成代码Properties.Resources.[NAME]
                using (Stream s = Properties.Resources.[NAME])
                {
                    SoundPlayer sndPing = new SoundPlayer(s);
                    sndPing.Play();
                }
    或者调用Assembly.GetManifestResourceStream(resourceName))获得资源流
      

  9.   

    http://www.cnblogs.com/hfyb/archive/2007/01/23/628232.html
    著個應當是你想要的