How to Create an Interactive YouTube Video Selection in Vue.js

Let’s start with my motivation. It came from the improvements of the #BlueCloudMirror game UI. We decided to provide the #BlueCloudMirror users a recorded YouTube LiveStream of us. This video provides an awesome overview of our major technologies.

The video takes approximately 50 minutes. This is a long time to watch, so we want to provide the user a new interactive architecture page, as you can see below. A user should have the ability to select the topic they is interested in. To do this, we needed the position selection for the YouTube video. In the gif you can see the result.

vue-youtube-selection

Topics of this blog post:

  • How to define dynamic values in a Vue template.
  • How to configure autostart for YouTube videos.
  • How to avoid SAMEORI problem with the YouTube video.

How to Define Dynamic Values in a Vue Template

You can find the code of the Architecture.vue file on GitHub.

For the UI, I used the option template, and for the event handling the options methods and data.

To show you a high level view of how the code works, I made a very simplified sequence diagram.

vue-youtube-diagram.png

Details of the Sequence Diagram

1. V-BIND onClick Event

The following code shows that the Vue template contains the button. This button will trigger an event. The definition for this event is defined using v-on together with the related method onYouTubeDemo.

<b-button
  block
  v-on:click="onYouTubeDemo"
  style="margin-right:10px;
         background-color: #FFFFFF;
         border-color:#030303;"
 ><fontcolor="black">Game demo [5:03 mins]</font>
</b-button>

2. Set the Right Value for ‘Game Demo’

The method  onYouTubeDemo is called by clicking the button we defined before. Inside this method, the internal JSON array variable sets the right YouTube video URL, for using in the templatefor the UI later.

In the following code, you can see the internal JSON array variable definition of youtubedata.The JSON array reflects the properties to display the video in an iFrame later.

var youtubedata = {     
  "0":{ width:"853",       
        height:"480",       
        src:"https://www.youtube.com/embed/Z4wU03JnEcU?autoplay=1&amp;amp;amp;start=303",                        frameborder:"0",       allow:"accelerometer; autoplay; encrypted-media; gyroscope;  picture-in-picture", 
        allowfullscreen:"" }};

Inside the code below, in  methods, the event method  onYouTubeDemo is responsible for setting the right video URL for the previouly defined JSON array variable youtubedata.The variable is used in the data option later.

methods: {   
  onYouTubeDemo() {   
     youtubedata[0].src="https://www.youtube.com/embed/Z4wU03JnEcU?autoplay=1&amp;amp;start=303"},

Data organizes the data exchange for the web front-end with these variables youtubeJSON:youtubedata. You can find more details in the Vue best practices docs.

data: function () {   
    return {youtubeJSON:youtubedata}},

3. Show the Video in an iFrame

With the v-bind, key and v-for we are setting the right data values for the YouTube video to display inside an iFrame. Here you can find the variable youtubeJSON. The blog post Use JSON to dynamically build web pages with Vue.js is awesome help for more details.

<center><iframe
   v-for="data in youtubeJSON"
   v-bind:key="data.width"
   v-bind:width="data.width"
   v-bind:height="data.height"
   v-bind:src="data.src"
   v-bind:frameborder="data.frameborder"
   v-bind:allow="data.allow"
   allowfullscreen
</iframe></center>

HOW TO CONFIGURE AUTOSTART FOR THE YOUTUBE VIDEO

I discovered on the internet: you have to ensure that the youtube url contains the parameter autoplay and the value is set to 1.

src:"https://www.youtube.com/embed/Z4wU03JnEcU?autoplay=1&start=303"

How to Avoid the SAMEORI Problem

The SAMEORI problem, in our case, is when you cannot display the video in a iFrame. To avoid the SAMEORI problem, simply ensure you use embed in your YouTube URL in the source code. A very useful note is on Stack Overflow related to the “sameori” error

src:"https://www.youtube.com/embed/Z4wU03JnEcU?autoplay=1&start=303"

This was all about my observations during my implementation of an interactive YouTube Video selection in Vue.

Thanks For Visiting, Keep Visiting

If you liked this post, share it with all of your programming buddies!

This post was originally published here

#vue-js #web-development

How to Create an Interactive YouTube Video Selection in Vue.js
21.55 GEEK