Mute YouTube sound in Delphi TWebBrowser

2019-08-10 06:20发布

问题:

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.

回答1:

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.



回答2:

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? (: