How to disable sound on flashplayer in my custom a

2020-07-23 05:00发布

I use Qt QWebView component which uses flash-player for video playback. How to disable sound on flashplayer executed inside of my QWebView?

One approach that I consider is to execute some javascript code for disabling sound on player, but when to execute it? For example next code disable sound if run it on 1 second after call "load":

page.mainFrame().evaluateJavaScript("""
    var mute_all_tags=function(tag){
        var elems = document.getElementsByTagName(tag);
        for(var i = 0; i < elems.length; i++){
            elems[i].muted=true;
            //alert(elems[i]);
        }
    }
    mute_all_tags("video");
    mute_all_tags("audio");
""")

Earlier calls don't stop sound. Calls on QWebView.loadFinished stops sound but for that moment some sound already issued , how can i stop sound immediately?

2条回答
Bombasti
2楼-- · 2020-07-23 05:28

Please fix your code to following :

page.mainFrame().evaluateJavaScript("var mute_all_tags=function(tag){var elems = document.getElementsByTagName(tag);for(var i = 0; i < elems.length; i++){elems[i].muted=true;}}mute_all_tags('video');mute_all_tags('audio');")

The String in your code is not valid :

  1. You are adding double quotes to string without Concatenation .

  2. You are using double quotes inside double quotes which is a mess.

查看更多
Evening l夕情丶
3楼-- · 2020-07-23 05:30

It sounds like you are loading external pages (i.e. 3rd-party sites) with HTML5 video or Flash video into your QWebView. Also, keeping the videos muted right from the start with absolutely no audio seems like the critical feature you are seeking.

Solution 1

Your page.mainFrame().evaluateJavaScript("...") seems to be the easiest solution, but there will be a lag before this script is executed.

Solution 2

An alternative is to scrape the target website, then using regex or something similar change all the <video> tags to add the mute property e.g. <video controls muted>. Do something analogous for any <embed> tags too. Then, load this modified HTML into the web view with the setHtml() method, also setting the base url, and possibly the referer header. Then the HTML will render with your videos muted from the start.

Solution 3

Another idea might be to intercept media URLs with Qt itself (e.g. .mp4, .mov), and initially hold them in a paused queue, call your page.mainFrame().evaluateJavaScript("...") to programmatically mute the <video> and <audio> tags, then allow the queue to proceed when the evaluateJavaScript() call returns. The media, if auto-playing, should start muted.

查看更多
登录 后发表回答