在 Delphi TWebBrowser 中静音 YouTube 声音
我正在 Delphi 2010 中编写一个小型视频预览工具,但我想以编程方式将视频静音,因为正如我所说,它是用于预览的。
我已经尝试过这段代码的几个版本,但总是会导致脚本错误,最终无法执行。
WebBrowser1.ControlInterface.Document.QueryInterface(IHtmlDocument2, doc);
doc.parentWindow.execScript( 'document.getElementById("movie_player").mute()', 'javascript' );
也尝试再等待一段时间让控件完成浏览,但仍然一无所获。
I'm coding a small Video Previewing tool in Delphi 2010, but I want to mute the videos programmatically, because as I said, it's for previews.
I've tried several versions of this code, but it always results in a script error, and in the end it's unable to do it.
WebBrowser1.ControlInterface.Document.QueryInterface(IHtmlDocument2, doc);
doc.parentWindow.execScript( 'document.getElementById("movie_player").mute()', 'javascript' );
Also tried to wait a little longer for the control to complete browsing, but still nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在 TWebBrowser 的 OnDocumentComplete 事件中调用您的代码。当内部文档完全加载时会触发此事件,因此该对象(如果预期存在)已下载并存在。如果不显示您的 JavaScript 代码,我无法告诉您更多信息。
但我会采取不同的做法。我会实现类似 这个直接进入您的导航网页。它可以在 onYouTubePlayerReady 事件处理程序中立即静音YouTube 播放器已满载。这比稍后调用该函数要好,因为由于 TWebBrowser 的导航完成和代码执行之间存在一些延迟,它可能会产生短促的声音突发。
Try to call your code in TWebBrowser's OnDocumentComplete event. This event is fired when the document inside is fully loaded, so the object, if it's expected to be there, is already downloaded and is present. Without showing of your JavaScript code I can't tell you more.
But I would do it differently. I would implement code like this one directly into your navigated web page. It can mute the sound immediately in the onYouTubePlayerReady event handler what means immediately when the YouTube player is fully loaded. It's better than call the function later on because it may produce a short sound burst because of some delay between the TWebBrowser's navigation completion and execution of your code.
引用 youtube API 的 http://code.google.com/apis/youtube/js_api_reference.html
我相信,当您尝试将视频静音时,“document.getElementById(“movie_player”)”会失败。
尝试像 setTimeout( 'document.getElementById("movie_player")', 10000 ); 那样调用它其中 10000 是 10 秒,甚至更长,播放器可能需要几秒钟才能下载。
我还会在不同的浏览器中尝试一下,看看它是否确实无法在 TWebBrowser 中按预期工作。
编辑
我也想尝试一下“VLC”,它可以本地和远程播放swf文件,有VLC库的接口,为什么不呢? (:
reference to youtube API's http://code.google.com/apis/youtube/js_api_reference.html
I believe that at the time you're trying to mute the video, the "document.getElementById("movie_player")" fails.
try to call it like setTimeout( 'document.getElementById("movie_player")', 10000 ); where 10000 is 10 seconds, or even longer, probably the player needs a couple of seconds to be downloaded.
I would also give it a try in different browsers to see if it's actually something that doesn't work as expected in TWebBrowser.
EDIT
I would also give "VLC" a go, it can play swf files locally and remotely, there are interfaces for the VLC libraries, so why not? (: