PyQt - Mute QWebPage

82 views Asked by At

I am search for a few hours, to find a good way to mute a QWebPage. I don't want to display the web page. I just fetched it and then i evaluate one line of javascript code in it. Then i close the QPageView.

But the web page includes some mp3 streams, so while the javascript code is evaluated i can hear from this stream. I try to cut the mp3 sound until terminated the QWebBrowser.

I use QWebKit instead of QWebEngine because there is no available package for msys posix platoform (link).

So the problem is for just one or two seconds. I didn't find anything useful about in QWebSettings page

In file: C:\mysys64\mingw64\include\QtWebKit\5.212.0\QtWebKit\private\qwebpreferences_p.h at line 108 i found this:

void setWebAudioEnabled(bool enable);

So somehow it might be a way to mute the QWebView or the QWebPage.

I found an answer here in stackoverflow that evaluates javascript code when the page is loaded to mute all video and audio tags that the page has. But this answer also has a delay problem.

So, how can i mute all the sound a web page has in python QWebKit - QWebPage?

2

There are 2 answers

1
dsillman2000 On

Have you considered using QWebPage.ToggleMediaMute? According to the C++ documentation:

Mute or unmute the hovered audio or video element. (Added in Qt 5.2)

This means that you should be able to take a QWebPage from your source code and call qwp.action(QWebPage.ToggleMediaMute) in order to mute audio from the page.

0
Chris P On

The problem solved using selenium.

  1. pip install selenium
  2. Download geckodriver.exe from this link
  3. Run this code snippet
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
url = "..."
javascript_code = "..."
firefox_options = Options()
firefox_options.add_argument("--headless")
wd = webdriver.Firefox(executable_path=r'G:\Geckodriver\geckodriver.exe',options=firefox_options)

wd.get(url)
javascript_output = str(wd.execute_script("return "+javascript_code)).strip()

With this solution there are two problems i think:

  1. What if the user doesn't have firefox installed in the pc. May i have a try except block to catch this error?

  2. There is a delay (~10 seconds) for a normal web page. That's not a big delay i think. :)