Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stop a video with Javascript in Youtube?

Situation: here, where I pressed some video.

Problem: I try to stop the video by Javascript in the console of Firebug:

player.stopVideo(playerid):Void   [1] [2]

Question: Why does not the command above work?

[1] Source for the part "player.stopVideo():Void"

[2] I looked playerid with Firebug from the source.

like image 240
Léo Léopold Hertz 준영 Avatar asked Jul 07 '09 19:07

Léo Léopold Hertz 준영


People also ask

How do you stop a JavaScript video?

Stopping videos with JavaScript # Add this method to your script: var stopVideo = function ( element ) { var iframe = element. querySelector( 'iframe'); var video = element. querySelector( 'video' ); if ( iframe !==

Do you need JavaScript to play videos?

In general, you can view video without JavaScript enabled. Most videos are played through Flash, which you can include directly into your page with either an object tag or an embed tag. There are libraries, such as SWFObject that will put those players into your page for you, but do so through the use of JavaScript.

How do you pause a video when another videos play button is clicked?

onplay = function() { vid2. pause(); };


Video Answer


2 Answers

Your video is requesting w/ the JSAPI enabled, so you are very close! All you need is a valid reference to the embedded player. Inspecting your page revealed that you are using the HTML DOM element id of "playerid" to identify your player.

Example:

<embed id="playerid" width="100%" height="100%" allowfullscreen="true" allowscriptaccess="always" quality="high" bgcolor="#000000" name="playerid" style="" src="http://www.youtube.com/apiplayerbeta?enablejsapi=1&playerapiid=normalplayer" type="application/x-shockwave-flash"> 

To obtain a reference to the player and then stop the video use the following code:

var myPlayer = document.getElementById('playerid'); myPlayer.stopVideo(); 
like image 152
Andrew Martinez Avatar answered Sep 28 '22 17:09

Andrew Martinez


The following works well, tested on wamp server. Just replace the 11-digit ID in the following line with that of the video you want to play.

http://www.youtube.com/v/***LpbzbyGjJGE***?enablejsapi=1&version=3&playerapiid=ytplayer

Good luck.

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<a href="#" onclick="var myPlayer = document.getElementById('playerid'); myPlayer.pauseVideo();">Pause</a>
<a href="#" onclick="var myPlayer = document.getElementById('playerid'); myPlayer.playVideo();">Play</a>
<embed id="playerid" width="500px" height="400px" allowfullscreen="true" allowscriptaccess="always" quality="high" bgcolor="#000000" name="playerid" style="" src="http://www.youtube.com/v/LpbzbyGjJGE?enablejsapi=1&version=3&playerapiid=ytplayer" type="application/x-shockwave-flash">
</body>
</html>
like image 27
Amar Avatar answered Sep 28 '22 18:09

Amar