vlitejs / vlite

🦋 vLitejs is a fast and lightweight Javascript library for customizing video and audio player in Javascript with a minimalist theme (HTML5, Youtube, Vimeo, Dailymotion)
https://vlite.js.org
MIT License
268 stars 18 forks source link

replace a youtube video by another one. #41

Closed Shawlee76 closed 3 years ago

Shawlee76 commented 3 years ago

Hi how can I load another youtube video to replace the current one which is being played ? I don't understand which methods I have to use in order to : 1- Stop or unload the video, 2- replace the youtube key by another 3- Play the new video.

Thanks by advance. Charlie

yoriiis commented 3 years ago

Hi, there are no methods for this, you can do it without, like this example:

<div id="player" class="vlite-js" data-youtube-id="C4qgAaxB_pc"></div>
<button id="button">Change video</button>
import 'vlitejs/dist/vlite.css';
import Vlitejs from 'vlitejs';
import VlitejsYoutube from 'vlitejs/dist/providers/youtube';

Vlitejs.registerProvider('youtube', VlitejsYoutube)

const vlite = new Vlitejs('#player', { provider: 'youtube' })

document.getElementById('button').addEventListener('click', (e) => {
    e.preventDefault()
    vlite.destroy()
    document
        .querySelector('body')
        .insertAdjacentHTML(
            'beforeend',
            '<div id="player" class="vlite-js" data-youtube-id="1VIZ89FEjYI"></div>'
        )
    const newVlite = new Vlitejs('#player', {
        provider: 'youtube',
        onReady: (player) => {
            player.play()
        }
    })})