Play tones with specific frequencies in C#

[Play tones]

This example shows how you can play tones with a given frequency and duration. When the user clicks the Play button, the following code plays a tone.

private void btnPlay_Click(object sender, EventArgs e)
    int freq = int.Parse(txtFrequency.Text);
    int duration = int.Parse(txtDuration.Text);

    System.Console.Beep(freq, duration);

The code parses the tone’s frequency (Hz) and duration (ms) from text boxes. It then calls System.Console.Beep to play the tone for the indicated duration.

Note that System.Console.Beep is synchronous so it waits the specified duration before returning. That means you can’t use this method to play a tone while the program does other things and you can’t use this method to play multiple tones at the same time.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in audio, multimedia and tagged , , , , , , , , , , , , . Bookmark the permalink.