Giter VIP home page Giter VIP logo

mediakit's Introduction

npm version size licence mediakit Stop media elements from playing all at once. Control them with a set of universal functions. Try it out.

๐Ÿ’ญ Problem

Many websites require embedded audio and video, either through HTML5 players or third party iFrames. However, managing these players can be difficult. Many audio players and iFrames can play at once, and each type of media has a different API, leading to an unpleasent experience for the user and developer.

โœจ Solution

mediakit aims to provide a simple interface for controlling all the media item on your page. Simply register the name and type of each item, and you'll have access to a universal control centre for your media. By default, mediakit ensures that only one item can play at a time.

๐Ÿ’พ Installation

๐Ÿ“ฆ Packager

Install with package manager:

npm i mediakitjs

Then import as needed.

import { create, play, pauseAll } from 'mediakitjs';

๐ŸŒ CDN

Load from CDN:

<script type="module" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mediakit.min.js</script>

Then grab mediakit from window.

const mk = window.mediakit;

๐Ÿš€ Usage

You need to give mediakit an object containing a query selector, type and name for each media item. type and selector are required. If no name is supplied, the selector string becomes the name (including #'s and .'s).


<audio id="audio1" src="resources/music.m4a" controls></audio>
<video id="video1" src="resources/video.mp4" controls></video>
<iframe id="vimeo1" src="{vimeo url}" width="640" height="360"></iframe>

mk.create([
    { type: 'audio', selector: '#audio1' },
    { type: 'video', selector: '#video1' },
    { type: 'vimeo', selector: '#vimeo1' },
])

๐Ÿ“บ YouTube Usage

For Vimeo, Video, and Audio, mediakit receives a reference to the existing iFrame or media element on the page. To include a YouTube video, you must pass a selector to an empty div or span that you want to be replaced with the YouTube iFrame. This is due to the design of the YouTube iFrame API, which requires us to register the iFrame on creation. Provide the videoId in the config property.

<div id="#youtube1"></div>

...

mk.create([
    { type: 'youtube', selector: '#youtube1', config: { videoId: '4eM12LJi_rg' } },
])

๐Ÿ“– Methods

play(name: string)

Play an element. If config.playExclusive === true, this method will pause all registered elements before playing the target element.

pause(name: string)

Pause an element.

stop(name: string)

Stop an element. Currently identical to pause(). In future versions, this method will pause the target element, then set its playback time to 0:00.

pauseAllExcept(name: string)

Pause all elements, except one.

pauseAll(name: string)

Pause all elements.

โš™๏ธ Configuration

create() accepts a second argument for configuration. The example below shows the default values of these properties.

mk.create(
    [
        {type: 'audio', selector: '#audio1'},
        {type: 'video', selector: '#video2'},
        {type: 'vimeo', selector: '#vimeo2'},
        {type: 'youtube', selector: '#youtube1', config: {videoId: '4eM12LJi_rg'}},
    ],
    {
        playExclusive: true,
        log: false,
    }
)

Supported

  • โœ… HTML5 Audio
  • โœ… HTML5 Video
  • โœ… YouTube
  • โœ… Vimeo

๐Ÿ›ฃ๏ธ Roadmap

  • Add support for seek()
  • Implement stop(). Current version mirrors pause()
  • Emit mediakit events. eg: mkPaused, mkPlayed
  • Add tests

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.