How to create an interactive YouTube Video selection in VUE

In this blog post I want to share my observations during my implementation of an interactive YouTube Video selection in VUE. Maybe this can save you some time, if you plan to do the same.

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 he 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 fileon 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 template for 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 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 frontend with these variables youtubeJSON:youtubedata.  You can find more details  in the VUE best practices.
data: function () {

return {

youtubeJSON:youtubedata

}

},

2. Show the video in iFrame

With the v-bind  and key, and v-for we are setting the right data values for the YouTube video to display inside 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 is 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&amp;start=303"

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

I hope this was useful for you and let’s see what’s next?

Greetings,

Thomas

PS and FYI … You can use the IBM Cloud for free just create a IBM Lite Account only a email is requested.

#VUE, #SAMEORI, #javascript, #IBMDeveloper, #BlueCloudMirror

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: