This is a simple console application that plays an audio file (e.g., .mp3) using the NAudio library in C#. The application will play the audio in a loop until the user presses a key to stop it.
- Plays
.mp3files. - Loops the audio until the user stops it.
- Supports basic console interaction (press any key to stop music).
- .NET Core or .NET 5/6/7+ (Windows, Linux, macOS).
- The NAudio library.
-
Create a new .NET console application:
dotnet new console -n AudioPlayerApp cd AudioPlayerApp -
Install NAudio via NuGet:
dotnet add package NAudio
-
Add the following code to your
Program.cs:using System; using NAudio.Wave; class Program { static void Main() { // Initialize the Audio File Reader for MP3 using (var audioFile = new AudioFileReader("C:\\Path\\To\\Your\\Music.mp3")) using (var outputDevice = new WaveOutEvent()) { // Set the output device and play the file outputDevice.Init(audioFile); outputDevice.Play(); Console.WriteLine("Playing music. Press any key to stop..."); Console.ReadKey(); // Stop the music outputDevice.Stop(); } } }
- Replace
"C:\\Path\\To\\Your\\Music.mp3"with the actual path to your.mp3file.
- Replace
-
Run the application:
dotnet run
-
Stopping the Music:
- Press any key to stop the music and exit the application.
- This example uses the
NAudiolibrary, which supports.mp3,.wav, and other audio formats. - The audio will play asynchronously in the background and will stop once a key is pressed.