+
-
{{name}}
-
{{state === "loading" ? "Loading..." : title}}
+
{{loadingState === "loading" ? "Loading..." : title}}
@@ -20,26 +20,22 @@ import YouTubePlayerFactory from "youtube-player";
// noinspection TypeScriptCheckImport
import {YouTubePlayer} from "youtube-player/dist/types";
import PlayerStates from "youtube-player/dist/constants/PlayerStates";
+import {LoadingState, PlayingState} from "@/common";
enum SOURCE {
YouTube,
DIRECT
}
-enum ChannelState {
- UNLOADED = "unloaded",
- LOADING = "loading",
- READY = "ready",
- PLAYING = "playing"
-}
-
@Component
export default class Channel extends Vue {
@Prop() public name: string = "Channel";
@Prop() public volume: number = 50;
@Prop() public url: string | undefined;
+ @Prop() public playing: PlayingState = PlayingState.STOPPED;
+
private youtubePlayer?: YouTubePlayer;
- private state = ChannelState.UNLOADED;
+ private loadingState = LoadingState.UNLOADED;
private title = "";
private get source() {
@@ -50,20 +46,18 @@ export default class Channel extends Vue {
}
}
- private get ytVideoId() {
- if (this.url) {
- const match = this.url.match(/v=([\w_\-]+)/);
- if (match !== null && match.length == 2) {
- return match[1];
- }
- }
- }
-
- public start() {
+ @Watch("playing")
+ private onPlayingChange() {
if (this.youtubePlayer) {
- this.youtubePlayer.setVolume(this.volume);
- this.youtubePlayer.playVideo();
- this.state = ChannelState.PLAYING;
+ switch (this.playing) {
+ case PlayingState.PLAYING:
+ this.youtubePlayer.setVolume(this.volume);
+ this.youtubePlayer.playVideo();
+ break;
+ case PlayingState.STOPPED:
+ this.youtubePlayer.stopVideo();
+ break;
+ }
}
}
@@ -76,8 +70,11 @@ export default class Channel extends Vue {
});
this.youtubePlayer.on("stateChange", (event) => {
switch (event.data) {
- case PlayerStates.BUFFERING:
- this.state = ChannelState.READY;
+ case PlayerStates.BUFFERING:
+ this.loadingState = LoadingState.LOADED;
+ if (this.playing == PlayingState.PLAYING) {
+ this.youtubePlayer!.playVideo();
+ }
break;
case PlayerStates.ENDED:
this.youtubePlayer!.playVideo();
@@ -87,7 +84,7 @@ export default class Channel extends Vue {
}
const videoId = Channel.extractYoutubeId(this.url);
if (videoId !== undefined) {
- this.state = ChannelState.LOADING;
+ this.loadingState = LoadingState.LOADING;
console.log(`Loading YouTube video "${videoId}"`);
this.youtubePlayer.loadVideoById(videoId);
this.title = "Loading...";
@@ -101,14 +98,11 @@ export default class Channel extends Vue {
console.error(`Something went wrong trying to parse ${this.url}`);
}
} else {
+ this.loadingState = LoadingState.UNLOADED;
this.title = "N/A";
- }
- }
-
- private static extractYoutubeId(url: string): string | undefined {
- const videoIdMatch = url.match(/(v=|youtu\.be\/)([^&]+)/);
- if (videoIdMatch !== null && videoIdMatch.length == 3) {
- return videoIdMatch[2];
+ if (this.youtubePlayer) {
+ this.youtubePlayer.stopVideo();
+ }
}
}
@@ -119,10 +113,22 @@ export default class Channel extends Vue {
}
}
+ @Watch("loadingState")
+ private onLoadingStateChange() {
+ this.$emit("loadingState", this.loadingState);
+ }
+
private mounted() {
this.onUrlChange();
}
+ private static extractYoutubeId(url: string): string | undefined {
+ const videoIdMatch = url.match(/(v=|youtu\.be\/)([^&]+)/);
+ if (videoIdMatch !== null && videoIdMatch.length == 3) {
+ return videoIdMatch[2];
+ }
+ }
+
private static fetchYoutubeTitle(videoId: string): Promise
{
return new Promise((resolve, reject) => {
let url = `https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${videoId}`;