0
votes

I would like to use a YouTube embed and make it play/pause with JavaScript Control. I have checked the api documentation But all theses cases refer to a iframe that you embed on your website, but I want to use the original YouTube URL and not create a webpage.

See use case here (picture)

Example URL used : https://www.youtube.com/embed/M7lc1UVf-VE?autohide=1&autoplay=1&enablejsapi=1

The problem seems to be that the iframe doesn't have an ID, so I tried many JavaScript command, but the player never pause or play with my Qt Pushbutton.

Any advice appreciated!

QString jsValue = "document.getElementById('ytplayer').pauseVideo();";
ui->webView->page()->mainFrame()->evaluateJavaScript(jsValue);
1
Short question : Can I use javascript to play/pause this video? (using youtube site and not embedded it) : youtube.com/embed/…Astro
To motivate discussion.. will pay some 1000 Dogecoins to anyone that can help! I'm sure it's possible to do..Astro

1 Answers

0
votes

Solution Found!

YouTube generate a unique ID that you first have to retrieve

QWebElement player = ui->webView->page()->mainFrame()->documentElement().findFirst("div[id=\"player\"]");

QWebElement embed = player.findFirst("embed");

QString embedID = embed.attribute("id");
QString jsToExecute = QString("document.getElementById('%1').playVideo();").arg(embedID);
ui->webView->page()->mainFrame()->evaluateJavaScript(jsToExecute);