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.
Topics of this blog post:
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.
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>
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;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;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}},
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"
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.
If you liked this post, share it with all of your programming buddies!
This post was originally published here
#vue-js #web-development