Archive
Posts Tagged ‘MP3’
Fix: Allow rewind of Mp3MediaStreamSource back to start
2014/11/18
Leave a comment
My comment at:
https://github.com/loarabia/ManagedMediaHelpers/issues/16
While trying to use Mp3MediaSource at ClipFlair Studio’s AudioRecorder control (http://clipflair.codeplex.com), I noticed that when I was doing Stop() at MediaElement and then Play() it kept on playing from where it was before at Mp3MediaStreamSource
So I did the following fix:
1) added these fields
/// <summary>
/// The first known frame (for rewind)
/// </summary>
private MpegFrame firstFrame;
/// <summary>
/// The first audio stream position (for rewind)
/// </summary>
private long firstAudioStreamPosition;
2) added to the end of ReadPastId3v2TagsCallback:
this.firstFrame = mpegLayer3Frame; //keeping the 1st frame position for rewinding
this.firstAudioStreamPosition = audioStream.Position;
3) changed SeekAsync:
/// <summary>
/// <para>
/// Only supporting seeking back to start of the stream
/// (e.g. when doing MediaElement.Stop()).
/// </para>
/// <para>
/// In a fuller MediaStreamSource, the logic here would be to actually seek to
/// the correct mpeg frame matching the seekToTime passed in.
/// </para>
/// </summary>
/// <param name="seekToTime">
/// The time to seek to (in 100-nanosecond units [hns])
/// </param>
protected override void SeekAsync(long seekToTime)
{
/*
if (seekToTime > this.trackDuration.TotalMilliseconds * 10)
{
throw new InvalidOperationException(
"The seek position is beyond the length of the stream");
}
*/
if (seekToTime != 0) //only supporting rewinding back to start
throw new InvalidOperationException(
"Only supporting seeking back to start of the stream");
else
{
this.currentFrame = firstFrame;
this.currentFrameStartPosition = MpegFrame.FrameHeaderSize;
this.audioStream.Position = firstAudioStreamPosition;
}
this.ReportSeekCompleted(seekToTime);
}
note that I changed the documentation for that method to say that time passed to it is in 100 ns units, not in ns units (according to WaveMediaStreamSource code that I found, unless that one has it wrong)
Categories: Posts
Audio, ClipFlair, Fix, MediaElement, MediaStreamSource, MP3, Silverlight