• Rewind and Fast Forward using the Smooth Streaming Media Element (SSME) Beta 2

    Published by on January 15th, 2010 7:16 am under Adaptive Streaming, IIS7, Live Smooth Streaming, Silverlight, Silverlight 3, Smooth Streaming, Smooth Streaming Media Element, Smooth Streaming Player Development Kit, SSME

    6 Comments

    Hey Folks! Time ago I blogged about how to add the Slow Motion capability to your Smooth Streaming player by using the Smooth Streaming Media Element published by the IIS team.

    Now it’s time to share with you how to provide the rewind and fast forward options to enable a real rich video experience.

    Again, adding support for these features is easy, and if you understood what we did for providing the Slow Motion experience, then the following will be a piece of cake.

    For rewind, the trick is getting all the playback rates under 0.0 and for fast forward, all the playback rates above 1.0.

    Show me the code!

    Rewind
    /// <summary>
    /// Toogles the PlaybackRate of the Smooth Streaming Media Element.
    /// Uses the PlaybackRates under 0.0 to support Rewind.
    /// </summary>
    public void OnRewind()
    {
        // PlaybackRate index to set on the SSME.
        int newPlaybackRateIndex = 0;
        // Verify the state of the SmoothPlayer
        if (this.SmoothPlayer.CurrentState == SmoothStreamingMediaElementState.Paused
            || this.SmoothPlayer.CurrentState == SmoothStreamingMediaElementState.Playing
            || this.SmoothPlayer.CurrentState == SmoothStreamingMediaElementState.Buffering)
        {
            // Get the current PlaybackRate of the SSME
            double playbackRate = this.PlaybackRate;
            // The first time we get the Rewind PlaybackRates from
            // the SSME supported PlayBackRates. Rewind PlaybackRates 
            // are under 0.0
            if (this.rewindPlaybackRates == null)
            {
                this.rewindPlaybackRates = new List<double>();
                IList<double> supportedPlaybackRates = this.SmoothPlayer.SupportedPlaybackRates;
                for (int i = 0; i < supportedPlaybackRates.Count - 1; i++)
                {
                    if (supportedPlaybackRates[i] < 0.0)
                    {
                        this.rewindPlaybackRates.Add(supportedPlaybackRates[i]);
                    }
                }
                this.rewindPlaybackRates.Add(1.0);
            }
            // Verify if the current PlaybackRate is within the 
            // allowables values. Get the index of the new 
            // PlaybackRate to set.
            if (playbackRate >= 0.0)
            {
                newPlaybackRateIndex = 0;
            }
            else
            {
                for (int i = 0; i < this.rewindPlaybackRates.Count - 1; i++)
                {
                    if (this.rewindPlaybackRates[i] == playbackRate)
                    {
                        newPlaybackRateIndex = (i + 1) % this.rewindPlaybackRates.Count;
                        break;
                    }
                }
            }
            this.SmoothPlayer.SetPlaybackRate(this.rewindPlaybackRates[newPlaybackRateIndex]);
        }
    }
    Fast Forward
    /// <summary>
    /// Toogles the PlaybackRate of the Smooth Streaming Media Element.
    /// Uses the PlaybackRates greather than 1.0 to support Fast forward.
    /// </summary>
    public void OnFastForward()
    {
        // PlaybackRate index to set on the SSME.
        int newPlaybackRateIndex = 0;
        // Verify the state of the SmoothPlayer
        if (this.SmoothPlayer.CurrentState == SmoothStreamingMediaElementState.Paused
            || this.SmoothPlayer.CurrentState == SmoothStreamingMediaElementState.Playing
            || this.SmoothPlayer.CurrentState == SmoothStreamingMediaElementState.Buffering)
        {
            // Get the current PlaybackRate of the SSME
            double playbackRate = this.PlaybackRate;
            // The first time we get the Fast Forward PlaybackRates from
            // the SSME supported PlayBackRates. Fast Forward PlaybackRates 
            // are above 1.0
            if (this.fastForwardPlaybackRates == null)
            {
                this.fastForwardPlaybackRates = new List<double>();
                IList<double> supportedPlaybackRates = this.SmoothPlayer.SupportedPlaybackRates;
                for (int i = 0; i < supportedPlaybackRates.Count - 1; i++)
                {
                    if (supportedPlaybackRates[i] > 1.0)
                    {
                        this.fastForwardPlaybackRates.Add(supportedPlaybackRates[i]);
                    }
                }
                this.fastForwardPlaybackRates.Add(1.0);
            }
            // Verify if the current PlaybackRate is within the 
            // allowables values. Get the index of the new 
            // PlaybackRate to set.
            if (playbackRate <= 1.0)
            {
                newPlaybackRateIndex = 0;
            }
            else
            {
                for (int i = 0; i < this.fastForwardPlaybackRates.Count - 1; i++)
                {
                    if (this.fastForwardPlaybackRates[i] == playbackRate)
                    {
                        newPlaybackRateIndex = (i + 1) % this.fastForwardPlaybackRates.Count;
                        break;
                    }
                }
            }
            this.SmoothPlayer.SetPlaybackRate(this.fastForwardPlaybackRates[newPlaybackRateIndex]);
        }
    }

    For more information about the SSME Beta 2 release, check Vishal’s post : IIS Smooth Streaming Player Development Kit Beta 2 released

    Enjoy it!
    kick it on DotNetKicks.com
    Shout it

    Tags: , , , , , , , ,

    • http://altgeek.org/ StephanJade

      Great article as for me. I’d like to read a bit more concerning that matter. Thanx for sharing this info.

    • John Mills

      Thank you for this post. Your blog has been very helpful as I try to implement the SmoothStreaming Media Element in my own application.

      While Fast Forward and Rewind are nice, I would like to be able to jump to a specific position in the timeline. With the old media element, I could set the position like this:

      MyMedia.Position = TimeSpan.FromTicks((long)JumpTo);

      JumpTo is a Double that is a percentage value of the duration of the track.

      For some reason, this has no effect when working with a Smooth Streaming file. Is this a bug? or is there a reason you can’t jump to a point in a smooth streaming file?

      Thanks

    • Salina Cheung

      SupportedPlaybackRates has only 4x and 8x, when I tried to add 2x, i got exception saying “collection has fixed size”.
      how do I add new rates to the collection?



Map

Categories