C#
audio streaming
media playback
programming
software development

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.

Browse interview questions

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.

csharp
1using System;
2using System.IO;
3using NAudio.Wave;
4
5public static class AudioPlayer
6{
7    public static void PlayWavStream(Stream stream)
8    {
9        using var reader = new WaveFileReader(stream);
10        using var output = new WaveOutEvent();
11        output.Init(reader);
12        output.Play();
13
14        while (output.PlaybackState == PlaybackState.Playing)
15        {
16            System.Threading.Thread.Sleep(100);
17        }
18    }
19}

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

csharp
1using System.IO;
2
3byte[] wavBytes = File.ReadAllBytes("sample.wav");
4using var memoryStream = new MemoryStream(wavBytes);
5AudioPlayer.PlayWavStream(memoryStream);

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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.