WaveOutEvent player = new WaveOutEvent(); AudioFileReader audio = new AudioFileReader("music.wav"); //assume music file exists in project directory player.Init(audio); player.Play(); //audio is playing here player.Stop(); //audio has stopped playing
private WaveOutEvent player; private AudioFileReader audio; private void PlayAudio(string filename) { audio = new AudioFileReader(filename); player = new WaveOutEvent(); player.Init(audio); player.Play(); } private void StopAudio() { if (player != null) { player.Stop(); player.Dispose(); audio.Dispose(); } }In this example, a PlayAudio method is created to handle audio playback. It creates a new AudioFileReader and WaveOutEvent objects, initializes the player with the audio file, and plays the audio. A StopAudio method is created to stop audio playback. It checks if the player object exists and calls the Stop, Dispose methods on both the player and audio objects.