Skip to content

Instantly share code, notes, and snippets.

@AngelAlvie
Created August 22, 2018 19:05
Show Gist options
  • Save AngelAlvie/2b6971fd80561f580d033e60f2ddbcb0 to your computer and use it in GitHub Desktop.
Save AngelAlvie/2b6971fd80561f580d033e60f2ddbcb0 to your computer and use it in GitHub Desktop.
Player.js - An Asynchronous Wrapper for the HTML5 Video Player
/** Asynchronous Player is a closure that is configurable with callbacks and signals.
*/
function AsyncPlayer(videoElement) {
let player = null;
const VIDEO_VOLUME = 50;
const VIDEO_LENGTH_THRESHOLD = 5;
let buffer_start_time_ms = 0;
let video_duration_sec = 0;
let video_duration_ms = 0;
let start_time = 0;
let playing = false;
let buffer_lock = false;
let has_ended = false;
let video_started = false;
const ignore = () => {};
return (message, data=null, cb = (message, data)=>{}) => {
//Handle Asynchronous messages
if (message === "load") {
// Do the asynchronous loading, and notify the cb when finished
player = document.getElementById(videoElement);
// Just verifying that the player controls are disabled
player.controls = false;
var req = new XMLHttpRequest();
req.open('GET', data, true);
req.responseType = 'blob';
req.onload = function() {
if (this.status === 200) {
var videoBlob = this.response;
var vid = URL.createObjectURL(videoBlob);
player.src = vid;
cb("loaded", null);
} else {
cb("error", null);
}
}
req.onerror = function() {
cb("error", null);
}
req.send();
} else if (message === "play") {
player
.play()
.then(() => {
video_duration_sec = player.duration;
cb("video start", {
video_duration_sec: video_duration_sec
});
});
player.onwaiting = (data) => {
if (!buffer_lock) {
buffer_start_time_ms = Date.now()
buffer_lock = true;
cb("buffer started", null);
}
};
player.onpause = () => {playing = false;};
player.onplaying = (data) => {
playing = true;
if (buffer_lock) {
buffer_lock = false;
cb("buffer finished", Date.now() - buffer_start_time_ms);
}
}
player.onended = (data) => {
player.onended = ignore;
player.onplaying = ignore;
player.onpause = ignore;
player.onwaiting = ignore;
cb("ended", null);
};
} else if (message === "seek") {
player.currentTime = data;
cb("seeked", null);
} else if (message === "pause"){
player.pause();
playing = false;
cb("paused", null);
} else if (message === "resume") {
player.play();
playing = true;
cb("playing", null);
}
// Handle synchronous messages
if (message === "getVideoDurationSec") {
return video_duration_sec;
} else if (message === "getVideoDurationMs") {
return video_duration_ms;
} else if (message === "getStartTime") {
return start_time;
} else if (message === "getPlayingState") {
return playing;
} else if (message === "getCurrentTime") {
return player.currentTime;
} else if (message === "setPlayingState") {
playing = data;
if (data) {
player.play();
} else {
player.pause();
}
}
};
}
@wiudhiuwe
Copy link

Luckyi8 Game is an online gaming hub with exciting mobile games offering fun and winning opportunities. It provides you with a hundred fish table games, keno, and slot machine games to keep you entertained.

@wiudhiuwe
Copy link

Lucky PKR is a thrilling and engaging game that really captures the spirit of chance and strategy. The blend of luck-based gameplay with skillful decision-making creates an exciting experience for players. It's especially fun for those who enjoy a mix of casual play and the potential for big rewards. The user interface is smooth and easy to navigate, making it accessible for both new players and experienced ones. The game also offers a fair chance at winning, with clear rules and transparent outcomes. Overall, Lucky PKR is a fun way to test your luck and skill while enjoying an interactive gaming experience. Highly recommended for those looking to try their hand at something exciting and rewarding!

@wiudhiuwe
Copy link

3 Patti Crown is an exciting and fast-paced card game that brings the thrill of traditional Indian Poker to your fingertips. With smooth gameplay, vibrant graphics, and easy-to-understand rules, it offers a fun experience for both casual and experienced players. Perfect for those who love a blend of strategy and luck on 3Patti Crown Game APK!

@wiudhiuwe
Copy link

"3 Patti Boss takes the classic card game to new heights with intense competition and exciting gameplay. It's more than just a game – it’s a battle of strategy, skill, and nerves. Whether you're a pro or just starting out, every hand feels like a chance to outsmart your opponents and claim the title of the ultimate 3 Patti Boss. The thrilling rush of each round keeps you hooked, and with the chance to win big, it’s the perfect game for those who love a challenge!"

@broadstar-g
Copy link

Lucky 101 is an exhilarating online gaming hub that brings a fresh twist to entertainment, offering an array of exciting games like Crash and Baccarat tailored for players in Pakistan. With its sleek design and intuitive navigation, the Lucky101 Game APK guarantees a smooth and immersive experience. Start with minimal deposits and enjoy swift, secure withdrawals via JazzCash or Easypaisa, ensuring hassle-free transactions. Exclusive daily bonuses and referral rewards amplify the fun, making every game a shot at big wins. Download now for a one-of-a-kind gaming thrill!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment