YouTube iFrame Controller

by mihau.co / repo: Github

// HTML
<iframe
  id="youtube-iframe"
  src="https://www.youtube-nocookie.com/embed/your_video_id?enablejsapi=1"
  title="YouTube video player"
  frameborder="0"
  allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
  referrerpolicy="strict-origin-when-cross-origin"
  allowfullscreen
>
</iframe>

// JavaScript
import { YouTubeIFrameCtrl } from './YouTubeIFrameCtrl.js';

const youTubeIFrame = document.getElementById('youtube-iframe')
const youTubeIFrameCtrl = new YouTubeIFrameCtrl(youTubeIFrame)
// select command to see example code
// you can use bult in method...
youTubeIFrameCtrl.play()

// ...or command method
youTubeIFrameCtrl.command('playVideo')
// you can use bult in method...
youTubeIFrameCtrl.pause()

// ...or command method
youTubeIFrameCtrl.command('pauseVideo')
// you can use bult in method...
youTubeIFrameCtrl.mute()

// ...or command method
youTubeIFrameCtrl.command('mute')
// you can use bult in method...
youTubeIFrameCtrl.unMute()

// ...or command method
youTubeIFrameCtrl.command('unMute')
// use command method with arguments array
// first element in array is time in seconds
youTubeIFrameCtrl.command('seekTo', [10])
// use command method with arguments array
// first element in array is volume level (0-100)
youTubeIFrameCtrl.command('setVolume', [10])