Xeno-rat
Loading...
Searching...
No Matches
NAudio.Wave.MediaFoundationReader Class Reference

Class for reading any file that Media Foundation can play Will only work in Windows Vista and above Automatically converts to PCM If it is a video file with multiple audio streams, it will pick out the first audio stream. More...

Inheritance diagram for NAudio.Wave.MediaFoundationReader:
Collaboration diagram for NAudio.Wave.MediaFoundationReader:

Classes

class  MediaFoundationReaderSettings
 Allows customisation of this reader class. More...
 

Public Member Functions

 MediaFoundationReader (string file)
 Creates a new MediaFoundationReader based on the supplied file.
 
 MediaFoundationReader (string file, MediaFoundationReaderSettings settings)
 Creates a new MediaFoundationReader based on the supplied file.
 
override int Read (byte[] buffer, int offset, int count)
 Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
 
- Public Member Functions inherited from NAudio.Wave.WaveStream
override void Flush ()
 Flushes the buffer of the current stream and causes any buffered data to be written to the underlying device.
 
override long Seek (long offset, SeekOrigin origin)
 Sets the position within the current stream.
 
override void SetLength (long length)
 Throws a NotSupportedException with the message "Can't set length of a WaveFormatString".
 
override void Write (byte[] buffer, int offset, int count)
 Throws a NotSupportedException with the message "Can't write to a WaveFormatString".
 
void Skip (int seconds)
 Skips the playback to a new position in the audio file based on the specified number of seconds.
 
virtual bool HasData (int count)
 Checks if there is data available at the current position.
 

Protected Member Functions

 MediaFoundationReader ()
 Default constructor.
 
void Init (MediaFoundationReaderSettings initialSettings)
 Initializes the MediaFoundationReader with the specified settings.
 
virtual IMFSourceReader CreateReader (MediaFoundationReaderSettings settings)
 Creates a Media Foundation source reader with the specified settings.
 
override void Dispose (bool disposing)
 Releases the unmanaged resources used by the ClassName and optionally releases the managed resources.
 

Properties

override WaveFormat WaveFormat [get]
 WaveFormat of this stream (n.b. this is after converting to PCM)
 
override long Length [get]
 The bytesRequired of this stream in bytes (n.b may not be accurate)
 
override long Position [get, set]
 Current position within this stream.
 
- Properties inherited from NAudio.Wave.WaveStream
WaveFormat WaveFormat [get]
 Retrieves the WaveFormat for this stream.
 
override bool CanRead [get]
 We can read from this stream.
 
override bool CanSeek [get]
 We can seek within this stream.
 
override bool CanWrite [get]
 We can't write to this stream.
 
virtual int BlockAlign [get]
 The block alignment for this wavestream. Do not modify the Position to anything that is not a whole multiple of this value.
 
virtual TimeSpan CurrentTime [get, set]
 The current position in the stream in Time format.
 
virtual TimeSpan TotalTime [get]
 Total length in real-time of the stream (may be an estimate for compressed files)
 
- Properties inherited from NAudio.Wave.IWaveProvider

Events

EventHandler WaveFormatChanged
 WaveFormat has changed.
 

Private Member Functions

WaveFormat GetCurrentWaveFormat (IMFSourceReader reader)
 Retrieves the current wave format from the specified IMFSourceReader.
 
long GetLength (IMFSourceReader reader)
 Gets the length of the media source in bytes.
 
void EnsureBuffer (int bytesRequired)
 Ensures that the decoder output buffer has the required capacity.
 
int ReadFromDecoderBuffer (byte[] buffer, int offset, int needed)
 Reads bytes from the decoder output buffer into the provided buffer.
 
void Reposition (long desiredPosition)
 Repositions the media player to the desired position in time.
 
void OnWaveFormatChanged ()
 Raises the WaveFormatChanged event.
 

Static Private Member Functions

static MediaType GetCurrentMediaType (IMFSourceReader reader)
 Retrieves the current media type of the specified IMFSourceReader.
 

Private Attributes

WaveFormat waveFormat
 
long length
 
MediaFoundationReaderSettings settings
 
readonly string file
 
IMFSourceReader pReader
 
long position
 
byte[] decoderOutputBuffer
 
int decoderOutputOffset
 
int decoderOutputCount
 
long repositionTo = -1
 

Detailed Description

Class for reading any file that Media Foundation can play Will only work in Windows Vista and above Automatically converts to PCM If it is a video file with multiple audio streams, it will pick out the first audio stream.

Constructor & Destructor Documentation

◆ MediaFoundationReader() [1/3]

