koel/resources/assets/js/components/screens/youtube.vue

72 lines
1.8 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
<section id="youtubeWrapper">
<screen-header>{{ title }}</screen-header>
<div id="player">
<screen-placeholder data-testid="youtube-placeholder">
<template v-slot:icon>
<i class="fa fa-youtube-play"></i>
</template>
YouTube videos will be played here.
<span class="d-block instruction">
Start a video playback from the right sidebar.
</span>
</screen-placeholder>
</div>
</section>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
import { defineAsyncComponent, ref } from 'vue'
2022-04-15 14:24:30 +00:00
import { YouTubePlayer } from 'youtube-player/dist/types'
import { eventBus } from '@/utils'
import { playback } from '@/services'
import createYouTubePlayer from 'youtube-player'
2022-04-15 17:00:08 +00:00
let player: YouTubePlayer|null = null
2022-04-15 14:24:30 +00:00
2022-04-21 16:06:45 +00:00
const ScreenHeader = defineAsyncComponent(() => import('@/components/ui/ScreenHeader.vue'))
2022-04-15 17:00:08 +00:00
const ScreenPlaceholder = defineAsyncComponent(() => import('@/components/ui/screen-placeholder.vue'))
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const title = ref('YouTube Video')
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
/**
* Initialize the YouTube player. This should only be called once.
*/
const maybeInitPlayer = () => {
if (!player) {
player = createYouTubePlayer('player', {
width: '100%',
height: '100%'
})
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
// Pause song playback when video is played
player.on('stateChange', ({ data }) => data === 1 && playback.pause())
}
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
eventBus.on({
'PLAY_YOUTUBE_VIDEO': (payload: { id: string, title: string }) => {
title.value = payload.title
maybeInitPlayer()
player!.loadVideoById(payload.id)
player!.playVideo()
},
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
/**
* Stop video playback when a song is played/resumed.
*/
'SONG_STARTED': () => player && player.pauseVideo()
2022-04-15 14:24:30 +00:00
})
</script>
<style lang="scss" scoped>
#player {
height: 100%;
.instruction {
font-size: 1.5rem;
}
}
</style>