Tip of the week # 205

Lightning Web Component with Youtube Controls.
Description : Code snapshot for Lightning Web Component with Youtube Controls.-
YoutubePlayback.html
-
YoutubePlayback.js
import { LightningElement } from 'lwc'; import { ShowToastEvent } from 'lightning/platformShowToastEvent'; export default class YoutubePlayback extends LightningElement { iframeElement = null; playerReady = false; volume = 100; playbackSpeed =1; playbackSpeedOptions = [ { label: '0.5x', value:0.5}, { label: '1x', value:1}, { label: '1.5x', value:1.5}, { label: '2x', value:2} ]; subtitlesEnabled = true; get selectedVideoUrl() { return this.selectedVideo='https://www.youtube.com/embed/NeXIV-wMVUk?autoplay=1&enablejsapi=1&controls=0&cc_load_policy=1&cc_lang_pref=en'; } renderedCallback() { this.iframeElement = this.template.querySelector('iframe'); if (this.iframeElement) { this.playerReady = true; } } sendCommand(command, args = []) { if (this.iframeElement && this.playerReady) { const message = JSON.stringify({ event: "command", func: command, args }); this.iframeElement.contentWindow.postMessage(message, "*"); } else { this.showToast('Error', 'Video player not ready', 'error'); } } handlePlay() { this.sendCommand("playVideo"); } handlePause() { this.sendCommand("pauseVideo"); } handleRestart() { this.sendCommand("seekTo", [0, true]); setTimeout(() => this.sendCommand("playVideo"), 500); } showTooltip() { this.isTooltipVisible = true; } hideTooltip() { this.isTooltipVisible = false; } showToast(title, message, variant) { this.dispatchEvent(new ShowToastEvent({ title, message, variant })); } handleVolumeChange(event) { this.volume = event.target.value; this.sendCommand("setVolume", [this.volume]); } handleSpeedChange(event) { this.playbackSpeed = Number(event.target.value); console.log(this.playbackSpeed); this.sendCommand("setPlaybackRate", [this.playbackSpeed]); } toggleSubtitles() { this.subtitlesEnabled = !this.subtitlesEnabled; this.sendCommand("setOption", ["captions", "track", this.subtitlesEnabled ? { languageCode: "en" } : {}]); } } 61.0 true lightning__homePage -
Output of Code
Happy Sharing...
Everyone has their own favorites, so please feel free to share with yours contacts and comments below for any type of help!Use below Chrome Extension for Productivity and share your feedback. Download Link : https://chromewebstore.google.com/detail/quick-shortcuts/ldodfklenhficdgllchmlcldbpeidifp?utm_source=ext_app_menu
Comments
Post a Comment