#include <windows.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <wmp.h>
#include <memory>
#include <string>
class VideoWindow {
public:
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar);
private:
VideoWindow();
~VideoWindow();
void CreateVideoWindow(const std::string &video_url);
flutter::PluginRegistrarWindows *registrar_;
HWND hwnd_;
CComPtr<IWMPPlayer> player_;
};
void VideoWindow::RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar) {
auto channel = std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
registrar->messenger(), "video_window_channel",
&flutter::StandardMethodCodec::GetInstance());
auto plugin = std::make_unique<VideoWindow>();
channel->SetMethodCallHandler(
[plugin_pointer = plugin.get()](const auto &call, auto result) {
if (call.method_name().compare("createVideoWindow") == 0) {
const auto *arguments = std::get_if<flutter::EncodableMap>(call.arguments());
if (arguments) {
auto video_url_it = arguments->find(flutter::EncodableValue("videoUrl"));
if (video_url_it != arguments->end()) {
std::string video_url = std::get<std::string>(video_url_it->second);
plugin_pointer->CreateVideoWindow(video_url);
result->Success();
return;
}
}
result->Error("Invalid arguments", "Expected videoUrl");
} else {
result->NotImplemented();
}
});
registrar->AddPlugin(std::move(plugin));
}
VideoWindow::VideoWindow() : registrar_(nullptr), hwnd_(nullptr) {}
VideoWindow::~VideoWindow() {
if (player_) {
player_->close();
}
}
void VideoWindow::CreateVideoWindow(const std::string &video_url) {
if (!hwnd_) {
hwnd_ = CreateWindowEx(0, L"STATIC", L"Video Player", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, nullptr, nullptr,
GetModuleHandle(nullptr), nullptr);
ShowWindow(hwnd_, SW_SHOW);
}
if (!player_) {
CoInitialize(nullptr);
player_.CoCreateInstance(CLSID_WindowsMediaPlayer, nullptr, CLSCTX_INPROC_SERVER);
CComPtr<IWMPPlayer4> player4;
player_.QueryInterface(&player4);
player4->put_uiMode(CComBSTR(L"none"));
player4->put_windowlessVideo(VARIANT_TRUE);
player4->put_stretchToFit(VARIANT_TRUE);
player4->put_URL(CComBSTR(video_url.c_str()));
player4->put_windowlessVideo(VARIANT_TRUE);
player4->put_stretchToFit(VARIANT_TRUE);
player4->put_windowlessVideoParent(hwnd_);
}
player_->controls->play();
}
2024-10-08
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 《企业文明》 国家级知网月刊 ISSN:1006-5989 CN:50-1014/G2 【收稿方向】企业管理、经济...