npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

media-progress-timer

v3.0.0

Published

Accurate progress timer for web based media control

Downloads

23

Readme

Media progress timer

This library provides a helper designed for accurate tracking of remote playback position in media applications.

The typical naive approach I've come across simply uses setTimeout without a reference time and quickly drifts out of sync. To avoid this the library:

  • Always uses a reference time to avoid drifting.
  • Calls a user provided callback with the current position and duration.
  • Supports missing durations to make sure radio streams still count play time.
  • Uses requestAnimationFrame for performance / battery friendly updates.
  • Supports a legacy setTimeout based fallback mode.

Obviously there is still a chance that the remote side and the web application drift apart. With these measures polling code that resynchronizes can be run a lot less frequently, possibly even removed.

Install

There are no fancy build commands or minified versions. Just use the source file as it is or your module loader of choice.

There is also an NPM package at https://www.npmjs.com/package/media-progress-timer

Usage

var timer = ProgressTimer({
    // Your callback for updating UI state, required.
    callback: function(position, duration) {
        /* Your code here */
    },
    // Target frame rate when using legacy setTimeout fallback, default: 30.
    fallbackTargetFrameRate: 30,
    // Force legacy setTimeout fallback for testing, default: false.
    disableRequestAnimationFrame: false
});

// When playback starts set the timer and start it:
timer.set(0, 60000).start();

// When playback pauses, stop the timer:
timer.stop();

// When playback resumes, start the timer:
timer.start();

// When a seek event occours update the position:
timer.set(position)

// When playback stops, reset the timer.
timer.reset();
  • Just running new ProgressTimer(callback) is also possible if you don't want to set any options.
  • All function return this and are thus chainable.
  • The initial state of the timer is a position of zero and an infinite duration.
  • For the set call you may omit duration, in this case the timer will continue using the previous duration.
  • Positions will always be normalized to a number between zero and duration.
  • Durations will be normalized to a number between zero and infinite, null is considered an alias for infinite.

Background

This helper was created in order to help Mopidy client developers handle time better as too many of the clients have been constantly polling for the current time position to "fix" the drifting.

There is nothing Mopidy, or even media specific about this code so if it comes in handy for some other use cases then awesome :-)

Changelog

  • 3.0.0 (2016-03-25)
    • Removed updateRate support from RAF mode and fallback mode.
    • Added fallbackTargetFrameRate setting to the setTimeout mode.
    • Use UMD loader pattern.
    • Use clearTimeout and cancelAnimationFrame to stop timer.
    • Stop triggering callback in set when timer is running. This avoids some unintended behaviours and makes the timer more well behaved.

See https://github.com/adamcik/media-progress-timer/releases for previous releases.