ビープ音・システムサウンド音を鳴らす - C#

C#でビープ音、システムサウンド音を鳴らすコードを紹介します。

概要

ビープやシステムサウンド音を鳴らすには以下の方法があります。
  • Conlole.Beep()を利用する
  • Microsoft.VisualBasic.Interaction.Beep() を利用する
  • Windows API の MessageBeep() を利用する
  • Windows API の Beep()を利用する
  • System.Media.SystemSounds クラスのメソッドを利用する

プログラム例

UI

Windowsアプリケーションを新規作成しフォームにボタンを5つ配置します。

コード

以下のコードを記述します。
using Microsoft.VisualBasic;
using System.Runtime.InteropServices;


namespace BeepDemo
{
  public partial class FormMain : Form
  {
    public enum beepType : uint
    {
      /// <summary>
      /// A simple windows beep
      /// </summary>            
      SimpleBeep = 0xFFFFFFFF,
      /// <summary>
      /// A standard windows OK beep
      /// </summary>
      OK = 0x00,
      /// <summary>
      /// A standard windows Question beep
      /// </summary>
      Question = 0x20,
      /// <summary>
      /// A standard windows Exclamation beep
      /// </summary>
      Exclamation = 0x30,
      /// <summary>
      /// A standard windows Asterisk beep
      /// </summary>
      Asterisk = 0x40,
    }

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool MessageBeep(beepType uType);

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool Beep(uint dwFreq, uint dwDuration);


    public FormMain()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
      Console.Beep();
    }

    private void button2_Click(object sender, EventArgs e)
    {
      Interaction.Beep();
    }

    private void button3_Click(object sender, EventArgs e)
    {
      MessageBeep(0);
    }

    private void button4_Click(object sender, EventArgs e)
    {
      Beep(700, 40);
    }

    private void button5_Click(object sender, EventArgs e)
    {
      System.Media.SystemSounds.Beep.Play();
      //System.Media.SystemSounds.Asterisk.Play();	
      //System.Media.SystemSounds.Exclamation.Play();	
      //System.Media.SystemSounds.Hand.Play();
      //System.Media.SystemSounds.Question.Play();
    }
  }
}

解説

button1のクリックイベントでは、Console.Beep(); メソッドを呼び出し、ビープ音を鳴らします。
    private void button1_Click(object sender, EventArgs e)
    {
      Console.Beep();
    }

button2のクリックイベントでは、Interaction.Beep(); メソッドを呼び出し、Windowsのシステムサウンドを鳴らします。
Windows APIを呼び出すため、DllImport属性を利用して、MessageBeep 関数をインポートしています。
    private void button2_Click(object sender, EventArgs e)
    {
      Interaction.Beep();
    }

button3のクリックイベントでは、MessageBeep(0); Windows APIを呼び出し、Windowsのシステムサウンドを鳴らします。
    private void button3_Click(object sender, EventArgs e)
    {
      MessageBeep(0);
    }

button4のクリックイベントでは、Beep(0); Windows APIを呼び出し、ビープ音を鳴らします。
Windows APIを呼び出すため、DllImport属性を利用して、Beep 関数をインポートしています。

1つ目の引数に、鳴らす音の周波数 (ヘルツ単位)を与えます。2つ目の引数に、サウンドの再生時間 (ミリ秒単位)を与えます。 下記コードでは、700ヘルツのビープ音を40ミリ秒再生します。
    private void button4_Click(object sender, EventArgs e)
    {
      Beep(700,40);
    }

button5のクリックイベントでは、System.Media.SystemSounds クラスのメソッドを利用してシステムサウンド恩を再生します。
    private void button5_Click(object sender, EventArgs e)
    {
      System.Media.SystemSounds.Beep.Play();
      //System.Media.SystemSounds.Asterisk.Play();	
      //System.Media.SystemSounds.Exclamation.Play();	
      //System.Media.SystemSounds.Hand.Play();
      //System.Media.SystemSounds.Question.Play();
    }

実行結果

プロジェクトを実行します。

下図のウィンドウが表示されます。


  • Button1をクリックするとスピーカーまたは、マザーボードのスピーカーから音が出ます。(実行しているPCにより違いあり)
  • Button2,Button3はWindowsで設定したサウンドが鳴ります。
  • Button4をクリックするとスピーカーまたは、マザーボードのスピーカーから音が鳴りますが、Button1とは違った音の高さと長さで鳴ります。
  • Button5をクリックするとWindowsで設定したサウンドが鳴ります。

System.Media.SystemSounds クラスを利用する場合

Windowsのシステムサウンドを再生する場合は、System.Media.SystemSounds名前空間のクラスを利用できます。
システムサウンドは以下のメソッドで再生できます。

メソッド 動作
System.Media.SystemSounds.Asterisk.Play();メッセージ(情報)のサウンド再生
System.Media.SystemSounds.Beep.Play();一般の警告音の再生
System.Media.SystemSounds.Exclamation.Play();メッセージ(警告)のサウンド再生
System.Media.SystemSounds.Hand.Play();システムエラーのサウンド再生
System.Media.SystemSounds.Question.Play();メッセージ(問い合わせ)のサウンド再生

著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
掲載日: 2008-12-03
iPentec all rights reserverd.