|
| 1 | +var Player = function(ui) { |
| 2 | + var player = ui._context.createElement('div') |
| 3 | + |
| 4 | + player.dom.id = "yt-player" |
| 5 | + this.element = player |
| 6 | + this.ui = ui |
| 7 | + |
| 8 | + ui.element = player |
| 9 | + ui.parent.element.append(ui.element) |
| 10 | + |
| 11 | + var tag = document.createElement('script') |
| 12 | + tag.src = "https://www.youtube.com/iframe_api" |
| 13 | + tag.async = false |
| 14 | + var self = this |
| 15 | + this.iframeIsReady = false |
| 16 | + tag.onload = ui._context.wrapNativeCallback(function(res) { |
| 17 | + self.youTubeIframeAPIReady() |
| 18 | + }) |
| 19 | + |
| 20 | + var firstScriptTag = document.getElementsByTagName('script')[0] |
| 21 | + firstScriptTag.parentNode.insertBefore(tag, firstScriptTag) |
| 22 | +} |
| 23 | + |
| 24 | +Player.prototype.playerReady = function(event) { |
| 25 | + this.playerController = event.target |
| 26 | + this.ui.ready = true |
| 27 | + this.ui.waiting = true |
| 28 | +} |
| 29 | + |
| 30 | +Player.prototype.playerStateChange = function(event) { |
| 31 | + log("playerStateChange", event) |
| 32 | + switch (event.data) { |
| 33 | + case YT.PlayerState.PLAYING: |
| 34 | + this.ui.duration = event.target.getDuration() |
| 35 | + this.ui.waiting = false |
| 36 | + this.ui.paused = false |
| 37 | + break |
| 38 | + case YT.PlayerState.PAUSED: |
| 39 | + this.ui.paused = true |
| 40 | + break |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +Player.prototype.setupPlayer = function() { |
| 45 | + log("setupPlayer", this.source, "iframeIsReady", this.iframeIsReady) |
| 46 | + if (!this.source || !this.iframeIsReady) |
| 47 | + return |
| 48 | + |
| 49 | + var ui = this.ui |
| 50 | + this.player |
| 51 | + var self = this |
| 52 | + YT.ready(function() { |
| 53 | + self.player = new YT.Player("yt-player", { |
| 54 | + width: ui.width, |
| 55 | + height: ui.height, |
| 56 | + videoId: this.source, |
| 57 | + playerVars: { |
| 58 | + end: 0, autoplay: ui.autoPlay, loop: 0, controls: 0, showinfo: 0, modestbranding: 1, fs: 0, cc_load_policty: 0, iv_load_policy: 3, autohide: 0 |
| 59 | + }, |
| 60 | + events: { |
| 61 | + onReady: ui._context.wrapNativeCallback(self.playerReady).bind(self), |
| 62 | + onStateChange: ui._context.wrapNativeCallback(self.playerStateChange).bind(self) |
| 63 | + } |
| 64 | + }); |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +Player.prototype.youTubeIframeAPIReady = function() { |
| 69 | + this.iframeIsReady = true |
| 70 | + this.setupPlayer() |
| 71 | +} |
| 72 | + |
| 73 | +Player.prototype.dispose = function() { |
| 74 | + var ui = this.ui |
| 75 | + if (!ui) |
| 76 | + return |
| 77 | + |
| 78 | + var element = ui.element |
| 79 | + if (element) { |
| 80 | + element.remove() |
| 81 | + ui.element = null |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +Player.prototype.setSource = function(url) { |
| 86 | + this.ui.ready = false |
| 87 | + this.source = url |
| 88 | + this.setupPlayer() |
| 89 | +} |
| 90 | + |
| 91 | +Player.prototype.play = function() { |
| 92 | + if (!this.playerController) |
| 93 | + return |
| 94 | + this.playerController.playVideo(); |
| 95 | +} |
| 96 | + |
| 97 | +Player.prototype.pause = function() { |
| 98 | + this.playerController.pauseVideo(); |
| 99 | +} |
| 100 | + |
| 101 | +Player.prototype.stop = function() { |
| 102 | + this.playerController.pauseVideo(); |
| 103 | +} |
| 104 | + |
| 105 | +Player.prototype.seek = function(delta) { |
| 106 | +} |
| 107 | + |
| 108 | +Player.prototype.seekTo = function(tp) { |
| 109 | +} |
| 110 | + |
| 111 | +Player.prototype.setVolume = function(volume) { |
| 112 | +} |
| 113 | + |
| 114 | +Player.prototype.setMute = function(muted) { |
| 115 | +} |
| 116 | + |
| 117 | +Player.prototype.setLoop = function(loop) { |
| 118 | +} |
| 119 | + |
| 120 | +Player.prototype.setOption = function(name, value) { |
| 121 | +} |
| 122 | + |
| 123 | +Player.prototype.setRect = function(l, t, r, b) { |
| 124 | + //not needed in this port |
| 125 | +} |
| 126 | + |
| 127 | +Player.prototype.setVisibility = function(visible) { |
| 128 | +} |
| 129 | + |
| 130 | +Player.prototype.setupDrm = function(type, options, callback, error) { |
| 131 | + log('Not implemented') |
| 132 | +} |
| 133 | + |
| 134 | +Player.prototype.getSubtitles = function() { |
| 135 | + return [] |
| 136 | +} |
| 137 | + |
| 138 | +Player.prototype.getVideoTracks = function() { |
| 139 | + return [] |
| 140 | +} |
| 141 | + |
| 142 | +Player.prototype.getAudioTracks = function() { |
| 143 | + return [] |
| 144 | +} |
| 145 | + |
| 146 | +Player.prototype.setAudioTrack = function(trackId) { |
| 147 | +} |
| 148 | + |
| 149 | +Player.prototype.setVideoTrack = function(trackId) { |
| 150 | +} |
| 151 | + |
| 152 | +Player.prototype.setVideoTrack = function(trackId) { |
| 153 | +} |
| 154 | + |
| 155 | +Player.prototype.setBackgroundColor = function(color) { |
| 156 | +} |
| 157 | + |
| 158 | +exports.createPlayer = function(ui) { |
| 159 | + return new Player(ui) |
| 160 | +} |
| 161 | + |
| 162 | +exports.probeUrl = function(url) { |
| 163 | + return 10 |
| 164 | +} |
| 165 | + |
| 166 | +exports.Player = Player |
0 commit comments