2024-10-08

#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();
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 面对有意为难你的人,不要退缩,要勇敢面对。 一次校会,主持人大喊:“下面有请成绩最差的学生家长上台讲话。” 一位男...
    天边的虹阅读 128评论 0 6
  • 《企业文明》 国家级知网月刊 ISSN:1006-5989 CN:50-1014/G2 【收稿方向】企业管理、经济...
    岁月静好925阅读 147评论 0 0
  • 从今天开始,你将涅槃重生,逆风翻盘,这局是必胜局,你将会有越来越多的钱,越来越多的财富。你将有无限可能。 从今天开...
    尹小月阅读 87评论 0 0
  • 国庆节的夜晚与平时的夜晚并无什么特别的不同。路灯照射下明亮的街面,来来往往的车辆,行色匆匆的人流……。偶然的念起:...
    74e80bcc1a31阅读 72评论 0 3
  • 这几天看《易经的奥秘》 曾仕强老师的 744也在看,他看2,我看1。 今天起来有气数不够的感觉。 少说话,不动气。...
    Asherly阅读 80评论 0 0

友情链接更多精彩内容