Play audio from a stream using C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, playing audio from a stream usually means two steps: decode the stream into a playable audio format and feed that decoded audio to an output device. The exact code depends on the audio format and the library you use. In practice, NAudio is the most common answer because the built-in .NET APIs are limited for modern streaming scenarios.
A Practical NAudio Pattern
If you already have a stream that contains WAV data, NAudio can play it directly.
This works because WaveFileReader understands the WAV structure and WaveOutEvent sends the decoded audio to the output device.
Streams Are Not Automatically Decoded
A Stream is only a source of bytes. It is not a guarantee that the bytes are ready for playback.
For example:
- WAV stream: often readable directly with
WaveFileReader - MP3 stream: needs an MP3-capable reader or decoder
- raw PCM stream: needs the correct format metadata
That is the first conceptual point to get right. The playback library needs to know what format the stream contains.
Example With an In-Memory Stream
This shows the difference between "play a file path" and "play from a stream." Once the bytes are already in a Stream, the reader works against that stream rather than reopening the file by path.
Network and Buffered Streaming
For real streaming scenarios such as HTTP audio, you often need buffering rather than just reading from a fully available stream once.
That usually means:
- receiving bytes incrementally
- decoding them as they arrive
- feeding a buffered provider to the output device
The exact implementation depends on the codec and latency requirements. That is why one fixed code sample cannot cover every network audio format cleanly.
Dispose Objects in the Right Order
Audio playback code often holds unmanaged resources such as audio devices or decoder handles. Use using blocks or equivalent disposal patterns so readers and output devices are cleaned up properly.
If you keep the output device alive longer than the reader, or dispose the stream too early, playback can fail in ways that look unrelated to the original stream logic.
Common Pitfalls
The most common mistake is assuming a Stream can be played directly without knowing the actual audio format.
Another issue is using a reader that does not match the stream content, such as treating MP3 bytes as WAV data.
Developers also often forget that playback is asynchronous with respect to the output device. If the process exits or objects are disposed immediately, the sound may stop before playback completes.
Finally, for real-time network streaming, do not assume a simple memory-stream example is enough. Live streaming usually needs buffering and format-aware decoding.
Summary
- In C#, playing audio from a stream means decoding bytes and feeding them to an output device.
- NAudio is a common practical choice for stream-based audio playback.
- Choose the reader based on the actual stream format, such as WAV versus MP3.
- Manage lifetime and disposal carefully so playback can complete correctly.
- For live network audio, expect to need buffering rather than only a one-shot stream reader.
Related reading
- Populating a list of integers in .NET
- Possible to call C code from C?
- PowerShell script to return versions of .NET Framework on a machine?
- Practical difference between List and IEnumerable
- Practical example where Tuple can be used in .Net 4.0?
- Predefined type 'System.Object' is not defined or imported .net 4.6
- Prevent Caching in ASP.NET MVC for specific actions using an attribute
- Prevent multiple instances of a given app in .NET?

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.