如何在Youtube中使用Javascript停止视频?

时间:2022-03-17 18:57:45

Situation: here, where I pressed some video.

情况:在这里,我按下了一些视频。

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

问题:我尝试在Firebug的控制台中通过Javascript停止视频:

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

Question: Why does not the command above work?

问题:为什么上面的命令不起作用?

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

[1]部分来源“player.stopVideo():Void”

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

[2]我从源头看到了Firebug的玩家。

3 个解决方案

#1


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.

您的视频正在请求启用JSAPI,因此您非常接近!您所需要的只是嵌入式播放器的有效参考。检查您的页面显示您使用HTML DOM元素ID“playerid”来识别您的播放器。

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();

#2


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.

以下工作正常,在wamp服务器上测试。只需将以下行中的11位数ID替换为您要播放的视频。

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>

#3


So this is quickest way to do it!

所以这是最快捷的方式!

Open Developer Options

打开开发者选项

Hover over YouTube play button.

将鼠标悬停在YouTube播放按钮上。

Press Control/Command + Shift + C

按Control / Command + Shift + C.

Select the Button

选择按钮

Add id to it.

添加id到它。

Go to console and run the following

转到控制台并运行以下命令

var x = document.getElementById('id'); x.click();

var x = document.getElementById('id'); x.click();

That's it!

#1


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.

您的视频正在请求启用JSAPI,因此您非常接近!您所需要的只是嵌入式播放器的有效参考。检查您的页面显示您使用HTML DOM元素ID“playerid”来识别您的播放器。

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();

#2


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.

以下工作正常,在wamp服务器上测试。只需将以下行中的11位数ID替换为您要播放的视频。

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>

#3


So this is quickest way to do it!

所以这是最快捷的方式!

Open Developer Options

打开开发者选项

Hover over YouTube play button.

将鼠标悬停在YouTube播放按钮上。

Press Control/Command + Shift + C

按Control / Command + Shift + C.

Select the Button

选择按钮

Add id to it.

添加id到它。

Go to console and run the following

转到控制台并运行以下命令

var x = document.getElementById('id'); x.click();

var x = document.getElementById('id'); x.click();

That's it!