Skip to content

Instantly share code, notes, and snippets.

@diiq
Created June 10, 2017 02:16
Show Gist options
  • Save diiq/4f8f67069bc79574c5ac7523e06acde2 to your computer and use it in GitHub Desktop.
Save diiq/4f8f67069bc79574c5ac7523e06acde2 to your computer and use it in GitHub Desktop.
Intubator (an old haxxy Angular 1 snippet)
class Player
constructor: (@gplayer, @nextId) ->
@top = true
@vol = 30
load: (id) =>
@top = false
@gplayer.loadVideoById
videoId: @nextId()
@gplayer.mute()
@gplayer.pauseVideo()
skip: () ->
@gplayer.playVideo()
setTimeout(=>
@duration = @gplayer.getDuration()
if @duration > 12
@gplayer.seekTo @duration * .25
else if @duration == 0
@load @nextId()
@skip()
else
@gplayer.seekTo 0
@gplayer.pauseVideo()
, 5000)
play: ->
@gplayer.unMute()
@volume()
@gplayer.playVideo()
@top = true
volume: (v) ->
if v?
@vol = v
@gplayer.setVolume(@vol)
console.log @vol
toggle: ->
if @top
@load @nextId
@skip()
0
else
@play()
@duration * 1000
angular.module('diiq.art')
.controller 'IntubatorCtrl', (YoutubeService, $timeout) ->
@volume = 30
i = 0
@volSettings =
from: 100,
to: 1,
step: -5,
vertical: true
css: {}
limits: false
value: false
callback: (value) =>
@player1.volume value
@player2.volume value
nextId = =>
id = @list.result.items[i].id.videoId
i += 1
if i >= @list.result.items.length
i = 0
YoutubeService.randomVids().then (@list) =>
id
firstVid = =>
@player1.load()
@player2.load()
@player2.skip()
@player1.play()
nextVid = =>
a = @player1.toggle()
b = @player2.toggle()
# Some foolishness to compensate for videos shorter than 10s.
duration = a || b
nextTime = 10000
if duration
nextTime = Math.max(Math.min(duration - 500, 10000), 5010)
$timeout(nextVid, nextTime)
@toggleMute = =>
console.log @volume
if @volume > 0
@volume = 0
else
@volume = 30
@player1.volume @volume
@player2.volume @volume
YoutubeService.loadYoutube().then =>
YoutubeService.randomVids().then (@list) =>
YoutubeService.makeIframe("tube1").then (gplayer) =>
@player1 = new Player(gplayer, nextId)
YoutubeService.makeIframe("tube2").then (gplayer) =>
@player2 = new Player(gplayer, nextId)
firstVid()
$timeout(nextVid, 6000)
return this
window.gapiCallbacks = []
window.iframeCallbacks = []
window.onGAPILoad = ->
_.each window.gapiCallbacks, (callback) ->
callback()
onYouTubeIframeAPIReady = ->
_.each window.iframeCallbacks, (callback) ->
callback()
angular.module('diiq.art')
.service 'YoutubeService', ($q) ->
pad = (num, l) ->
padded = num + ''
while(padded.length < l)
padded = "0" + padded
padded
randCount = ->
pad(Math.floor(Math.random() * 9999), 4)
@randomVids = ->
$q (resolve, reject) ->
gapi.client.youtube.search.list(
q: 'dscf' + randCount()
part:'id'
maxResults: 50
order: 'date'
type: 'video'
videoEmbeddable: 'true'
).then (res) ->
resolve(res)
loadGapi = ->
$q (resolve, reject) ->
if window.gapi.client
resolve()
else
window.gapiCallbacks.push(resolve)
@loadYoutube = ->
$q (resolve, reject) =>
loadGapi().then ->
gapi.client.setApiKey('[YOUR API KEY GOES HERE]');
gapi.client.load "youtube", "v3", (a, b) ->
resolve()
loadIframe = ->
$q (resolve, reject) ->
if window.YT.Player
resolve()
else
window.iframeCallbacks.push(resolve)
@makeIframe = (selector) ->
$q (resolve, reject) =>
loadIframe().then ->
console.log "ok"
it = new YT.Player selector,
events:
onReady: -> resolve(it)
this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment