快速开始
快速开始
基于 qplayer2-core 的播放器使用
导入头文件
#include <QMediaModel.h>
#include <QIPlayerContext.h>
#include <QIPlayerControlHandler.h>
#include <QIPlayerRenderHandler.h>
初始化
建议将播放器创建方法放置在主窗口的构造函数中
bundle id 和 author id 都用于 QPlayer2 鉴权,任选其一即可。若两个同时填写,则其中一个鉴权通过即认定为鉴权通过。
权限说明见 QPlayer2 鉴权相关
mpPlayerContext = QIPlayerContext::create();
mpPlayerContext->init(QLogLevel::LOG_VERBOSE, std::filesystem::current_path().string(), "您的 bundle id", "1.4.0", "您的 author id", mHwnd);
设置渲染窗口句柄
初始化后设置渲染窗口句柄,建议放在主窗口的构造函数中。mpVideoRenderWindow 可替换为您创建并注册的渲染窗口
mpPlayerContext->get_render_hander()->set_window_hwnd(mpVideoRenderWindow->get_hwnd());
mpVideoRenderWindow 的相关代码
构造函数
VideoRenderWindow::VideoRenderWindow(HWND parent_hwnd, HINSTANCE hinstance)
:mHwnd(nullptr)
{
HINSTANCE hInst = (HINSTANCE)GetWindowLongPtr(parent_hwnd, GWLP_HINSTANCE);
//视频显示窗口
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = render_window_proc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInst;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = TEXT(L"QRenderView");
wcex.hIconSm = NULL;
if (!RegisterClassExW(&wcex)) {
throw "render view create failed!";
}
mHwnd = CreateWindowW(wcex.lpszClassName, TEXT(L"QRenderView"), WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
10, 10, 80, 20, parent_hwnd, NULL, hInst, NULL);
if (mHwnd == nullptr)
{
throw "render view create failed!";
}
}
返回句柄
HWND VideoRenderWindow::get_hwnd()
{
return mHwnd;
}
配置播放器事件循环
LRESULT CALLBACK PlayerWindow::main_player_window_proc(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param) {
PlayerWindow* pplayer_window = (PlayerWindow*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
if (pplayer_window != nullptr) {
if (mpPlayerContext != nullptr) {
pplayer_window->mpPlayerContext->on_receive_message(hwnd, message, w_param, l_param);
return 0;
}
}
}
创建播放数据模型
要在播放前完成播放数据模型的构建。
add_stream_element 可调用多次,但默认播放的元素只能有一个,即第五个参数 is_default 只能有一个元素设置为 true,其他应为 false
QMedia::QMediaModelBuilder* pmodel_builder = new QMedia::QMediaModelBuilder;
pmodel_builder->add_stream_element("", QMedia::QUrlType::QAUDIO_AND_VIDEO, 1080, "http://demo-videos.qnsdk.com/qiniu-2023-1080p.mp4", true, "", "", QMedia::QVideoRenderType::PLANE, "", "", QMedia::QUrlMethod::NORMAL);
model = pmodel_builder->build(false, false);
播放
mpPlayerWindow->get_control_handler()->play_media_model(model, 0);
停止播放
mpPlayerWindow->get_control_handler()->stop();
释放
delete 播放器 context 之前,应先调用 stop 方法,再调用 release 方法,最后再 delete context。避免内存泄漏问题
mpPlayerWindow->get_context()->release();
delete mpPlayerWindow;
mpPlayerWindow = nullptr;
mpPlayerWindow 的析构函数
if (mpPlayerContext != nullptr)
{
delete mpPlayerContext;
mpPlayerContext = nullptr;
}
文档反馈
(如有产品使用问题,请 提交工单)