如何使用平台调用播放 WAV 文件(C# 编程指南)

下面的 C# 代码示例说明了如何使用平台调用服务在 Windows 操作系统中播放 WAV 声音文件。

示例

此示例代码使用 DllImportAttributewinmm.dllPlaySound 方法入口点导入为 Form1 PlaySound()。 本示例具有一个带按钮的简单 Windows 窗体。 单击该按钮将打开一个标准的 Windows OpenFileDialog 对话框,以便你可以打开要播放的文件。 选中波形文件后,该文件将使用 winmm.dll 库的 PlaySound() 方法播放。 有关此方法的详细信息,请参阅使用 PlaySound 功能处理波形音频文件。 浏览并选择具有 .wav 扩展名的文件,然后单击“打开”以使用平台调用播放波形文件。 文本框中显示所选文件的完整路径。

通过筛选器设置对“打开文件”对话框进行筛选,以仅显示扩展名为 .wav 的文件:

dialog1.Filter = "Wav Files (*.wav)|*.wav";
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WinSound
{
    public partial class Form1 : Form
    {
        private TextBox textBox1;
        private Button button1;

        public Form1()  // Constructor.
        {
            InitializeComponent();
        }

        [DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true, CharSet = CharSet.Unicode, ThrowOnUnmappableChar = true)]
        private static extern bool PlaySound(string szSound, System.IntPtr hMod, PlaySoundFlags flags);

        [System.Flags]
        public enum PlaySoundFlags : int
        {
            SND_SYNC = 0x0000,
            SND_ASYNC = 0x0001,
            SND_NODEFAULT = 0x0002,
            SND_LOOP = 0x0008,
            SND_NOSTOP = 0x0010,
            SND_NOWAIT = 0x00002000,
            SND_FILENAME = 0x00020000,
            SND_RESOURCE = 0x00040004
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            var dialog1 = new OpenFileDialog();

            dialog1.Title = "Browse to find sound file to play";
            dialog1.InitialDirectory = @"c:\";
            dialog1.Filter = "Wav Files (*.wav)|*.wav";
            dialog1.FilterIndex = 2;
            dialog1.RestoreDirectory = true;

            if (dialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = dialog1.FileName;
                PlaySound(dialog1.FileName, new System.IntPtr(), PlaySoundFlags.SND_SYNC);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Including this empty method in the sample because in the IDE,
            // when users click on the form, generates code that looks for a default method
            // with this name. We add it here to prevent confusion for those using the samples.
        }
    }
}

编译代码

  1. 在 Visual Studio 中创建一个新的 C# Windows Forms 应用程序项目,并将其命名为“WinSound”。

  2. 复制上面的代码,将其粘贴到 Form1.cs 文件的内容中。

  3. 复制以下代码,然后将其粘贴到 方法中 Form1.Designer.cs 文件的任何现有代码之后。

    this.button1 = new System.Windows.Forms.Button();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(192, 40);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(88, 24);
    this.button1.TabIndex = 0;
    this.button1.Text = "Browse";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // textBox1
    //
    this.textBox1.Location = new System.Drawing.Point(8, 40);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(168, 20);
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "FIle path";
    //
    // Form1
    //
    this.AutoScaleDimensions = new System.Drawing.SizeF(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Platform Invoke WinSound C#";
    this.ResumeLayout(false);
    this.PerformLayout();
    
  4. 编译并运行该代码。

另请参阅