How to play a sound in C, .NET
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
C# and .NET provide several ways to play audio, from the simple System.Media.SoundPlayer (WAV only, Windows) to the cross-platform NAudio library (WAV, MP3, and more) and the modern Windows.Media.Playback API for UWP/WinUI apps. The right choice depends on the audio format, platform, and complexity — SoundPlayer for simple WAV alerts, NAudio for full audio control, and MediaPlayer for modern Windows apps.
System.Media.SoundPlayer (Simplest — WAV Only)
Play from an embedded resource:
Play system sounds:
SoundPlayer only supports WAV format. It is Windows-only and not available in .NET MAUI or cross-platform projects.
Console.Beep (Simplest Possible)
Console.Beep(frequency, duration) is Windows-only and blocks the thread for the specified duration.
NAudio Library (Full-Featured)
NAudio supports WAV, MP3, AIFF, WMA, and more:
Play MP3:
NAudio with Volume and Position Control
Windows.Media.Playback (UWP / WinUI)
For modern Windows apps:
Cross-Platform with .NET MAUI
Playing Audio from a Stream
Comparison
| Method | Formats | Platform | Async | Complexity |
SoundPlayer | WAV only | Windows | Yes (Play) | Minimal |
Console.Beep | Tone only | Windows | No (blocking) | Trivial |
| NAudio | WAV, MP3, AIFF, WMA | Windows | Yes | Medium |
MediaPlayer (WinUI) | MP3, WAV, AAC, etc. | Windows 10+ | Yes | Medium |
| MAUI AudioManager | Platform-dependent | Cross-platform | Yes | Medium |
Common Pitfalls
- Using
SoundPlayerfor MP3 files:SoundPlayeronly supports uncompressed WAV. Attempting to play an MP3 produces silence or an exception. Use NAudio orMediaPlayerfor MP3 playback. - Not disposing audio resources:
WaveOutEvent,AudioFileReader, andSoundPlayerhold unmanaged audio device handles. Failing to dispose them causes resource leaks and may block other audio playback. Always useusingstatements. - Blocking the UI thread with
PlaySync():SoundPlayer.PlaySync()blocks the calling thread until the sound finishes. In a GUI app, this freezes the UI. UsePlay()(async) or run playback on a background thread. - NAudio not available on Linux/macOS: NAudio uses Windows audio APIs (WaveOut, WASAPI, DirectSound). For cross-platform audio, use libraries like
OpenALbindings,SDL2, or the MAUI Community Toolkit. - Volume set to zero by default: Some audio APIs default to zero volume or muted state. Always set
outputDevice.Volumeor check system mixer settings if you hear no sound.
Summary
- Use
SoundPlayerfor simple WAV alerts on Windows — minimal code, no external packages - Use
Console.Beep()for basic tone generation — no files needed - Use NAudio for MP3 playback, volume control, seeking, and advanced audio features on Windows
- Use
Windows.Media.Playback.MediaPlayerfor UWP and WinUI applications - Use the MAUI Community Toolkit for cross-platform audio in .NET MAUI apps
- Always dispose audio objects with
usingto prevent resource leaks
Related reading
- How to preserve HttpContext in Web API async task
- How to prevent blank xmlns attributes in output from .NET's XmlDocument?
- How to prevent ReflectionTypeLoadException when calling Assembly.GetTypes
- How to print the current Stack Trace in .NET without any exception?
- How to programmatically click a button in WPF?
- How to properly and completely close/reset a TcpClient connection?
- How to properly idiomatic avoid CS9107 when passing primary constructor parameters to base
- How to properly implement kafka consumer as a background service on .NET Core

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.