[SOLVED] QWebView - play/pause video programmatically?



  • I am trying to control the playback of YouTube video that is played inside a QWebView :
    "See use-case picture":https://www.dropbox.com/s/a02hw55ngzvuaie/useCaseYoutube.png?dl=0

    The player can be controller with the "YouTube Javascript API":https://developers.google.com/youtube/js_api_reference

    Problem is that most of theses examples use embedded video. I use the YouTube website directly and not embedded video.

    What I tried so far (no success yet):

    @void WebBrowser::pauseVideo() {

    qDebug() << "WebBrowser::pauseVideo()";
    
    QString jsValue =  "$('#movie_player').pauseVideo();";
    ui->webView->page()->mainFrame()->evaluateJavaScript(jsValue);
    ui->webView->page()->mainFrame()->documentElement().evaluateJavaScript(jsValue);
    
    
    jsValue = "$('#movie_player').get(0).pauseVideo();";
    ui->webView->page()->mainFrame()->evaluateJavaScript(jsValue);
    ui->webView->page()->mainFrame()->documentElement().evaluateJavaScript(jsValue);
    

    }
    void WebBrowser::playVideo() {

    qDebug() << "WebBrowser::playVideo()";
    
    QString jsValue = "$('#movie_player').playVideo();";
    ui->webView->page()->mainFrame()->evaluateJavaScript(jsValue);
    ui->webView->page()->mainFrame()->documentElement().evaluateJavaScript(jsValue);
    
    jsValue = "$('#movie_player').get(0).playVideo();";
    ui->webView->page()->mainFrame()->evaluateJavaScript(jsValue);
    ui->webView->page()->mainFrame()->documentElement().evaluateJavaScript(jsValue);
    

    }
    @

    Any help is welcome, I'm sure it can be done
    Inside youtube source code of a page I can see " "enablejsapi": 1," but maybe i'm not using the right playerID?



  • Solved!

    Youtube page doesn't use jquery ;)

    @//-----------------------------------------------------------------
    void WebBrowser::playVideo() {

    qDebug() << "WebBrowser::playVideo()";
    
    QString jsValue = "document.getElementById('movie_player').playVideo();";
    ui->webView->page()->mainFrame()->evaluateJavaScript(jsValue);
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.