How to disable sound on flashplayer in my custom application?
Asked Answered
U

2

3

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?

Unloosen answered 31/3, 2015 at 19:40 Comment(3)
What HTML is in the WebView? First you say it's a flashplayer, then you reference <audio>/<video> tags.Brachylogy
for example video from youtube.Unloosen
Do you control the HTML being served, or is it 3rd party like from a YouTube page?Sloppy
S
5

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.

Sloppy answered 9/4, 2015 at 8:0 Comment(0)
C
3

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.

Chink answered 9/4, 2015 at 8:15 Comment(3)
What about the lag before the videos mute? How can he solve that?Sloppy
my code works when I call it and I mentioned this. Question was - when to call it. This is python, and triple quotes do what I need.Unloosen
iam sorry i didnt get the question well , however i see that you have found what you were looking for , good luck :) , even though i dont know how your code works without concatenation even if it is python.Chink

© 2022 - 2024 — McMap. All rights reserved.