NAudio.Wave.MediaFoundationReader.MediaFoundationReader ( )
inlineprotected

Default constructor.

◆ MediaFoundationReader() [2/3]

NAudio.Wave.MediaFoundationReader.MediaFoundationReader ( string file)
inline

Creates a new MediaFoundationReader based on the supplied file.

Parameters
fileFilename (can also be a URL e.g. http:// mms:// file://)

◆ MediaFoundationReader() [3/3]

NAudio.Wave.MediaFoundationReader.MediaFoundationReader ( string file,
MediaFoundationReaderSettings settings )
inline

Creates a new MediaFoundationReader based on the supplied file.

Parameters
fileFilename
settingsAdvanced settings
Here is the call graph for this function:

Member Function Documentation

◆ CreateReader()

virtual IMFSourceReader NAudio.Wave.MediaFoundationReader.CreateReader ( MediaFoundationReaderSettings settings)
inlineprotectedvirtual

Creates a Media Foundation source reader with the specified settings.

Parameters
settingsThe settings for the Media Foundation reader.
Returns
The Media Foundation source reader created with the specified settings.

This method creates a Media Foundation source reader from the specified file URL and sets the stream selection for audio. It then creates a partial media type indicating the desired audio format and sets it as the current media type for the reader. If the specified media type is not supported, it may adjust the sample rate and channel count to accommodate certain audio formats.

Exceptions
COMExceptionThrown when the specified media type is not supported (MF_E_INVALIDMEDIATYPE).

Reimplemented in NAudio.Wave.StreamMediaFoundationReader.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Dispose()

override void NAudio.Wave.MediaFoundationReader.Dispose ( bool disposing)
inlineprotected

Releases the unmanaged resources used by the ClassName and optionally releases the managed resources.

Parameters
disposingTrue to release both managed and unmanaged resources; false to release only unmanaged resources.

This method releases the unmanaged resources used by the ClassName. If the disposing parameter is true, this method also releases all managed resources that this ClassName holds.

◆ EnsureBuffer()

void NAudio.Wave.MediaFoundationReader.EnsureBuffer ( int bytesRequired)
inlineprivate

Ensures that the decoder output buffer has the required capacity.

Parameters
bytesRequiredThe number of bytes required for the buffer.

This method checks if the decoder output buffer is null or has insufficient capacity to hold the required number of bytes. If the buffer is null or smaller than the required size, a new buffer with the required capacity is created.

Here is the caller graph for this function:

◆ GetCurrentMediaType()

static MediaType NAudio.Wave.MediaFoundationReader.GetCurrentMediaType ( IMFSourceReader reader)
inlinestaticprivate

Retrieves the current media type of the specified IMFSourceReader.

Parameters
readerThe IMFSourceReader from which to retrieve the current media type.
Returns
A new MediaType object representing the current media type of the IMFSourceReader.

This method retrieves the current media type of the specified IMFSourceReader for the first audio stream. It then creates a new MediaType object based on the retrieved media type and returns it.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetCurrentWaveFormat()

WaveFormat NAudio.Wave.MediaFoundationReader.GetCurrentWaveFormat ( IMFSourceReader reader)
inlineprivate

Retrieves the current wave format from the specified IMFSourceReader.

Parameters
readerThe IMFSourceReader from which to retrieve the wave format.
Returns
The WaveFormat corresponding to the current audio stream's media type.
Exceptions
InvalidDataExceptionThrown when the audio sub type is not supported.

This method retrieves the current media type of the first audio stream from the specified IMFSourceReader and extracts relevant information such as the audio sub type, number of channels, bits per sample, and sample rate. It then determines the appropriate WaveFormat based on the audio sub type and returns it. If the audio sub type is not supported, an InvalidDataException is thrown with a descriptive message indicating the unsupported sub type.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetLength()

long NAudio.Wave.MediaFoundationReader.GetLength ( IMFSourceReader reader)
inlineprivate

Gets the length of the media source in bytes.

Parameters
readerThe IMFSourceReader used to read the media source.
Returns
The length of the media source in bytes.

This method retrieves the duration of the media source using the GetPresentationAttribute method of the IMFSourceReader interface. If the duration attribute is not found, it returns 0, indicating that the media source does not support providing its duration (e.g., streaming media). If an error occurs during the retrieval of the duration attribute, an exception is thrown using Marshal.ThrowExceptionForHR. The method then calculates the length in bytes based on the retrieved duration and the average bytes per second of the wave format.

Exceptions
System.Runtime.InteropServices.COMExceptionThrown when an error occurs during the retrieval of the duration attribute.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Init()

void NAudio.Wave.MediaFoundationReader.Init ( MediaFoundationReaderSettings initialSettings)
inlineprotected

Initializes the MediaFoundationReader with the specified settings.

Parameters
initialSettingsThe initial settings for the MediaFoundationReader.

This method initializes the MediaFoundation API, sets the specified initial settings, creates a reader based on the settings, retrieves the wave format, sets the stream selection for audio, and gets the length of the reader. If the initialSettings is null, default settings are used. If the SingleReaderObject property is set to true in the settings, the reader object is retained; otherwise, it is released.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnWaveFormatChanged()

void NAudio.Wave.MediaFoundationReader.OnWaveFormatChanged ( )
inlineprivate

Raises the WaveFormatChanged event.

This method raises the WaveFormatChanged event, indicating that the wave format has been changed.

Here is the caller graph for this function:

◆ Read()

override int NAudio.Wave.MediaFoundationReader.Read ( byte[] buffer,
int offset,
int count )
inline

Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

Parameters
bufferAn array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.
offsetThe zero-based byte offset in buffer at which to begin storing the data read from the current stream.
countThe maximum number of bytes to be read from the current stream.
Returns
The total number of bytes written into buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero if the end of the stream is reached before any bytes are read.

This method reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. If the end of the stream is reached before any bytes are read, it returns zero. The method will continue to read until count bytes have been read or the end of the stream is reached. This method may block until at least one byte is available or end of the stream is reached.

Exceptions
InvalidOperationExceptionThrown when there is a Media Foundation read error.

Implements NAudio.Wave.IWaveProvider.

Here is the call graph for this function:

◆ ReadFromDecoderBuffer()

int NAudio.Wave.MediaFoundationReader.ReadFromDecoderBuffer ( byte[] buffer,
int offset,
int needed )
inlineprivate

Reads bytes from the decoder output buffer into the provided buffer.

Parameters
bufferThe destination buffer to copy the bytes into.
offsetThe zero-based byte offset in buffer at which to begin copying bytes.
neededThe number of bytes needed to be read from the decoder output buffer.
Returns
The actual number of bytes read from the decoder output buffer, which is the minimum of needed and the available bytes in the decoder output buffer.
Here is the caller graph for this function:

◆ Reposition()

void NAudio.Wave.MediaFoundationReader.Reposition ( long desiredPosition)
inlineprivate

Repositions the media player to the desired position in time.

Parameters
desiredPositionThe desired position in time to reposition the media player to.
Exceptions
System.Runtime.InteropServices.COMExceptionThrown when there is a failure in setting the current position in the media player.

This method calculates the position in 100 nanosecond units based on the desired position and the average bytes per second of the wave format. It then creates a PropVariant from the calculated position and sets the current position in the media player using the PropVariant. After repositioning, it resets the decoder output count and offset, updates the position, and clears the reposition flag.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ decoderOutputBuffer

byte [] NAudio.Wave.MediaFoundationReader.decoderOutputBuffer
private

◆ decoderOutputCount

int NAudio.Wave.MediaFoundationReader.decoderOutputCount
private

◆ decoderOutputOffset

int NAudio.Wave.MediaFoundationReader.decoderOutputOffset
private

◆ file

readonly string NAudio.Wave.MediaFoundationReader.file
private

◆ length

long NAudio.Wave.MediaFoundationReader.length
private

◆ position

long NAudio.Wave.MediaFoundationReader.position
private

◆ pReader

IMFSourceReader NAudio.Wave.MediaFoundationReader.pReader
private

◆ repositionTo

long NAudio.Wave.MediaFoundationReader.repositionTo = -1
private

◆ settings

MediaFoundationReaderSettings NAudio.Wave.MediaFoundationReader.settings
private

◆ waveFormat

WaveFormat NAudio.Wave.MediaFoundationReader.waveFormat
private

Property Documentation

◆ Length

override long NAudio.Wave.MediaFoundationReader.Length
get

The bytesRequired of this stream in bytes (n.b may not be accurate)

◆ Position

override long NAudio.Wave.MediaFoundationReader.Position
getset

Current position within this stream.

◆ WaveFormat

override WaveFormat NAudio.Wave.MediaFoundationReader.WaveFormat
get

WaveFormat of this stream (n.b. this is after converting to PCM)

Implements NAudio.Wave.IWaveProvider.

Event Documentation

◆ WaveFormatChanged

EventHandler NAudio.Wave.MediaFoundationReader.WaveFormatChanged

WaveFormat has changed.


The documentation for this class was generated from the following file: