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...
|
| 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.
|
|
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.
|
|
|
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.
|
|
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)
|
|
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.
◆ MediaFoundationReader() [1/3]
NAudio.Wave.MediaFoundationReader.MediaFoundationReader |
( |
| ) |
|
|
inlineprotected |
◆ MediaFoundationReader() [2/3]
NAudio.Wave.MediaFoundationReader.MediaFoundationReader |
( |
string | file | ) |
|
|
inline |
Creates a new MediaFoundationReader based on the supplied file.
- Parameters
-
◆ MediaFoundationReader() [3/3]
Creates a new MediaFoundationReader based on the supplied file.
- Parameters
-
file | Filename |
settings | Advanced settings |
◆ CreateReader()
Creates a Media Foundation source reader with the specified settings.
- Parameters
-
settings | The 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
-
COMException | Thrown when the specified media type is not supported (MF_E_INVALIDMEDIATYPE). |
Reimplemented in NAudio.Wave.StreamMediaFoundationReader.
◆ 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
-
disposing | True 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
-
bytesRequired | The 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.
◆ GetCurrentMediaType()
Retrieves the current media type of the specified IMFSourceReader.
- Parameters
-
reader | The 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.
◆ GetCurrentWaveFormat()
Retrieves the current wave format from the specified IMFSourceReader.
- Parameters
-
reader | The IMFSourceReader from which to retrieve the wave format. |
- Returns
- The WaveFormat corresponding to the current audio stream's media type.
- Exceptions
-
InvalidDataException | Thrown 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.
◆ GetLength()
Gets the length of the media source in bytes.
- Parameters
-
reader | The 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.COMException | Thrown when an error occurs during the retrieval of the duration attribute. |
◆ Init()
Initializes the MediaFoundationReader with the specified settings.
- Parameters
-
initialSettings | The 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.
◆ 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.
◆ 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
-
buffer | An 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. |
offset | The zero-based byte offset in buffer at which to begin storing the data read from the current stream. |
count | The 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
-
InvalidOperationException | Thrown when there is a Media Foundation read error. |
Implements NAudio.Wave.IWaveProvider.
◆ 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
-
buffer | The destination buffer to copy the bytes into. |
offset | The zero-based byte offset in buffer at which to begin copying bytes. |
needed | The 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.
◆ Reposition()
void NAudio.Wave.MediaFoundationReader.Reposition |
( |
long | desiredPosition | ) |
|
|
inlineprivate |
Repositions the media player to the desired position in time.
- Parameters
-
desiredPosition | The desired position in time to reposition the media player to. |
- Exceptions
-
System.Runtime.InteropServices.COMException | Thrown 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.
◆ 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
◆ repositionTo
long NAudio.Wave.MediaFoundationReader.repositionTo = -1 |
|
private |
◆ settings
◆ waveFormat
WaveFormat NAudio.Wave.MediaFoundationReader.waveFormat |
|
private |
◆ 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 |
◆ WaveFormatChanged
EventHandler NAudio.Wave.MediaFoundationReader.WaveFormatChanged |
The documentation for this class was generated from the following file: