Control embedded YouTube video by using JS - javascript

I have YouTube video embedded on my webpage. Can I control it by using JS?
I need to retrieve total length of the video and get events when user changes trackbar position (or position changed during playback).

Plyr.js
Using Plyr.js you can control YouTube (and also Vimeo) video with a simple HTML5 media player with 3 steps:
Embedding a YouTube video:
<div id="myVideo" data-type="youtube" data-video-id="4IP_E7efGWE"> </div>
Getting player instance (setup):
var videoEl = $('#myVideo').get(),
player = plyr.setup(videoEl);
Handling events and using methods:
player[0].on('playing', function(event) {
var instance = event.detail.plyr;
console.log(" >playing");
console.log(" >duration: " + instance.getDuration());
});
Example:
here is a jsfiddle: https://jsfiddle.net/beaver71/2g5ggcfa/

When it comes to youtube embeds the Youtube Iframe Player API will supply most of the functions needed for controlling/monitoring events from an embedded youtube video (refer to IFrame Player API
Reference).
If you want to get the length of the video you can use player.getDuration(), more details are available in the API documentation.
In regards to trackbar events the API doesn't supply a direct way to monitor those events but there is a hacky way to do it as mentioned in This question.
Another way to achieve this is by using a 3rd-party library like beaver mentioned in the comment section.
Hope this helps.

Related

is it Possible to get current play time of a video Embedded using iFrame using JavaScript? How?

is there a way to track user's activity on a video embedded using iFrame tag?
the metadata am most interested in is current Duration, title, src...
ps: YouTube Player API did not work for me :(
if it's for youtube as the tag suggest I'd consider using youtube iFrame API
as suggested in this post you could use player.playerInfo.currentTime using that API.
now if it's just a <video> in html, you can use the currentTime property you could also find the video element in your youtube iFrame and use that same property, it's gonna work. Basically as long as there is a video html element, you'll have access to this property

Limelight Video Player || How to play HTML5 video with it

Currently, I am facing issue with Limelight player. It is playing flash videos with Limelight player and player ID is the argument which is passed for the flash videos.
Flash player videos are causing issue and we need to change the approach to play HTML5 videos instead of flash player.But I can't see or find any relevant code in which video stream from limelight player can be feed into the HTML5 player.
Any suggestions or code snippets for making Limelight videos being played with the HTML5 player will be very helpful.
Regards
I am currently working on it and its bit of complex but here is what limelight came with :
<div id="limelight_player_659410"></div><script src="//video.limelight.com/player/limelightjs-player.js"></script>
<script>LimelightPlayerUtil.embed({"height":321,"playerId":"limelight_player_659410","width":480,"mediaId":"c3713feac461425e8889d55689358352","playerForm":"Player"});</script>
First approach is to create limelight callback function:
function limelightPlayerCallback(playerId, eventName, data) {
console.dir(data);
colsole.log(data.thumbnailUrl);
console.log(data.title);
console.log(data.description);
}
So when you call :
LimelightPlayerUtil.embed({"width":"480px","height":"300px", "playerId":"HEREisYOURcontainerID", "mediaId":"HEREisYOURmediaId", "playerForm":"Player"});
your limelightPlayerCallback will get "data".
Now you can see in console all data.proprties and use which one you want to manipulate HTML5 Video tag and display data.
Second approach: Not sure if this link is valid by limelight but I was able to get it from calls I was making :
https://production-ps.lvp.llnw.net/r/PlaylistService/media/d7d5b0ec19e1441781c3df4209fe5cb6/getMobilePlaylistByMediaId
This link will return JSON with all properties. Please note that "d7d5b0ec19e1441781c3df4209fe5cb6" is video ID so replace and use any of your video IDs. Once you get JSON you will get URL and you can inject HTML5 video tag :)

Embedded youtube videos - Simply play a video without audio?

