agora-pusher.js 3.4 KB

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