Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web player improvements #55

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 55 additions & 7 deletions internal/api/play.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,66 @@

<script>
var player = videojs("my_player");
player.hlsQualitySelector();
var qualities = player.qualityLevels();
var qualitySelector = player.hlsQualitySelector({displayCurrentQuality:true});
player.src("index.m3u8");
player.play();

// Logging
qualities.on('addqualitylevel', () => {
var q = qualities[qualities.length - 1]
console.log(`Stream loaded: ${q.height}: "${q.label}"`)
});
qualities.on('change', () => {
var q = qualities[qualities.selectedIndex]
console.log(`Stream selected: ${q.height}: "${q.label}"`);
});

// If a query arg is present, try to set the default player quality
const requestedQuality = parseInt(new URLSearchParams(window.location.search).get('quality'))
if(requestedQuality) {

var requestedQualityWasFound = false
qualities.on('addqualitylevel', () => {

// Don't do anything until actually load a stream that matches the requested quality
var q = qualities[qualities.length - 1]
if (q.height === requestedQuality) {
console.log(`Requested quality found; changing stream to: "${q.label}"`)
requestedQualityWasFound = true
}
if (!requestedQualityWasFound)
return

// Once we find the requested quality, set it. Note: Intentionally continuing to call this
// for all qualities that are loaded after the one that was requested - doesn't work otherwise!
qualitySelector.setQuality(requestedQuality);
})
}

// Play on manual button click
var hasFirstPlayed = false
document.getElementById("my_player").addEventListener("click", function(){
player.play();
hasFirstPlayed = true
player.play();
}, {once: true});

// try to autoplay after 500ms
setTimeout(function() {
player.play();
}, 500)
// Try to autoplay once the player is ready
// No guarantee it will work: https://videojs.com/blog/autoplay-best-practices-with-video-js/
// Note: Using 'canplay' event instead of 'ready' as it fires *after* all the streams are loaded;
// hasFirstPlayed check is because canplay can fire multiple times (i.e. when manually switching streams)
player.on('canplay', function(){
if(!hasFirstPlayed){
hasFirstPlayed = true

console.log("Trying to autoplay...")
var promise = player.play();
promise.then(function() {
console.log("Playback started")
}).catch(function(error) {
console.log("Couldn't autoplay, please play manually.", error)
});
}
})
</script>
</body>
</html>