MediaPlayer player = new MediaPlayer(); player.Open(new Uri("path/to/audio.mp3")); player.Play(); // plays the audio player.Close(); // closes the instance of the MediaPlayer
private void StopButton_Click(object sender, RoutedEventArgs e) { MediaPlayer player = new MediaPlayer(); player.Open(new Uri("path/to/audio.mp3")); player.Play(); // plays the audio player.Stop(); // stops the audio player.Close(); // closes the instance of the MediaPlayer }In this example, we create a new instance of MediaPlayer, open an audio file, play the audio, stop the audio, and then close the instance of the MediaPlayer. Package library: System.Windows.Media namespace in the PresentationCore.dll assembly.