@galacean/effects-plugin-multimedia 2.6.6 → 2.7.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -3,11 +3,11 @@
3
3
  * Description: Galacean Effects player multimedia plugin
4
4
  * Author: Ant Group CO., Ltd.
5
5
  * Contributors: 云垣
6
- * Version: v2.6.6
6
+ * Version: v2.7.0-alpha.0
7
7
  */
8
8
 
9
9
  import * as EFFECTS from '@galacean/effects';
10
- import { isFunction, passRenderLevel, spec, loadVideo, loadBinary, AbstractPlugin, effectsClass, math, assertExist, BaseRenderComponent, Texture, RendererComponent, registerPlugin, VFXItem, logger } from '@galacean/effects';
10
+ import { isFunction, passRenderLevel, spec, loadVideo, loadBinary, AbstractPlugin, effectsClass, math, assertExist, MaskableGraphic, Texture, RendererComponent, registerPlugin, VFXItem, logger } from '@galacean/effects';
11
11
 
12
12
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
13
13
  try {
@@ -479,11 +479,11 @@ var MultimediaError = /*#__PURE__*/ function(Error1) {
479
479
  }(AbstractPlugin);
480
480
 
481
481
  var seed = 0;
482
- var VideoComponent = /*#__PURE__*/ function(BaseRenderComponent) {
483
- _inherits(VideoComponent, BaseRenderComponent);
482
+ var VideoComponent = /*#__PURE__*/ function(MaskableGraphic) {
483
+ _inherits(VideoComponent, MaskableGraphic);
484
484
  function VideoComponent(engine) {
485
485
  var _this;
486
- _this = BaseRenderComponent.call(this, engine) || this;
486
+ _this = MaskableGraphic.call(this, engine) || this;
487
487
  _this.threshold = 0.03;
488
488
  /**
489
489
  * 播放标志位
@@ -559,27 +559,16 @@ var VideoComponent = /*#__PURE__*/ function(BaseRenderComponent) {
559
559
  _proto.onAwake = function onAwake() {
560
560
  var _this = this;
561
561
  var _this_item_composition, _this_item_composition1;
562
- BaseRenderComponent.prototype.onAwake.call(this);
562
+ MaskableGraphic.prototype.onAwake.call(this);
563
563
  (_this_item_composition = this.item.composition) == null ? void 0 : _this_item_composition.on("goto", function(option) {
564
- if (option.time > 0) {
565
- var _this_item = _this.item, endBehavior = _this_item.endBehavior, start = _this_item.start, duration = _this_item.duration;
566
- if (endBehavior === spec.EndBehavior.freeze || endBehavior === spec.EndBehavior.restart) {
567
- _this.setCurrentTime((option.time - start) % duration);
568
- } else {
569
- if (option.time >= duration) {
570
- _this.onDisable();
571
- } else {
572
- _this.setCurrentTime(option.time - start);
573
- }
574
- }
575
- }
564
+ _this.setCurrentTime(_this.item.time);
576
565
  });
577
566
  (_this_item_composition1 = this.item.composition) == null ? void 0 : _this_item_composition1.on("pause", function() {
578
567
  _this.pauseVideo();
579
568
  });
580
569
  };
581
570
  _proto.fromData = function fromData(data) {
582
- BaseRenderComponent.prototype.fromData.call(this, data);
571
+ MaskableGraphic.prototype.fromData.call(this, data);
583
572
  var interaction = data.interaction, options = data.options;
584
573
  var video = options.video, _options_startColor = options.startColor, startColor = _options_startColor === void 0 ? [
585
574
  1,
@@ -612,20 +601,21 @@ var VideoComponent = /*#__PURE__*/ function(BaseRenderComponent) {
612
601
  this.material.setColor("_Color", new math.Color().setFromArray(startColor));
613
602
  };
614
603
  _proto.render = function render(renderer) {
615
- BaseRenderComponent.prototype.render.call(this, renderer);
604
+ MaskableGraphic.prototype.render.call(this, renderer);
616
605
  this.renderer.texture.uploadCurrentVideoFrame();
617
606
  };
618
607
  _proto.onUpdate = function onUpdate(dt) {
619
- BaseRenderComponent.prototype.onUpdate.call(this, dt);
620
- var _this_item = this.item, time = _this_item.time, duration = _this_item.duration, endBehavior = _this_item.endBehavior, composition = _this_item.composition, start = _this_item.start;
608
+ MaskableGraphic.prototype.onUpdate.call(this, dt);
609
+ var _this_item = this.item, time = _this_item.time, duration = _this_item.duration, endBehavior = _this_item.endBehavior, composition = _this_item.composition;
621
610
  assertExist(composition);
622
611
  var _composition_rootItem = composition.rootItem, rootEndBehavior = _composition_rootItem.endBehavior, rootDuration = _composition_rootItem.duration;
623
- var isEnd = time === 0 || time === rootDuration - start || Math.abs(rootDuration - duration - time) < 1e-10 || Math.abs(time - duration) < this.threshold;
612
+ var isEnd = time === 0 || time === rootDuration || Math.abs(rootDuration - duration - time) < 1e-10 || Math.abs(time - duration) < this.threshold;
624
613
  if (time > 0 && !isEnd) {
625
614
  this.setVisible(true);
626
615
  this.playVideo();
627
616
  }
628
- if (time === 0 || time === rootDuration - start || Math.abs(rootDuration - duration - time) < 1e-10) {
617
+ this.renderer.texture.uploadCurrentVideoFrame();
618
+ if (time === 0 || time === rootDuration || Math.abs(rootDuration - duration - time) < 1e-10) {
629
619
  if (rootEndBehavior === spec.EndBehavior.freeze) {
630
620
  var _this_video;
631
621
  if (!((_this_video = this.video) == null ? void 0 : _this_video.paused)) {
@@ -754,7 +744,7 @@ var VideoComponent = /*#__PURE__*/ function(BaseRenderComponent) {
754
744
  this.video.pause();
755
745
  };
756
746
  _proto.onDestroy = function onDestroy() {
757
- BaseRenderComponent.prototype.onDestroy.call(this);
747
+ MaskableGraphic.prototype.onDestroy.call(this);
758
748
  this.played = false;
759
749
  this.isPlayLoading = false;
760
750
  this.pendingPause = false;
@@ -766,7 +756,7 @@ var VideoComponent = /*#__PURE__*/ function(BaseRenderComponent) {
766
756
  };
767
757
  _proto.onDisable = function onDisable() {
768
758
  var _this_item;
769
- BaseRenderComponent.prototype.onDisable.call(this);
759
+ MaskableGraphic.prototype.onDisable.call(this);
770
760
  this.isVideoActive = false;
771
761
  this.pauseVideo();
772
762
  var endBehavior = (_this_item = this.item) == null ? void 0 : _this_item.endBehavior;
@@ -775,12 +765,12 @@ var VideoComponent = /*#__PURE__*/ function(BaseRenderComponent) {
775
765
  }
776
766
  };
777
767
  _proto.onEnable = function onEnable() {
778
- BaseRenderComponent.prototype.onEnable.call(this);
768
+ MaskableGraphic.prototype.onEnable.call(this);
779
769
  this.isVideoActive = true;
780
770
  this.playVideo();
781
771
  };
782
772
  return VideoComponent;
783
- }(BaseRenderComponent);
773
+ }(MaskableGraphic);
784
774
  VideoComponent = __decorate([
785
775
  effectsClass(spec.DataType.VideoComponent)
786
776
  ], VideoComponent);
@@ -789,6 +779,7 @@ var AudioPlayer = /*#__PURE__*/ function() {
789
779
  function AudioPlayer(engine, audio) {
790
780
  this.engine = engine;
791
781
  this.audioSourceInfo = {};
782
+ this.pendingOffset = 0;
792
783
  this.isSupportAudioContext = !!window["AudioContext"];
793
784
  this.options = {
794
785
  endBehavior: spec.EndBehavior.destroy,
@@ -838,30 +829,83 @@ var AudioPlayer = /*#__PURE__*/ function() {
838
829
  return ((_this_audio = this.audio) == null ? void 0 : _this_audio.currentTime) || 0;
839
830
  }
840
831
  };
841
- _proto.play = function play() {
832
+ /**
833
+ * 设置音频当前时间
834
+ * WebAudio下设置 this.pendingOffset,后面play时直接从该事件play
835
+ * HTMLAudioElement下直接设置时间
836
+ */ _proto.setCurrentTime = function setCurrentTime(time) {
837
+ var t = Math.max(0, time);
838
+ if (this.isSupportAudioContext) {
839
+ var upper = this.options.duration && this.options.duration > 0 ? this.options.duration : undefined;
840
+ this.pendingOffset = upper ? Math.min(t, upper) : t;
841
+ return;
842
+ } else {
843
+ if (this.audio) {
844
+ var dur = Number.isFinite(this.audio.duration) ? this.audio.duration : undefined;
845
+ var clamped = dur ? Math.min(t, dur) : t;
846
+ this.audio.currentTime = clamped;
847
+ }
848
+ }
849
+ };
850
+ /**
851
+ * 播放音频
852
+ */ _proto.play = function play() {
842
853
  var _this = this;
843
854
  if (this.isSupportAudioContext) {
844
- var _this_audioSourceInfo = this.audioSourceInfo, audioContext = _this_audioSourceInfo.audioContext, source = _this_audioSourceInfo.source;
845
- if (source && audioContext) {
846
- switch(this.options.endBehavior){
847
- case spec.EndBehavior.destroy:
848
- case spec.EndBehavior.freeze:
849
- source.start(0);
850
- break;
851
- case spec.EndBehavior.restart:
852
- source.loop = true;
853
- source.loopStart = 0;
854
- source.loopEnd = this.options.duration;
855
- source.start(0);
856
- break;
857
- }
855
+ var _this_audioSourceInfo = this.audioSourceInfo, audioContext = _this_audioSourceInfo.audioContext, source = _this_audioSourceInfo.source, gainNode = _this_audioSourceInfo.gainNode;
856
+ if (!audioContext || !gainNode) {
857
+ return;
858
+ }
859
+ var buffer = source == null ? void 0 : source.buffer;
860
+ if (!buffer) {
861
+ return;
862
+ }
863
+ var maxDuration = this.options.duration && this.options.duration > 0 ? this.options.duration : buffer.duration;
864
+ // 保险起见,先停掉旧 source,吞掉异常(旧节点是否成功 stop 不影响后续逻辑)
865
+ // eslint-disable-next-line no-empty
866
+ try {
867
+ source == null ? void 0 : source.stop();
868
+ } catch (e) {}
869
+ // AudioBufferSourceNode 是一次性节点,start 后不能二次 start。无论之前是否 start 过,先调用 stop 清理旧节点。
870
+ var newSource = audioContext.createBufferSource();
871
+ newSource.buffer = buffer;
872
+ newSource.connect(gainNode);
873
+ var _source_playbackRate_value;
874
+ // 继承旧的播放参数
875
+ newSource.playbackRate.value = (_source_playbackRate_value = source == null ? void 0 : source.playbackRate.value) != null ? _source_playbackRate_value : 1;
876
+ if (this.options.endBehavior === spec.EndBehavior.restart) {
877
+ newSource.loop = true;
878
+ newSource.loopStart = 0;
879
+ newSource.loopEnd = maxDuration;
880
+ } else {
881
+ var _source_loop;
882
+ newSource.loop = (_source_loop = source == null ? void 0 : source.loop) != null ? _source_loop : false;
883
+ var _source_loopStart;
884
+ newSource.loopStart = (_source_loopStart = source == null ? void 0 : source.loopStart) != null ? _source_loopStart : 0;
885
+ var _source_loopEnd;
886
+ newSource.loopEnd = (_source_loopEnd = source == null ? void 0 : source.loopEnd) != null ? _source_loopEnd : maxDuration;
887
+ }
888
+ this.audioSourceInfo.source = newSource;
889
+ if (audioContext.state === "suspended") {
890
+ audioContext.resume().catch(function(e) {
891
+ return _this.engine.renderErrors.add(e);
892
+ });
893
+ }
894
+ try {
895
+ newSource.start(0, this.pendingOffset || 0);
896
+ this.pendingOffset = 0;
897
+ this.started = true;
898
+ } catch (e) {
899
+ this.engine.renderErrors.add(e);
858
900
  }
859
- this.started = true;
860
901
  } else {
861
- var _this_audio;
862
- (_this_audio = this.audio) == null ? void 0 : _this_audio.play().catch(function(e) {
863
- _this.engine.renderErrors.add(e);
864
- });
902
+ if (this.audio) {
903
+ this.audio.loop = this.options.endBehavior === spec.EndBehavior.restart;
904
+ this.audio.play().catch(function(e) {
905
+ return _this.engine.renderErrors.add(e);
906
+ });
907
+ this.started = true;
908
+ }
865
909
  }
866
910
  };
867
911
  _proto.pause = function pause() {
@@ -965,27 +1009,41 @@ var AudioComponent = /*#__PURE__*/ function(RendererComponent) {
965
1009
  function AudioComponent() {
966
1010
  var _this;
967
1011
  _this = RendererComponent.apply(this, arguments) || this;
968
- _this.isVideoPlay = false;
969
- _this.threshold = 0.03;
1012
+ _this.isPlaying = false;
970
1013
  return _this;
971
1014
  }
972
1015
  var _proto = AudioComponent.prototype;
1016
+ _proto.onAwake = function onAwake() {
1017
+ var _this = this;
1018
+ var _this_item_composition, _this_item_composition1, _this_item_composition2;
1019
+ RendererComponent.prototype.onAwake.call(this);
1020
+ (_this_item_composition = this.item.composition) == null ? void 0 : _this_item_composition.on("play", function(option) {
1021
+ if (_this.item.time <= 0) {
1022
+ return;
1023
+ }
1024
+ _this.audioPlayer.setCurrentTime(_this.item.time);
1025
+ _this.audioPlayer.play();
1026
+ _this.isPlaying = true;
1027
+ });
1028
+ (_this_item_composition1 = this.item.composition) == null ? void 0 : _this_item_composition1.on("pause", function() {
1029
+ _this.audioPlayer.pause();
1030
+ _this.isPlaying = false;
1031
+ });
1032
+ (_this_item_composition2 = this.item.composition) == null ? void 0 : _this_item_composition2.on("goto", function(option) {
1033
+ _this.audioPlayer.setCurrentTime(_this.item.time);
1034
+ });
1035
+ };
973
1036
  _proto.onUpdate = function onUpdate(dt) {
974
1037
  RendererComponent.prototype.onUpdate.call(this, dt);
975
1038
  var _this_item = this.item, time = _this_item.time, duration = _this_item.duration, endBehavior = _this_item.endBehavior;
1039
+ if (time > 0 && this.isPlaying == false) {
1040
+ this.audioPlayer.play();
1041
+ this.isPlaying = true;
1042
+ }
976
1043
  this.audioPlayer.setOptions({
977
1044
  duration: duration,
978
1045
  endBehavior: endBehavior
979
1046
  });
980
- if (time >= 0 && !this.isVideoPlay) {
981
- this.audioPlayer.play();
982
- this.isVideoPlay = true;
983
- }
984
- if (Math.abs(time - duration) < this.threshold) {
985
- if (endBehavior === spec.EndBehavior.destroy) {
986
- this.audioPlayer.pause();
987
- }
988
- }
989
1047
  };
990
1048
  _proto.fromData = function fromData(data) {
991
1049
  RendererComponent.prototype.fromData.call(this, data);
@@ -1053,7 +1111,7 @@ AudioComponent = __decorate([
1053
1111
 
1054
1112
  /**
1055
1113
  * 插件版本号
1056
- */ var version = "2.6.6";
1114
+ */ var version = "2.7.0-alpha.0";
1057
1115
  registerPlugin("video", VideoLoader, VFXItem);
1058
1116
  registerPlugin("audio", AudioLoader, VFXItem);
1059
1117
  logger.info("Plugin multimedia version: " + version + ".");