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

75 lines
2.1 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
2024-04-04 22:20:42 +00:00
<ScreenBase>
<template #header>
<ScreenHeader layout="collapsed">{{ title }}</ScreenHeader>
</template>
2022-04-15 14:24:30 +00:00
2024-04-04 22:20:42 +00:00
<ScreenEmptyState v-if="!showingVideo" data-testid="youtube-placeholder">
<template #icon>
<Icon :icon="faYoutube" />
</template>
YouTube videos will be played here.
<span class="secondary">Start a video playback from the right sidebar.</span>
</ScreenEmptyState>
<div id="player" />
</ScreenBase>
2022-04-15 14:24:30 +00:00
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
2024-04-04 22:20:42 +00:00
import { unescape } from 'lodash'
2022-07-15 07:23:55 +00:00
import { faYoutube } from '@fortawesome/free-brands-svg-icons'
2022-04-23 22:10:46 +00:00
import createYouTubePlayer from 'youtube-player'
2022-10-13 15:18:47 +00:00
import { ref, watch } from 'vue'
2022-06-10 10:47:46 +00:00
import type { YouTubePlayer } from 'youtube-player/dist/types'
2022-10-13 15:18:47 +00:00
import { eventBus, requireInjection, use } from '@/utils'
2022-04-24 08:50:45 +00:00
import { playbackService } from '@/services'
2024-05-19 05:49:42 +00:00
import { CurrentPlayableKey } from '@/symbols'
2022-04-15 14:24:30 +00:00
import ScreenHeader from '@/components/ui/ScreenHeader.vue'
import ScreenEmptyState from '@/components/ui/ScreenEmptyState.vue'
2024-04-04 22:20:42 +00:00
import ScreenBase from '@/components/screens/ScreenBase.vue'
2022-04-15 14:24:30 +00:00
let player: YouTubePlayer
2022-04-15 17:00:08 +00:00
const title = ref('YouTube Video')
2024-04-04 22:20:42 +00:00
const showingVideo = ref(false)
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 17:00:08 +00:00
})
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
// Pause song playback when video is played
2022-04-24 08:50:45 +00:00
player.on('stateChange', ({ data }) => data === 1 && playbackService.pause())
2022-04-15 17:00:08 +00:00
}
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
2024-05-19 05:49:42 +00:00
const currentSong = requireInjection(CurrentPlayableKey)
2022-04-23 22:10:46 +00:00
2022-10-13 15:18:47 +00:00
/**
* Pause video playback when a song is played/resumed.
*/
watch(() => currentSong.value?.playback_state, state => state === 'Playing' && player?.pauseVideo())
eventBus.on('PLAY_YOUTUBE_VIDEO', payload => {
2024-04-04 22:20:42 +00:00
showingVideo.value = true
title.value = unescape(payload.title)
2022-04-15 14:24:30 +00:00
2022-10-13 15:18:47 +00:00
use(getPlayer(), player => {
player.loadVideoById(payload.id)
player.playVideo()
})
2022-04-15 14:24:30 +00:00
})
</script>
2024-04-04 20:13:35 +00:00
<style lang="postcss" scoped>
2024-04-04 22:20:42 +00:00
:deep(iframe#player) {
/* this is the iframe created by the YouTubePlayer plugin, not the div element! */
@apply -m-6 w-auto h-auto flex-1 flex flex-col;
2022-04-15 14:24:30 +00:00
}
</style>