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

71 lines
1.7 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">
<ScreenEmptyState data-testid="youtube-placeholder">
2022-04-15 14:24:30 +00:00
<template v-slot:icon>
<i class="fa fa-youtube-play"></i>
</template>
YouTube videos will be played here.
2022-04-23 22:10:46 +00:00
<span class="d-block instruction">Start a video playback from the right sidebar.</span>
</ScreenEmptyState>
2022-04-15 14:24:30 +00:00
</div>
</section>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
2022-04-23 22:10:46 +00:00
import createYouTubePlayer from 'youtube-player'
2022-04-15 17:00:08 +00:00
import { defineAsyncComponent, ref } from 'vue'
2022-04-15 14:24:30 +00:00
import { YouTubePlayer } from 'youtube-player/dist/types'
2022-04-23 22:10:46 +00:00
import { eventBus, use } from '@/utils'
2022-04-15 14:24:30 +00:00
import { playback } from '@/services'
2022-04-23 22:10:46 +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'))
const ScreenEmptyState = defineAsyncComponent(() => import('@/components/ui/ScreenEmptyState.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-23 22:10:46 +00:00
const getPlayer = () => {
2022-04-15 17:00:08 +00:00
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-23 22:10:46 +00:00
return player
2022-04-15 17:00:08 +00:00
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
eventBus.on({
2022-04-23 22:10:46 +00:00
PLAY_YOUTUBE_VIDEO (payload: { id: string, title: string }) {
2022-04-15 17:00:08 +00:00
title.value = payload.title
2022-04-23 22:10:46 +00:00
use(getPlayer(), player => {
player.loadVideoById(payload.id)
player.playVideo()
})
2022-04-15 17:00:08 +00:00
},
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>