Last active
July 19, 2016 10:16
-
-
Save kuu/1fc59b93e0561d1d59903e1b7f30a9e2 to your computer and use it in GitHub Desktop.
Demonstrates a fixed bitrate in SmartTV environment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OO.ready(() => { | |
const player = OO.Player.create('container', '{embed_code}', playerParam); | |
if (OO.isSmartTV) { | |
player.mb.subscribe(OO.EVENTS.BITRATE_CHANGED, 'example', () => { | |
setHighestQuality(player); | |
}); | |
} else { | |
player.mb.subscribe(OO.EVENTS.BITRATE_CHANGED, 'example', () => { | |
avoidHighestQuality(player); | |
}); | |
} | |
}); | |
// Returns a bitrate object with top {n}-th quality (1-base) | |
function getTopN(player, n) { | |
const sorted = player.getBitratesAvailable().sort((a, b) => { | |
if (a.width < b.width && a.height < b.height) { | |
return -1; | |
} else if (a.width > b.width && a.height > b.height) { | |
return 1; | |
} | |
return 0; | |
}); | |
const index = sorted.length - n; | |
return sorted[index < 0 ? 0 : index]; | |
} | |
function setHighestQuality(player) { | |
const highest = getTopN(player, 1); | |
if (highest.id && highest.id !== player.getCurrentBitrate().id) { | |
player.setTargetBitrate(highest.id); | |
} | |
} | |
function avoidHighestQuality(player) { | |
const highest = getTopN(player, 1); | |
if (highest.id && highest.id === player.getCurrentBitrate().id) { | |
const second = getTopN(player, 2); | |
player.setTargetBitrate(second.id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment