import TRTCCloud from '@tencentcloud/trtc-cloud-wx' const trtcCloud = TRTCCloud.getTRTCShareInstance() const trtc = trtcCloud.trtc const InterfaceEventEmitter = trtcCloud.InterfaceEventEmitter Component({ properties: { }, data: { pusher: {}, }, lifetimes: { attached() { trtcCloud.logger.info('trtc-pusher attached') this.bindTRTCCloudEvent() // 注意事件绑定应该放在 domReady 之前,因为此时出发 domReady 会执行 ready 后的后续操作依赖 bindTRTCCloudEvent InterfaceEventEmitter.emit('pusherDomReady', true) }, detached() { trtcCloud.logger.info('trtc-pusher detached') InterfaceEventEmitter.emit('pusherDomReady', false) this.unbindTRTCCloudEvent() trtcCloud.exitRoom() }, }, methods: { pusherAttributesChange(event) { const {pusher, callback} = event this.setData({pusher}, callback) }, playerAudioRouteChange(event) { const { soundMode, callback } = event trtcCloud.logger.info('trtc-player playerAudioRouteChange', soundMode) this.setData({ soundMode }, callback) }, bindTRTCCloudEvent() { InterfaceEventEmitter.on('pusherAttributesChange', this.pusherAttributesChange, this) InterfaceEventEmitter.on('playerAudioRouteChange', this.playerAudioRouteChange, this) }, unbindTRTCCloudEvent() { InterfaceEventEmitter.off('pusherAttributesChange', this.pusherAttributesChange) InterfaceEventEmitter.off('playerAudioRouteChange', this.playerAudioRouteChange) }, // 请保持跟 wxml 中绑定的事件名称一致 _pusherStateChangeHandler(event) { trtc.pusherEventHandler(event) }, _pusherNetStatusHandler(event) { trtc.pusherNetStatusHandler(event) }, _pusherErrorHandler(event) { trtc.pusherErrorHandler(event) }, _pusherBGMStartHandler(event) { trtc.pusherBGMStartHandler(event) }, _pusherBGMProgressHandler(event) { trtc.pusherBGMProgressHandler(event) }, _pusherBGMCompleteHandler(event) { trtc.pusherBGMCompleteHandler(event) }, _pusherAudioVolumeNotify(event) { if (!trtcCloud.isEnterRoom) { return } trtc.pusherAudioVolumeNotify(event) } } })