UNPKG

youtubemanager2

Version:

YouTube player which mimics SoundManager2 Sound object

368 lines (330 loc) 9.76 kB
// Generated by CoffeeScript 1.7.1 /* youtubeManager 2013 (c) Andrey Popp <8mayday@gmail.com> */ var YoutubeSound, youtubeManager; YoutubeSound = (function() { var extractIdRe; extractIdRe = /v=([^&]+)/; function YoutubeSound(options) { var videoId; this.options = options; this.buffered = void 0; this.bytesLoaded = void 0; this.bytesTotal = 1; this.isBuffering = void 0; this.connected = void 0; this.duration = void 0; this.durationEstimate = void 0; this.isHTML5 = false; this.loaded = false; this.muted = false; this.paused = false; this.playState = 0; this.position = 0; this.readyState = 0; this.element = document.createElement('div'); this.element.setAttribute('id', options.id); this.container = options.container || document.body; this.container.appendChild(this.element); this._autoPlay = this.options.autoPlay; this._whileplaying = void 0; this._whileloading = void 0; this._previousState = void 0; this._onLoadFired = false; this._onPositionItems = []; videoId = options.youtubeVideoId || extractIdRe.exec(options.url)[1]; if (videoId == null) { throw new Error("cannot extract videoId from URL: " + options.url); } this.videoId = videoId; this.player = new YT.Player(options.id, { height: options.height, width: options.width, videoId: videoId, events: { onError: (function(_this) { return function() { return _this.onError(); }; })(this), onReady: (function(_this) { return function() { return _this.onReady(); }; })(this), onStateChange: (function(_this) { return function() { return _this.onStateChange(); }; })(this) }, playerVars: { controls: '0', enablejsapi: '1', modestbranding: '1', showinfo: '0', playerapiid: options.id } }); } YoutubeSound.prototype.onReady = function() { this.duration = this.durationEstimate = this.player.getDuration() * 1000; if (this._autoPlay) { this.play(); } if (this._autoPosition) { this.setPosition(this._autoPosition); } if (this.options.volume != null) { this.setVolume(this.options.volume); } this._startLoadingPoller(); return this.readyState = 1; }; YoutubeSound.prototype.onStateChange = function() { var state; state = this.player.getPlayerState(); if (state === -1) { this.duration = this.durationEstimate = this.player.getDuration() * 1000; this.playState = 0; } if (state === YT.PlayerState.PLAYING) { this.duration = this.durationEstimate = this.player.getDuration() * 1000; this.playState = 1; this._startPlayingPoller(); this.paused = false; if (this._previousState === YT.PlayerState.PAUSED) { if (this.options.onresume) { this.options.onresume.apply(this); } } else { if (this.options.onplay) { this.options.onplay.apply(this); } } } else if (state === YT.PlayerState.PAUSED) { this._stopPlayingPoller(); this.paused = true; if (this.options.onpause) { this.options.onpause.apply(this); } } else if (state === YT.PlayerState.ENDED) { this.paused = false; this.playState = 0; this._stopPlayingPoller(); if (this.options.onfinish) { this.options.onfinish.apply(this); } } return this._previousState = state; }; YoutubeSound.prototype.onError = function() { this.readyState = 2; this.loaded = false; this.stop(); if (this.options.onload) { return this.options.onload.apply(this, [this.loaded]); } }; YoutubeSound.prototype._startPlayingPoller = function() { return this._whileplaying = setInterval(((function(_this) { return function() { return _this._updatePosition(); }; })(this)), this.options.pollingInterval || 500); }; YoutubeSound.prototype._stopPlayingPoller = function() { if (!this._whileplaying) { return; } clearInterval(this._whileplaying); return this._whileplaying = void 0; }; YoutubeSound.prototype._updatePosition = function() { var item, _i, _len, _ref, _results; this.position = this.player.getCurrentTime() * 1000; if (this.options.whileplaying) { this.options.whileplaying.apply(this); } _ref = this._onPositionItems; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { item = _ref[_i]; if (!(!item.fired && this.position >= item.position)) { continue; } item.fired = true; if (item.method) { _results.push(item.method.apply(this, [item.position])); } else { _results.push(void 0); } } return _results; }; YoutubeSound.prototype._startLoadingPoller = function() { return this._whileloading = setInterval(((function(_this) { return function() { return _this._updateLoading(); }; })(this)), this.options.pollingInterval || 500); }; YoutubeSound.prototype._stopLoadingPoller = function() { if (!this._whileloading) { return; } clearInterval(this._whileloading); return this._whileloading = void 0; }; YoutubeSound.prototype._updateLoading = function() { this.bytesLoaded = this.player.getVideoLoadedFraction(); if (this.bytesLoaded > 0) { this.readyState = 3; this.loaded = true; if (this.options.onload && !this._onLoadFired) { this.options.onload.apply(this, [this.loaded]); } this._onLoadFired = true; } if (this.bytesLoaded === 1) { this._stopLoadingPoller(); } if (this.options.whileloading) { return this.options.whileloading.apply(this); } }; YoutubeSound.prototype.destruct = function() { this.player.destroy(); this._stopPlayingPoller(); return this._stopLoadingPoller(); }; YoutubeSound.prototype.load = function(url) { this.videoId = extractIdRe.exec(url)[1]; return this.player.cueVideoById(this.videoId); }; YoutubeSound.prototype.clearOnPosition = function(ms, method) { var index, value, _ref, _results; ms = parseInt(ms, 10); if (isNaN(ms)) { return false; } _ref = this._onPositionItems; _results = []; for (index in _ref) { value = _ref[index]; if (value.position === ms && value.method === method) { _results.push(this._onPositionItems.splice(index, 1)); } } return _results; }; YoutubeSound.prototype.onPosition = function(ms, method) { return this._onPositionItems.push({ position: parseInt(ms, 10), method: method, fired: false }); }; YoutubeSound.prototype.mute = function() { this.muted = true; return this.player.mute(); }; YoutubeSound.prototype.pause = function() { if (this.player.pauseVideo != null) { return this.player.pauseVideo(); } else { return this._autoPlay = false; } }; YoutubeSound.prototype.play = function() { if (this.player.playVideo != null) { return this.player.playVideo(); } else { return this._autoPlay = true; } }; YoutubeSound.prototype.resume = function() { return this.play(); }; YoutubeSound.prototype.setPan = function() {}; YoutubeSound.prototype.setPosition = function(ms) { if (this.player.seekTo) { return this.player.seekTo(ms / 1000); } else { return this._autoPosition = ms; } }; YoutubeSound.prototype.setVolume = function(v) { return this.player.setVolume(v); }; YoutubeSound.prototype.stop = function() { if (this.player.stopVideo != null) { this.player.seekTo(0); this.player.stopVideo(); this._stopPlayingPoller(); this._stopLoadingPoller(); return this.position = 0; } else { return this._autoPlay = false; } }; YoutubeSound.prototype.toggleMute = function() { if (this.player.isMuted()) { return this.unmute(); } else { return this.mute(); } }; YoutubeSound.prototype.togglePause = function() { if (this.player.getPlayerState() === YT.PlayerState.PLAYING) { return this.pause(); } else { return this.play(); } }; YoutubeSound.prototype.unload = function() {}; YoutubeSound.prototype.unmute = function() { this.muted = false; return this.player.unMute(); }; return YoutubeSound; })(); youtubeManager = { createSound: function(options) { return new YoutubeSound(options); }, setup: function(options) { var oldCallback; if (options == null) { options = {}; } if (window.onYouTubeIframeAPIReady != null) { oldCallback = window.onYouTubeIframeAPIReady; } window.onYouTubeIframeAPIReady = function() { if (options.onready) { options.onready(); } if (oldCallback) { return oldCallback(); } }; return this._injectScript(); }, _injectScript: function() { var firstScriptTag, tag; tag = document.createElement('script'); tag.src = "https://www.youtube.com/player_api"; firstScriptTag = document.getElementsByTagName('script')[0]; return firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); } }; if ((typeof define !== "undefined" && define !== null ? define.amd : void 0) != null) { define(youtubeManager); } if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) { module.exports = youtubeManager; } window.youtubeManager = youtubeManager;