Play/Pause YouTube player

//js var tag = document.createElement('script'); tag.src = "https://www.youtube.com/player_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function createVideo(vid){ return new YT.Player('top-player', {videoId: vid}); } function onYouTubePlayerAPIReady() { var video = $("#top-player").attr("data-id"); player = createVideo(video); $('#play').on("click", function() {player.playVideo();}); $('#pause').on("click", function() {player.pauseVideo();}); } //2. HTML markup. Add video id in 'data-id' attribute /* <div id="top-player" data-id="iIdvUYjnUCY"></div> <a href="#" id="pause">Pause</a> <a href="#" id="play">Play</a> */
Simple example how to play / pause youtube video

1 Response

I have recently implimented this code on in-buil player of teatv app ( http://teatvappdownloads.com/). initially it was not responsive, latest when I added two more lines on above snippet its working like charming.

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.