Question:
How can I control the sound output of this embedded video without overly complicating things?
Possible Solution (Complex)
I've looked into the youtube javascript control API, and, correct me if I'm wrong, but that will force me to incorporate a lot of complication into the setup to accomplish what appears to be a very simple task.
Note:
This is all only tested for webkit at this stage
Setup:
I've created an element that simulates an iphone controlling the channel of a tv by swapping youtube videos like so:
var channel1 = "http://www.youtube.com/embed/dFU6Cy4Hd5A?autoplay=1&rel=0&controls=0&showinfo=0&disablekb=1&iv_load_policy=3&modestbranding=1";
$("#channel-1").click(function(){
$("#tv").attr("src", channel1);
});
where the #channel-1 id is connected to a specific button on the remote, and the tv id is connected to the video player.
It looks like this:
Edit:
If the API is the only to accomplish this, please include a link to a comprehensive tutorial teaching the youtube web API, because I've looked, and found nothing. The only thing I've found so far is the official API page, and that's like trying to learn PHP from the manual.
For loading videos you would use loadVideoById rather than the rather odd snippet you have in your question and to play without audio you simply mute the player using player.mute() (where player is a reference retrieved from the youtube player element itself like described here). And one last thing, don't forget to add enablejsapi=1 to the url of the player as described here.
I would use
event.target.mute();

Is it possible to determine if a user finished to see an embedded youtube video with the api?

I need to check if a user has actually seen an embedded youtube video (meaning that he pressed play and saw until the end). Is it possible to do this with the youtube api (javascript)?
Thank you very much
Regards
Using the Youtube API is the most easiest way determining if the user clicked the play button.
For starters check out the Google Youtube Api:
https://developers.google.com/youtube/js_api_reference
You will be needing to use the Swfobject.js to load the video to use the Youtube API controls
on loading the video don't forget to add a listener (addListener)
when the API or the video was loaded you will able to see the state of the videos , here are those:
"-1" - video is ready to play
"1" - the video is playing (it clicked the play button)
"2" - the video is paused
"0" - end of the video
Sample coding on checking the current state of the loaded video:
https://developers.google.com/youtube/js_api_reference#SubscribingEvents
If it is a chromeless YouTube player or if you embedded the video via JW Player, then it is possible.
The chromeless player will strip out the coding and allow you to embed
the YouTube video like javascript, once done, then you can embed a
Google Analytics extension snippet/code to the address of the video
and track the hits that way. Another way is installing JW Player which
allows you to easily track analytics for YouTube videos by
incorporating your Google Analytics UPI.

loadVideoById() in YouTube's regular player (not chromeless)

I have a YouTube's player in the webpage. I need to change the video played by this player dynamicaly.
This is (relatively) easy using YouTube's chromeless player. It has method loadVideoById() which works perfectly. The problem is, that the chromeless player doesn't have any controls (play/pause, etc.). The regular YouTube player has all this, but it doesn't have the loadVideoById() method.
Is there any way to include the controls of regular player into chromeless player, or to implement loadVideoById() method in the regular player?
Thanks.
You can not do that, cause the calls in the "regular youtube player" have the VideoID in the URL instead as a parameter:
Regular Video: http://www.youtube.com/v/VIDEO_ID&enablejsapi=1&playerapiid=ytplayer
Chromeless: http://www.youtube.com/apiplayer?enablejsapi=1
Instead of that you can easily create your own function that changes the Embbebed SWF, i mean, say that you are using swfobject for the "Regular Player", then using createSWF function you will be able to replace the previous video for the actual one dynamically.
Hope this help you.
You can do this...
http://code.google.com/apis/ajax/playground/#change_the_playing_video
FYI there also a jQuery plugin that can do it for you, and provide you with a ton of controls!
http://tikku.com/jquery-youtube-tubeplayer-plugin
https://github.com/nirvanatikku/jQuery-TubePlayer-Plugin
I found it easier to use loadVideoByUrl() as an alternative to loadVideoByIdl() instead of using createSWF, you don't have to mess with the swfobject this way, it's a built-in function...just use the video ID to create a youtube swf url such as: url="http://www.youtube.com/e/" + VIDEO_ID + "?enablejsapi=1&version=3" and submit using the loadVideoByUrl() function like this: player.loadVideoByUrl(url)
That way you don't have to mess with the swfobject, it's spelled out here:
http://code.google.com/apis/youtube/js_api_reference.html#loadVideoById
"player.loadVideoByUrl(mediaContentUrl:String, startSeconds:Number):Void
Loads and plays the specified video.
* The mediaContentUrl must be a fully qualified YouTube player URL in the format http://www.youtube.com/e/VIDEO_ID. In YouTube Data API video feeds, the url attribute of the <media:content> tag contains a fully qualified player URL when the tag's format attribute has a value of 5.
* startSeconds accepts a float/integer and specifies the time from which the video should start playing. If startSeconds (number can be a float) is specified, the video will start from the closest keyframe to the specified time."

Categories