agora-player.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // components/agora-player/agora-player.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. width: {
  8. type: Number,
  9. value: 0
  10. },
  11. height: {
  12. type: Number,
  13. value: 0
  14. },
  15. x: {
  16. type: Number,
  17. value: 0
  18. },
  19. y: {
  20. type: Number,
  21. value: 0
  22. },
  23. debug: {
  24. type: Boolean,
  25. value: !1
  26. },
  27. /**
  28. * 0 - loading, 1 - ok, 2 - error
  29. */
  30. status: {
  31. type: String,
  32. value: "loading",
  33. observer: function (newVal, oldVal, changedPath) {
  34. console.log(`player status changed from ${oldVal} to ${newVal}`);
  35. }
  36. },
  37. orientation: {
  38. type: String,
  39. value: "vertical"
  40. },
  41. name: {
  42. type: String,
  43. value: ""
  44. },
  45. uid: {
  46. type: String,
  47. value: ""
  48. },
  49. url: {
  50. type: String,
  51. value: "",
  52. observer: function (newVal, oldVal, changedPath) {
  53. // 属性被改变时执行的函数(可选),也可以写成在methods段中定义的方法名字符串, 如:'_propertyChange'
  54. // 通常 newVal 就是新设置的数据, oldVal 是旧数据
  55. console.log(`player url changed from ${oldVal} to ${newVal}, path: ${changedPath}`);
  56. }
  57. }
  58. },
  59. /**
  60. * 组件的初始数据
  61. */
  62. data: {
  63. playContext: null,
  64. detached: false
  65. },
  66. /**
  67. * 组件的方法列表
  68. */
  69. methods: {
  70. /**
  71. * start live player via context
  72. * in most cases you should not call this manually in your page
  73. * as this will be automatically called in component ready method
  74. */
  75. start: function () {
  76. const uid = this.data.uid;
  77. console.log(`starting player ${uid}`);
  78. if (this.data.status === "ok") {
  79. console.log(`player ${uid} already started`);
  80. return;
  81. }
  82. if (this.data.detached) {
  83. console.log(`try to start pusher while component already detached`);
  84. return;
  85. }
  86. this.data.playContext.play();
  87. },
  88. /**
  89. * stop live pusher context
  90. */
  91. stop: function () {
  92. const uid = this.data.uid;
  93. console.log(`stopping player ${uid}`);
  94. this.data.playContext.stop();
  95. },
  96. /**
  97. * rotate video by rotation
  98. */
  99. rotate: function (rotation) {
  100. let orientation = rotation === 90 || rotation === 270 ? "horizontal" : "vertical";
  101. console.log(`rotation: ${rotation}, orientation: ${orientation}, uid: ${this.data.uid}`);
  102. this.setData({
  103. orientation: orientation
  104. });
  105. },
  106. /**
  107. * 播放器状态更新回调
  108. */
  109. playerStateChange: function (e) {
  110. this.triggerEvent('statechange', e);
  111. console.log(`live-player id: ${e.target.id}, code: ${e.detail.code}`)
  112. let uid = parseInt(e.target.id.split("-")[1]);
  113. if (e.detail.code === 2004) {
  114. console.log(`live-player ${uid} started playing`);
  115. if(this.data.status === "loading") {
  116. this.setData({
  117. status: "ok"
  118. });
  119. }
  120. } else if (e.detail.code === -2301) {
  121. console.log(`live-player ${uid} stopped`, "error");
  122. this.setData({
  123. status: "error"
  124. })
  125. }
  126. },
  127. playerNetStatus: function(e) {
  128. this.triggerEvent('netstatus', e);
  129. },
  130. },
  131. /**
  132. * 组件生命周期
  133. */
  134. ready: function () {
  135. console.log(`player ${this.data.uid} ready`);
  136. this.data.playContext || (this.data.playContext = wx.createLivePlayerContext(`player-${this.data.uid}`, this));
  137. // if we already have url when component mounted, start directly
  138. if(this.data.url) {
  139. this.start();
  140. }
  141. },
  142. moved: function () {
  143. console.log(`player ${this.data.uid} moved`);
  144. },
  145. detached: function () {
  146. console.log(`player ${this.data.uid} detached`);
  147. // auto stop player when detached
  148. this.data.playContext && this.data.playContext.stop();
  149. this.data.detached = true;
  150. }
  151. })