VideoScene.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { jsx as _jsx } from "react/jsx-runtime";
  2. import { useContext, useEffect } from "react";
  3. import { observer } from "mobx-react";
  4. import { KrpanoRendererContext } from "../contexts";
  5. import { videoSceneModel } from "../models";
  6. const DEFAULT_PLUGIN_ATTRS = {
  7. loop: true,
  8. volume: 0,
  9. };
  10. export const VideoScene = observer(({ name, videointerfaceXmlUrl, videoplayerUrl, sourceList, playRes, pluginAttrs, children, onVisibility, }) => {
  11. const renderer = useContext(KrpanoRendererContext);
  12. const model = videoSceneModel;
  13. const getSourceListStr = () => {
  14. let str = "";
  15. sourceList.forEach((item) => {
  16. str += `videointerface_addsource('${item.res}', '${item.url}', '${item.poster}');`;
  17. });
  18. return str;
  19. };
  20. const objectToString = (obj) => {
  21. let stack = [];
  22. Object.keys(obj).forEach((key) => {
  23. stack.push(`${key}="${obj[key]}"`);
  24. });
  25. return stack.join(" ");
  26. };
  27. const visibilityHandler = () => {
  28. if (document.visibilityState === "hidden") {
  29. model.pause();
  30. }
  31. onVisibility === null || onVisibility === void 0 ? void 0 : onVisibility();
  32. };
  33. useEffect(() => {
  34. window.addEventListener("visibilitychange", visibilityHandler);
  35. return () => {
  36. window.removeEventListener("visibilitychange", visibilityHandler);
  37. };
  38. }, []);
  39. useEffect(() => {
  40. if (!renderer)
  41. return;
  42. const _pluginAttrs = objectToString(Object.assign({}, DEFAULT_PLUGIN_ATTRS, pluginAttrs, {
  43. pausedonstart: !model.playing,
  44. }));
  45. renderer.tagAction.pushSyncTag("scene", {
  46. name,
  47. }, `
  48. <!-- include the videoplayer interface / skin -->
  49. <include url="${videointerfaceXmlUrl}" />
  50. <!-- include the videoplayer plugin -->
  51. <plugin ${_pluginAttrs} name="video"
  52. url="${videoplayerUrl}"
  53. onloaded="add_video_sources();"
  54. />
  55. <!-- use the videoplayer plugin as panoramic image source -->
  56. <image>
  57. <sphere url="plugin:video" />
  58. </image>
  59. <action name="add_video_sources" >
  60. ${getSourceListStr()}
  61. videointerface_play('${playRes}');
  62. </action>
  63. `);
  64. }, [renderer]);
  65. return _jsx("div", { className: "video-scene", children: children });
  66. });