Table of Contents

Class Mp3FileReaderBase

Namespace
NAudio.Wave
Assembly
NAudio.Core.dll

Class for reading from MP3 files

public class Mp3FileReaderBase : WaveStream, IAsyncDisposable, IDisposable, IWaveProvider
Inheritance
Mp3FileReaderBase
Implements
Derived
Inherited Members
Extension Methods

Constructors

Mp3FileReaderBase(Stream, FrameDecompressorBuilder)

Opens MP3 from a stream rather than a file Will not dispose of this stream itself

public Mp3FileReaderBase(Stream inputStream, Mp3FileReaderBase.FrameDecompressorBuilder frameDecompressorBuilder)

Parameters

inputStream Stream

The incoming stream containing MP3 data

frameDecompressorBuilder Mp3FileReaderBase.FrameDecompressorBuilder

Factory method to build a frame decompressor

Mp3FileReaderBase(Stream, FrameDecompressorBuilder, bool)

Constructor that takes an input stream and a frame decompressor builder

protected Mp3FileReaderBase(Stream inputStream, Mp3FileReaderBase.FrameDecompressorBuilder frameDecompressorBuilder, bool ownInputStream)

Parameters

inputStream Stream

Input stream

frameDecompressorBuilder Mp3FileReaderBase.FrameDecompressorBuilder

Factory method to build a frame decompressor

ownInputStream bool

Whether we own the stream and should dispose it

Exceptions

ArgumentNullException

Mp3FileReaderBase(string, FrameDecompressorBuilder)

Supports opening a MP3 file

public Mp3FileReaderBase(string mp3FileName, Mp3FileReaderBase.FrameDecompressorBuilder frameDecompressorBuilder)

Parameters

mp3FileName string

MP3 File name

frameDecompressorBuilder Mp3FileReaderBase.FrameDecompressorBuilder

Factory method to build a frame decompressor

Properties

Id3v1Tag

ID3v1 tag if present

public byte[] Id3v1Tag { get; }

Property Value

byte[]

Id3v2Tag

ID3v2 tag if present

public Id3v2Tag Id3v2Tag { get; }

Property Value

Id3v2Tag

IsLengthExact

Returns true if Length reflects the exact frame-summed sample count, false if it is an estimate from the first frame's bitrate. False is only possible for headerless VBR files; CBR files and VBR files with a Xing/Info header report true immediately after the constructor returns.

public bool IsLengthExact { get; }

Property Value

bool

Length

This is the length in bytes of data available to be read out from the Read method (i.e. the decompressed MP3 length).

public override long Length { get; }

Property Value

long

Remarks

Length is exact when the MP3 has a Xing/Info Frames field (most VBR encoders write one), or for CBR files (computed from the first frame's bitrate). For headerless VBR files Length is an estimate until enough of the file has been read sequentially or EnsureExactLengthAsync(CancellationToken) has run. Check IsLengthExact to disambiguate.

Mp3WaveFormat

The MP3 wave format (n.b. NOT the output format of this stream - see the WaveFormat property)

public Mp3WaveFormat Mp3WaveFormat { get; }

Property Value

Mp3WaveFormat

Position

public override long Position { get; set; }

Property Value

long

WaveFormat

public override WaveFormat WaveFormat { get; }

Property Value

WaveFormat

XingHeader

Xing header if present

public XingHeader XingHeader { get; }

Property Value

XingHeader

Methods

Dispose(bool)

Disposes this WaveStream

protected override void Dispose(bool disposing)

Parameters

disposing bool

EnsureExactLengthAsync(CancellationToken)

Forces a full scan of the MP3 frame index so Length reports the exact frame-summed sample count and IsLengthExact returns true. No-op (returns a completed task) if Length is already exact — which it is from the constructor for CBR files and for VBR files carrying a Xing/Info header. Safe to call concurrently with Read and Position changes; scan is serialised against playback by the same internal lock. Restores the current playback position when complete.

public Task EnsureExactLengthAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Token observed between frames during the scan.

Returns

Task

Remarks

This is about Length only — it is not a prerequisite for seeking. Position extends the frame index on demand whatever this reports.

Read(byte[], int, int)

Reads decompressed PCM data from our MP3 file.

public override int Read(byte[] sampleBuffer, int offset, int numBytes)

Parameters

sampleBuffer byte[]
offset int
numBytes int

Returns

int

Read(Span<byte>)

Reads decompressed PCM data from our MP3 file.

public override int Read(Span<byte> sampleBuffer)

Parameters

sampleBuffer Span<byte>

Returns

int

ReadNextFrame()

Reads the next mp3 frame

public Mp3Frame ReadNextFrame()

Returns

Mp3Frame

Next mp3 frame, or null if EOF