VideoPlayer.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import common, { AttributeUnit } from "../../../src/common/common";
  8. import CConst from "../../../src/common/CConst";
  9. import NotifierCenter from "../../../src/webtcp/NotifierCenter";
  10. import GameBase from "../../../src/common/GameBase";
  11. const { ccclass, property } = cc._decorator;
  12. @ccclass
  13. export default class VideoPlayer extends GameBase {
  14. @property({ tooltip: '播放按钮', type: cc.Node })
  15. nodeStart: cc.Node = null;
  16. @property({ tooltip: '视频资源', type: cc.VideoPlayer })
  17. video: cc.VideoPlayer = null;
  18. protected onEnable(): void {
  19. this.nodeStart.active = true;
  20. this.loadVideo();
  21. }
  22. protected onDisable(): void {
  23. this.stopVideo();
  24. }
  25. async loadVideo(){
  26. let unitCur = common.getUnitNum();
  27. let pageNum = common.getPageNum();
  28. let attribute: AttributeUnit = common.attributeMap[unitCur];
  29. let configOne = attribute.config[pageNum - 1];
  30. this.video.remoteURL = configOne.videoName;
  31. };
  32. /** 事件-点击屏幕 */
  33. eventTouch(){
  34. this.playVideo();
  35. }
  36. /** 事件-视频相关 */
  37. eventVideo(videoplayer: cc.VideoPlayer, event: cc.VideoPlayer.EventType, customEventData): void {
  38. common.log('视频:', this.getStatus(event), '; url: ', videoplayer.remoteURL);
  39. if(event === cc.VideoPlayer.EventType.PLAYING){
  40. this.nodeStart.active = false;
  41. }
  42. else if(event === cc.VideoPlayer.EventType.READY_TO_PLAY){
  43. this.playVideo();
  44. }
  45. else if (event === cc.VideoPlayer.EventType.COMPLETED) {
  46. common.setGameToLocal();
  47. NotifierCenter.trigger(CConst.EVENT_ENTER_GAME, {});
  48. }
  49. }
  50. playVideo(){
  51. if (!this.video.isPlaying()) {
  52. this.video.play();
  53. }
  54. }
  55. stopVideo(){
  56. this.video.stop();
  57. }
  58. getStatus (event) {
  59. switch (event) {
  60. case cc.VideoPlayer.EventType.PLAYING:
  61. return 'PLAYING';
  62. case cc.VideoPlayer.EventType.PAUSED:
  63. return 'PAUSED';
  64. case cc.VideoPlayer.EventType.STOPPED:
  65. return 'STOPPED';
  66. case cc.VideoPlayer.EventType.COMPLETED:
  67. return 'COMPLETED';
  68. case cc.VideoPlayer.EventType.META_LOADED:
  69. return 'META_LOADED';
  70. case cc.VideoPlayer.EventType.CLICKED:
  71. return 'CLICKED';
  72. case cc.VideoPlayer.EventType.READY_TO_PLAY:
  73. return 'READY_TO_PLAY';
  74. default:
  75. return 'NONE';
  76. }
  77. };
  78. }