Giter Club home page Giter Club logo

embedplayer's Introduction

Embed Player

Unified jQuery interface to various audio/video players without dependency on their official JavaScript libraries. Currently supported players:

  • YouTube
  • Vimeo
  • Twitch
  • SoundCloud
  • Dailymotion
  • HMTL 5 audio/video

Internet Explorer <=8 is not supported.

Online Demo

Why?

So you have an unified interface for all of these players and you don't need to load the player APIs from the respective services (which is basically XSS).

Example

<iframe src="https://player.vimeo.com/video/1084537?api=1" id="embed"
	width="640" height="360" frameborder="0" allowfullscreen></iframe>
$('#embed').on('embedplayer:statechange', function (event) {
	console.log('state:', event.state);
}).on('embedplayer:error', function (event) {
	console.error('error:', event.error);
}).on('embedplayer:durationchange', function (event) {
	console.log('duration:', event.duration);
}).on('embedplayer:volumechange', function (event) {
	console.log('volume:', event.volume);
}).on('embedplayer:timeupdate', function (event) {
	console.log('currentTime:', event.currentTime);
}).on('embedplayer:ready', function (event) {
	console.log('link:', $(this).embedplayer('link'));
}).embedplayer('listen'); // enable all events

$('#embed').embedplayer('play');
$('#embed').embedplayer('seek',30);
$('#embed').embedplayer('volume',0.5);
$('#embed').embedplayer('pause');
$('#embed').embedplayer('stop');

Note: For Twitch players you need to pass the origin of the current host as an request parameter. E.g.:

$('<iframe>').
  attrs({src: 'https://player.twitch.tv/?allowfullscreen&video=v92780016&origin=' +
    encodeURIComponent(location.origin)}).
  appendTo(document.body).
  embedplayer('play');

YouTube also has this origin parameter, but it seems to be optional.

Fullscreen Support

Embed player doesn't provide an API for toggling a video to fullscreen because player APIs don't provide a method for that. You can use the HTML5 fullscreen API to implement this feature yourself, though:

<button onclick="$('#embed')[0].requestFullscreen();">Fullscreen</button>

NOTE: The players do provide their own fullscreen buttons. Just adding your own will get you a situation where you can enter fullscreen twice and get situations that are very confusing to users. If you don't set the allowfullscreen attribute on the iframe some players will still render a grayed out non-functional fullscreen button, which is still confusing. So maybe just let this be handeled by the players.

Bugs/TODO

If the iframe is not loaded when the embed player is initialized any initialization message sent to the iframe will be lost. As I see it, it's not possible to determine if an iframe is already loaded cross browser (Firefox does not implement iframe.readyState).

API Reference

Functions

Properties

Events

Functions

init()

Initializes the embed. All other functions do this implicitely as well.

Examples:

$('#embed').embedplayer('init');

or

$('#embed').embedplayer();

listen([events])

Enable certain events.

Examples:

$('#embed').embedplayer('listen', 'timeupdate error');

or:

$('#embed').embedplayer('listen', ['timeupdate', 'error']);

or to enable all events:

$('#embed').embedplayer('listen');

play()

Example:

$('#embed').embedplayer('play');

pause()

Example:

$('#embed').embedplayer('pause');

stop()

Not all players support this. If it is not supported it is the same as pause().

Example:

$('#embed').embedplayer('stop');

seek(time)

time is given in seconds.

Example:

$('#embed').embedplayer('seek', time);

next()

Play the next video from the playlist. Currently only supported for YouTube playlists.

Example:

$('#embed').embedplayer('next');

prev()

Play the previous video from the playlist. Currently only supported for YouTube playlists.

Example:

$('#embed').embedplayer('prev');

supported()

Example:

if (!$('#embed').embedplayer('supported')) {
	alert('Cannot control this embed!');
}

volume(value)

value is in the range of 0 to 1.

Example:

$('#embed').embedplayer('volume', value);

Properties

volume(callback)

callback is a function that takes the volume value as paramert. The volume is in the range of 0 to 1. The value might be NaN if the player is not yet initialized or for some players if it hasn't started playing.

Example:

$('#embed').embedplayer('volume', function (value) { console.log(value); });

currenttime(callback)

callback is a function that takes the current time value as paramert. The time is given in seconds. The value might be NaN if the player is not yet initialized or for some players if it hasn't started playing.

Example:

$('#embed').embedplayer('currenttime', function (value) { console.log(value); });

duration(callback)

callback is a function that takes the duration value as paramert. The duration is given in seconds. The value might be NaN if the player is not yet initialized or for some players if it hasn't started playing.

Example:

$('#embed').embedplayer('duration', function (value) { console.log(value); });

state

Possible states:

  • init
  • playing
  • paused
  • finished
  • buffering

Not all states are supported by all players.

Example:

console.log($('#embed').embedplayer('state'));

link

Link to a web page representing the video. Might be null if it can't be determined.

Example:

console.log($('#embed').embedplayer('link'));

Events

embeplayer:statechange

Event object properties:

embeplayer:ready

Player is ready to receive commands.

embeplayer:play

Started playing a media.

embeplayer:pause

Paused playback.

embeplayer:finish

End of medium is reached and playback stopped.

embeplayer:buffering

Waiting for data to arrive. Not every backend supports this event.

embeplayer:timeupdate

Current time changed. This can be used to display playback progress.

Event object properties:

  • currentTime Number in seconds

embeplayer:volumechange

The playback volume changed. This can be used to implement a volume widget.

Event object properties:

  • volume Number between 0 and 1

embeplayer:durationchange

This event is sent when the player loaded enough of the video to know it's duration.

Event object properties:

  • duration Number in seconds, for streams it might be Infinity

embeplayer:error

Event object properties:

  • error String possible values (might change, except for the first 4):
    • error
    • not_found
    • forbidden
    • illegal_parameter
    • informational
    • success
    • redirection
    • found
    • not_modified
    • client_error
    • internal_server_error
    • server_error
    • aborted
    • network_error
    • decoding_error
    • src_not_supported
  • message String (optional)
  • title String (optional)
  • statusCode String (optional) is a HTTP status code associated with the error

Note: The Vimeo backend currently only supports message and title and just uses the error code error for all kinds of errors. I need to find a list of Vimeo error names to make an appropriate mapping.

embedplayer's People

Contributors

panzi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

embedplayer's Issues

Twitch?

Hey just been trying it all out and it works great! Only issue I've found is the twitch videos the controls play/pause/volume etc.. don't work?

Youtube origin parameter breaks postMessage

Here https://developers.google.com/youtube/player_parameters#origin it says that origin should be present if enablejsapi is set.

I've tried to set my origin to the video url, but embedplayer silently stops working - events like timeupdate does not trigger.

I've tried to set origin from src to data.detail.origin naively, but then I get infinite errors

The target origin provided ('https://www.youtube.com') does not match the recipient window's origin (<my actual origin>)

Is it a problem in embedplayer or am I using it wrong?

Uncaught TypeError: Cannot read property 'listening' and 'state' of undefined

Hi,
I got this error when I playing html5 video or audio and without pause/stop then change video on select/dropdown.

The two variable undefined.

But if I pause or stop there's no issue. I tried to put this code below on the function but didn't work.

$('#holder').embedplayer('pause');

How can I rid of it so there's no error?

Thanks

Feature Request: Fullscreen support

I couldn't find any way to make the player fullscreen using your library or detect if the player is fullscreen. I think having that would be really nice. Also changing source of the video without having to escape out of fullscreen would be nice too for having playlists.

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.