2020-01-10 03:02:46 +01:00
|
|
|
<div>
|
2020-01-10 15:16:48 +01:00
|
|
|
<div>
|
|
|
|
<div bind:this={element}></div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{#if duration}
|
|
|
|
{Math.round(currentTime)} / {Math.round(duration)}
|
|
|
|
<input type="range" min="0" max={duration} value={currentTime} disabled>
|
|
|
|
{/if}
|
|
|
|
|
2020-01-10 03:02:46 +01:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<script>
|
2020-01-10 15:16:48 +01:00
|
|
|
import { onMount, onDestroy } from 'svelte'
|
2020-01-10 03:02:46 +01:00
|
|
|
import { get } from 'svelte/store'
|
|
|
|
import YoutubePlayer from 'yt-player'
|
2020-01-10 15:16:48 +01:00
|
|
|
import { entry, paused, muted, volume } from '/store.js'
|
2020-01-10 03:02:46 +01:00
|
|
|
|
|
|
|
let element
|
|
|
|
let player
|
|
|
|
|
2020-01-10 15:16:48 +01:00
|
|
|
let currentTime = null
|
|
|
|
let duration = null
|
|
|
|
|
2020-01-10 03:02:46 +01:00
|
|
|
$: updateEntry($entry)
|
2020-01-10 15:16:48 +01:00
|
|
|
$: updatePaused($paused)
|
2020-01-10 03:02:46 +01:00
|
|
|
$: updateMuted($muted)
|
2020-01-10 03:42:02 +01:00
|
|
|
$: updateVolume($volume)
|
2020-01-10 03:02:46 +01:00
|
|
|
|
2020-01-10 15:16:48 +01:00
|
|
|
const updateViewerDurationCallback = () => {
|
|
|
|
console.log('update')
|
|
|
|
if (player) {
|
|
|
|
duration = player.getDuration()
|
|
|
|
currentTime = player.getCurrentTime()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 03:02:46 +01:00
|
|
|
const updateEntry = (entry) => {
|
2020-01-10 15:16:48 +01:00
|
|
|
if (player && entry) {
|
|
|
|
duration = null
|
|
|
|
currentTime = null
|
|
|
|
player.off('playing', updateViewerDurationCallback)
|
|
|
|
|
|
|
|
player.load(entry.data.id, !$paused)
|
|
|
|
}
|
2020-01-10 03:02:46 +01:00
|
|
|
}
|
|
|
|
|
2020-01-10 15:16:48 +01:00
|
|
|
const updatePaused = (paused) => {
|
|
|
|
if (player) paused ? player.pause() : player.play()
|
2020-01-10 03:02:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const updateMuted = (muted) => {
|
|
|
|
if (player) muted ? player.mute() : player.unMute()
|
|
|
|
}
|
|
|
|
|
2020-01-10 03:42:02 +01:00
|
|
|
const updateVolume = (volume) => {
|
|
|
|
if (player) player.setVolume(volume)
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:16:48 +01:00
|
|
|
|
|
|
|
|
2020-01-10 03:02:46 +01:00
|
|
|
onMount(() => {
|
|
|
|
player = new YoutubePlayer(element, {
|
|
|
|
width: 300,
|
|
|
|
height: 300,
|
2020-01-10 15:16:48 +01:00
|
|
|
autoplay: !$paused,
|
2020-01-10 14:30:17 +01:00
|
|
|
controls: false,
|
2020-01-10 03:02:46 +01:00
|
|
|
keyboard: false,
|
|
|
|
fullscreen: false,
|
|
|
|
modestBranding: true,
|
|
|
|
related: false
|
|
|
|
})
|
|
|
|
|
2020-01-10 15:16:48 +01:00
|
|
|
updatePaused($paused)
|
2020-01-10 03:42:02 +01:00
|
|
|
updateMuted($muted)
|
|
|
|
updateVolume($volume)
|
|
|
|
|
2020-01-10 15:16:48 +01:00
|
|
|
// player.on('playing', () => {
|
|
|
|
// $paused = false
|
|
|
|
// })
|
|
|
|
|
|
|
|
// player.on('paused', () => {
|
|
|
|
// $paused = true
|
|
|
|
// })
|
|
|
|
|
|
|
|
player.on('unstarted', () => {
|
|
|
|
player.once('playing', updateViewerDurationCallback)
|
|
|
|
})
|
|
|
|
|
|
|
|
player.on('timeupdate', (time) => {
|
|
|
|
currentTime = time
|
|
|
|
})
|
|
|
|
|
2020-01-10 03:02:46 +01:00
|
|
|
player.on('ended', () => entry.next())
|
2020-01-10 14:30:17 +01:00
|
|
|
|
|
|
|
player.on('unplayable', (...args) => {
|
|
|
|
console.log('unplayable', ...args)
|
|
|
|
entry.next()
|
|
|
|
})
|
2020-01-10 03:02:46 +01:00
|
|
|
})
|
2020-01-10 15:16:48 +01:00
|
|
|
|
|
|
|
onDestroy(() => {
|
|
|
|
if (player) {
|
|
|
|
player.destroy()
|
|
|
|
}
|
|
|
|
})
|
2020-01-10 03:02:46 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
|
|
|
</style>
|