C# - Audio
C#
Suonare un segnale acustico
- È possibile suonare un segnale acustico di una data frequenza e per una certa durata.
Console.Beep(Convert.ToInt32(txtFrequency.Text), Convert.ToInt32(txtDuration.Text));
Suonare un file wav
- È possibile suonare un file wave, utilizzando la classe SoundPlayer.
using System.Media;
SoundPlayer player = new SoundPlayer(txtFile.Text);
try
{
player.Play();
}
catch (Exception)
{
MessageBox.Show("An error occurred while playing media.");
}
finally
{
player.Dispose();
}
Suonare un file audio con Windows Media Player
- Per suonare un file audio, si può utilizzare la dll del Media Player.
- Aggiungere nei riferimenti il file ‘wmp.dll’ che dovrebbe trovarsi nella cartella ‘Windows\System32’.
- Creare l’oggetto:
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
- Per il Play:
private void btnPlay_Click(object sender, EventArgs e)
{
wplayer.URL = @"C:\Users\Public\Music\Sample Music\Maid with the Flaxen Hair.mp3";
wplayer.controls.play();
}
- Per lo Stop:
private void btnStop_Click(object sender, EventArgs e)
{
wplayer.controls.stop();
}