convert playing state to paused

This commit is contained in:
wryk 2020-01-10 15:16:48 +01:00
parent e6e88fddd5
commit 5b02f84a86
5 changed files with 63 additions and 16 deletions

View File

@ -34,7 +34,7 @@
import { onMount } from 'svelte' import { onMount } from 'svelte'
import Controls from '/components/Controls.svelte' import Controls from '/components/Controls.svelte'
import Viewer from '/components/Viewer.svelte' import Viewer from '/components/Viewer.svelte'
import { playing, loading, entry as currentEntry, entries } from '/store.js' import { entry as currentEntry, entries } from '/store.js'
onMount(() => { onMount(() => {
const unsubscribe = entries.subscribe(async (xs) => { const unsubscribe = entries.subscribe(async (xs) => {

View File

@ -19,7 +19,7 @@
<div class="controls-group"> <div class="controls-group">
<button on:click={() => entry.previous()}>⏮️</button> <button on:click={() => entry.previous()}>⏮️</button>
<button on:click={() => $playing = !$playing}>{#if $playing}⏸️{:else}{/if}</button> <button on:click={() => $paused = !$paused}>{#if $paused}▶️{:else}{/if}</button>
<button on:click={() => entry.next()}>⏭️</button> <button on:click={() => entry.next()}>⏭️</button>
</div> </div>
@ -30,7 +30,7 @@
</div> </div>
<script> <script>
import { playing, volume, muted, entry } from '/store.js' import { paused, volume, muted, entry } from '/store.js'
</script> </script>
<style> <style>

View File

@ -1,27 +1,52 @@
<div> <div>
<div>
<div bind:this={element}></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}
</div> </div>
<script> <script>
import { onMount } from 'svelte' import { onMount, onDestroy } from 'svelte'
import { get } from 'svelte/store' import { get } from 'svelte/store'
import YoutubePlayer from 'yt-player' import YoutubePlayer from 'yt-player'
import { entry, playing, muted, volume } from '/store.js' import { entry, paused, muted, volume } from '/store.js'
let element let element
let player let player
let currentTime = null
let duration = null
$: updateEntry($entry) $: updateEntry($entry)
$: updatePlaying($playing) $: updatePaused($paused)
$: updateMuted($muted) $: updateMuted($muted)
$: updateVolume($volume) $: updateVolume($volume)
const updateEntry = (entry) => { const updateViewerDurationCallback = () => {
if (player && entry) player.load(entry.data.id, get(playing)) console.log('update')
if (player) {
duration = player.getDuration()
currentTime = player.getCurrentTime()
}
} }
const updatePlaying = (playing) => { const updateEntry = (entry) => {
if (player) playing ? player.play() : player.pause() if (player && entry) {
duration = null
currentTime = null
player.off('playing', updateViewerDurationCallback)
player.load(entry.data.id, !$paused)
}
}
const updatePaused = (paused) => {
if (player) paused ? player.pause() : player.play()
} }
const updateMuted = (muted) => { const updateMuted = (muted) => {
@ -32,11 +57,13 @@
if (player) player.setVolume(volume) if (player) player.setVolume(volume)
} }
onMount(() => { onMount(() => {
player = new YoutubePlayer(element, { player = new YoutubePlayer(element, {
width: 300, width: 300,
height: 300, height: 300,
autoplay: $playing, autoplay: !$paused,
controls: false, controls: false,
keyboard: false, keyboard: false,
fullscreen: false, fullscreen: false,
@ -44,10 +71,26 @@
related: false related: false
}) })
updatePlaying($playing) updatePaused($paused)
updateMuted($muted) updateMuted($muted)
updateVolume($volume) updateVolume($volume)
// player.on('playing', () => {
// $paused = false
// })
// player.on('paused', () => {
// $paused = true
// })
player.on('unstarted', () => {
player.once('playing', updateViewerDurationCallback)
})
player.on('timeupdate', (time) => {
currentTime = time
})
player.on('ended', () => entry.next()) player.on('ended', () => entry.next())
player.on('unplayable', (...args) => { player.on('unplayable', (...args) => {
@ -55,6 +98,12 @@
entry.next() entry.next()
}) })
}) })
onDestroy(() => {
if (player) {
player.destroy()
}
})
</script> </script>
<style> <style>

View File

@ -10,7 +10,7 @@ export const hashtags = writableLocalStorage('hashtags', [
'pouetradio' 'pouetradio'
]) ])
export const playing = writable(true) export const paused = writable(false)
export const muted = writableLocalStorage('muted', false) export const muted = writableLocalStorage('muted', false)
export const volume = writableLocalStorage('volume', 100) export const volume = writableLocalStorage('volume', 100)

View File

@ -62,8 +62,6 @@ async function statusToEntry(status) {
async function urlToEntry(urlAsString) { async function urlToEntry(urlAsString) {
const url = new URL(urlAsString) const url = new URL(urlAsString)
console.log(url.hostname)
if (['youtube.com', 'music.youtube.com'].includes(url.hostname) && url.searchParams.has('v')) { if (['youtube.com', 'music.youtube.com'].includes(url.hostname) && url.searchParams.has('v')) {
return await mkYoutubeEntry(url.searchParams.get('v')) return await mkYoutubeEntry(url.searchParams.get('v'))
} else if (url.hostname === 'youtu.be') { } else if (url.hostname === 'youtu.be') {