Class Mp3FileReaderBase
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
inputStreamStreamThe incoming stream containing MP3 data
frameDecompressorBuilderMp3FileReaderBase.FrameDecompressorBuilderFactory 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
inputStreamStreamInput stream
frameDecompressorBuilderMp3FileReaderBase.FrameDecompressorBuilderFactory method to build a frame decompressor
ownInputStreamboolWhether we own the stream and should dispose it
Exceptions
Mp3FileReaderBase(string, FrameDecompressorBuilder)
Supports opening a MP3 file
public Mp3FileReaderBase(string mp3FileName, Mp3FileReaderBase.FrameDecompressorBuilder frameDecompressorBuilder)
Parameters
mp3FileNamestringMP3 File name
frameDecompressorBuilderMp3FileReaderBase.FrameDecompressorBuilderFactory 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
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
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
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
Position
public override long Position { get; set; }
Property Value
WaveFormat
public override WaveFormat WaveFormat { get; }
Property Value
XingHeader
Xing header if present
public XingHeader XingHeader { get; }
Property Value
Methods
Dispose(bool)
Disposes this WaveStream
protected override void Dispose(bool disposing)
Parameters
disposingbool
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
cancellationTokenCancellationTokenToken observed between frames during the scan.
Returns
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
Returns
Read(Span<byte>)
Reads decompressed PCM data from our MP3 file.
public override int Read(Span<byte> sampleBuffer)
Parameters
Returns
ReadNextFrame()
Reads the next mp3 frame
public Mp3Frame ReadNextFrame()
Returns
- Mp3Frame
Next mp3 frame, or null if EOF