public static void PlayFromFile(string filename, int frequency) { using ( FileStream stream = new FileStream(filename, FileMode.Open)) { var waveFormat = WaveFormat.CreateMuLawFormat(frequency * 2, 1); var reader = new NAudio.Wave.RawSourceWaveStream(stream, waveFormat); using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader)) { convertedStream.Seek(0, 0); WaveOutEvent player = new WaveOutEvent(); WaveChannel32 volumeStream = new WaveChannel32(convertedStream); player.Init(volumeStream); player.Play(); while (player.PlaybackState == PlaybackState.Playing) { System.Threading.Thread.Sleep(100); var input = Console.ReadKey(); if (input.KeyChar > 1) ; { player.Stop(); } } } } }
/// <summary> /// Sets position to 0 and pauses the song. Does not release any resources. /// </summary> public void Stop() { //Reset position in case of new start reader.Position = 0; output.Stop(); }
public void StopSound() { waveOutEvent.Stop(); }