gpt4 book ai didi

Delphi 中的 Javascript 和 youtube api

转载 作者:行者123 更新时间:2023-11-30 16:31:03 24 4
gpt4 key购买 nike

我正在尝试对 WebBrowser1 执行具有 youtube api 的 javascript。

procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate(ExtractFilePath(ParamStr(0)) + 'test.html');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Doc: IHTMLDocument2; // current HTML document
HTMLWindow: IHTMLWindow2; // parent window of current HTML document
JSFn: string;
begin
// Get reference to current document
Doc := WebBrowser1.Document as IHTMLDocument2;
if not Assigned(Doc) then
Exit;
// Get parent window of current document
HTMLWindow := Doc.parentWindow;
if not Assigned(HTMLWindow) then
Exit;
// Run JavaScript
try
JSFn := 'onYouTubePlayerAPIReady()';
HTMLWindow.execScript(JSFn, 'JavaScript');
except
on E : Exception do
ShowMessage(E.ClassName+' error raised, with message : '+E.Message); //error EOleException, 80020101
end;
end;

但这引发了一个错误:EOleException,80020101

这是我的test.html

<div id="player"></div>
<script src="http://www.youtube.com/player_api"></script>
<script>

// create youtube player
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', { //<-- error
height: '390',
width: '640',
videoId: '0Bmhjf0rKe8',
events: {
'onReady': onPlayerReady
}
});
}


// autoplay video
function onPlayerReady(event) {
event.target.playVideo();
}

</script>

在这里,我想将视频嵌入到 WebBrowser1,然后使用具有 youtube api 的 javascript 自动播放它。

这有可能使这项工作成功吗?

最佳答案

您正在使用 Youtube javascript player API ,已弃用。您必须使用 Youtube Iframe player API .如果您控制了 HTML,则不需要在 Delphi 端编写特殊代码。

这是一个完整的工作示例:

HTML 代码(与 YT API 引用页中的代码相同,我只是添加了自动播放变量):

<!DOCTYPE html>
<html>
<head>
<!-- // this is needed to force our embedded browser to run in EDGE mode -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>

<script>

// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: { 'autoplay': 1, 'controls': 0 }, // this is essential for autoplay
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}

// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}

// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>

德尔福代码:

unit u_frm_main;

interface

uses
MsHtml,
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.OleCtrls, SHDocVw;

type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate(ExtractFilePath(ParamStr(0)) + 'test.html');
end;

end.

关于Delphi 中的 Javascript 和 youtube api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33315801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com