0

I embed youtube within QML WebEngineView. Everything is working fine but only the autoplay is not working. I try to add ( ?autoplay=1 ) after URL as this answer How can I autoplay a video using the new embed code style for Youtube? but It still not working.

Here is my QML code.

ApplicationWindow {
    id: root
    visible: true
    width: 576; height: 400
    title: "Test"
    x: 0;y: 0;

    WebEngineView {
        id: webViewID
        anchors.fill: parent
        backgroundColor: "black"
    }

    Component.onCompleted: {
        webViewID.loadHtml('<iframe id="ms-youtube" width="100%" height="100%" src="https: //www.youtube.com/embed/-mN3VyJuCjM?autoplay=1 " title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')
    }
}
Joe
  • 811
  • 1
  • 8
  • 14
  • you probably have to avoid spaces in the URL. also check the `baseUrl` that is probably important too. also I would format the HTML as that should be, i.e. ` ...` – folibis Sep 06 '22 at 12:44

1 Answers1

3

pyqt5 qwebenginview doesn't autoplay youtube videos

You need to add settings.playbackRequiresUserGesture: false to your WebEngineView instance. Check Qts documentation for more information about why https://doc.qt.io/qt-6/qml-qtwebengine-webenginesettings.html#playbackRequiresUserGesture-prop

ApplicationWindow {
    id: root
    visible: true
    width: 576
    height: 400
    title: "Test"

    WebEngineView {
        id: webViewID
        anchors.fill: parent
        backgroundColor: "black"
        settings.playbackRequiresUserGesture: false
    }

    Component.onCompleted: {
        webViewID.url = "https://www.youtube.com/embed/-mN3VyJuCjM?autoplay=1"
    }
}
iam_peter
  • 3,205
  • 1
  • 19
  • 31