如何使用平台叫用播放 WAV 檔

下列 C# 程式碼範例說明了如何在 Windows 作業系統上使用平台叫用服務來播放 WAV 音效檔。

範例

這個範例程式碼會使用 DllImportAttribute,以將 winmm.dllPlaySound 方法進入點匯入為 Form1 PlaySound()。 這個範例包含具有按鈕的簡單 Windows Form。 按一下按鈕會開啟標準視窗 OpenFileDialog 對話方塊,讓您可以開啟要播放的檔案。 選取音訊檔時,會使用 winmm.dll 程式庫檔的 PlaySound() 方法來進行播放。 如需此方法的詳細資訊,請參閱 Using the PlaySound function with Waveform-Audio Files (於波形音訊檔案使用 PlaySound 函式)。 瀏覽並選取副檔名為 .wav 的檔案,然後選取 [開啟],以使用平台叫用來播放音訊檔案。 文字方塊會顯示所選取檔案的完整路徑。

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:\";
        //<Snippet5>
        dialog1.Filter = "Wav Files (*.wav)|*.wav";
        //</Snippet5>
        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.
    }
}

透過篩選設定來篩選 [開啟檔案] 對話方塊,使其只顯示副檔名為 .wav 的檔案。

編譯程式碼

在 Visual Studio 中建立新的 C# Windows Forms 應用程式專案,並將其命名為 WinSound。 複製上述程式碼,並將其貼至 Form1.cs 檔案的內容上。 複製下列程式碼,並將其貼入 Form1.Designer.cs 檔案的 InitializeComponent() 方法中的任何現有程式碼後面。

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();

編譯並執行程式碼。

另請參閱