QML WebEngineView doesn't play YouTube video if the video hasn't 480p quality
The program was written using version Qt 6.4.3
CMakeLists.txt file:
cmake_minimum_required(VERSION 3.16)
project(youtube_video VERSION 0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 6.4 REQUIRED COMPONENTS Quick WebEngineQuick)
qt_standard_project_setup()
qt_add_executable(appyoutube_video
main.cpp
)
qt_add_qml_module(appyoutube_video
URI youtube_video
VERSION 1.0
QML_FILES Main.qml
)
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(appyoutube_video PROPERTIES
# MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appyoutube_video
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
target_link_libraries(appyoutube_video
PRIVATE Qt6::Quick Qt6::WebEngineQuick
)
include(GNUInstallDirs)
install(TARGETS appyoutube_video
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
cmake_minimum_required(VERSION 3.16)
project(youtube_video VERSION 0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 6.4 REQUIRED COMPONENTS Quick WebEngineQuick)
qt_standard_project_setup()
qt_add_executable(appyoutube_video
main.cpp
)
qt_add_qml_module(appyoutube_video
URI youtube_video
VERSION 1.0
QML_FILES Main.qml
)
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(appyoutube_video PROPERTIES
# MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appyoutube_video
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
target_link_libraries(appyoutube_video
PRIVATE Qt6::Quick Qt6::WebEngineQuick
)
include(GNUInstallDirs)
install(TARGETS appyoutube_video
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
To copy to clipboard, switch view to plain text mode
main.cpp file:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtWebEngineQuick/qtwebenginequickglobal.h>
int main(int argc, char *argv[]) {
QtWebEngineQuick::initialize();
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
const QUrl url
(u
"qrc:/youtube_video/Main.qml"_qs
);
&engine,
&QQmlApplicationEngine
::objectCreationFailed,
&app,
[]() { QCoreApplication::exit(-1);
}, Qt
::QueuedConnection);
engine.load(url);
return app.exec();
}
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtWebEngineQuick/qtwebenginequickglobal.h>
int main(int argc, char *argv[]) {
QtWebEngineQuick::initialize();
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
const QUrl url(u"qrc:/youtube_video/Main.qml"_qs);
QObject::connect(
&engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Main.qml file:
import QtQuick
import QtQuick.Controls
import QtQuick.Window
import QtWebEngine
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
WebEngineView {
anchors.fill: parent
url: "https://www.youtube.com/embed/cabBXCq7_P8"
}
}
import QtQuick
import QtQuick.Controls
import QtQuick.Window
import QtWebEngine
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
WebEngineView {
anchors.fill: parent
url: "https://www.youtube.com/embed/cabBXCq7_P8"
}
}
To copy to clipboard, switch view to plain text mode
Shows a preview after launch:
01.jpg
But trying to play the video results in an error:
02.jpg
If change url to: "https://www.youtube.com/watch?v=cabBXCq7_P8"
import QtQuick
import QtQuick.Controls
import QtQuick.Window
import QtWebEngine
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
WebEngineView {
id: webView
anchors.fill: parent
url: "https://www.youtube.com/watch?v=cabBXCq7_P8" //"https://www.youtube.com/embed/cabBXCq7_P8"
}
}
import QtQuick
import QtQuick.Controls
import QtQuick.Window
import QtWebEngine
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
WebEngineView {
id: webView
anchors.fill: parent
url: "https://www.youtube.com/watch?v=cabBXCq7_P8" //"https://www.youtube.com/embed/cabBXCq7_P8"
}
}
To copy to clipboard, switch view to plain text mode
We will receive a message: You browser can't play this video
03.png
Can anyone help me what the error is and how to fix it?
As a result, the video is expected to play
Bookmarks