private long GetLength() { // http://msdn.microsoft.com/en-gb/library/windows/desktop/dd389281%28v=vs.85%29.aspx#getting_file_duration var variantValue = reader.GetPresentationAttribute(SourceReaderIndex.MediaSource, PresentationDescriptionAttributeKeys.Duration); var lengthInBytes = (variantValue * waveFormat.AverageBytesPerSecond) / 10000000L; return(lengthInBytes); }
private long GetLength(SourceReader reader) { lock (_lockObj) { try { if (reader == null) { return(0); } var value = reader.GetPresentationAttribute(SourceReaderIndex.MediaSource, MediaFoundationAttributes.MF_PD_DURATION); //bug: still, depending on the decoder, this returns imprecise values. return(NanoSecond100UnitsToBytes((long)(ulong)value.Value)); } catch (Exception) { return(0); } } }
private void Initialize(SourceReader reader) { // Invalidate selection for all streams reader.SetStreamSelection(SourceReaderIndex.AllStreams, false); // Select only audio stream reader.SetStreamSelection(SourceReaderIndex.FirstAudioStream, true); // Get the media type for the current stream. using (var mediaType = reader.GetNativeMediaType(SourceReaderIndex.FirstAudioStream, 0)) { var majorType = mediaType.Get(MediaTypeAttributeKeys.MajorType); if (majorType != MediaTypeGuids.Audio) throw new InvalidOperationException("Input stream doesn't contain an audio stream."); } // Set the type on the source reader to use PCM using (var partialType = new MediaType()) { partialType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Audio); partialType.Set(MediaTypeAttributeKeys.Subtype, AudioFormatGuids.Pcm); reader.SetCurrentMediaType(SourceReaderIndex.FirstAudioStream, partialType); } // Retrieve back the real media type using (var realMediaType = reader.GetCurrentMediaType(SourceReaderIndex.FirstAudioStream)) { int sizeRef; WaveFormat = realMediaType.ExtracttWaveFormat(out sizeRef); } Duration = new TimeSpan(reader.GetPresentationAttribute(SourceReaderIndex.MediaSource, PresentationDescriptionAttributeKeys.Duration)); }