C# play sound
- User
C# play sound
- Assistant
To play a sound in C#, you can use the
System.Media.SoundPlayerclass. Here is an example code snippet that demonstrates playing a sound:csharpusing System; using System.Media; class Program { static void Main() { // Create a new SoundPlayer instance and supply the sound file path SoundPlayer player = new SoundPlayer(@"C:\path\to\sound.wav"); // Play the sound player.Play(); // Wait for the sound to finish playing while (player.IsLoadCompleted == false) { // Do nothing and wait } // Clean up resources player.Dispose(); } }Replace
"C:\path\to\sound.wav"with the actual path and filename of the sound you want to play. This example code will play the sound and wait for it to finish playing before proceeding with the program execution. - User
sound file is embedded resource
- Assistant
If the sound file is embedded as a resource in your C# project, you can use the
System.Reflection.Assemblyclass to access and play the embedded sound file. Here's an example of how you can play an embedded sound file:csharpusing System; using System.IO; using System.Media; using System.Reflection; class Program { static void Main() { // Get the embedded sound file resource Assembly assembly = Assembly.GetExecutingAssembly(); Stream soundStream = assembly.GetManifestResourceStream("YourNamespace.sound.wav"); // Create a new SoundPlayer instance and load the sound from the stream SoundPlayer player = new SoundPlayer(soundStream); // Play the sound player.Play(); // Wait for the sound to finish playing while (player.IsLoadCompleted == false) { // Do nothing and wait } // Clean up resources soundStream.Dispose(); player.Dispose(); } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.