import TRTCCloud, {translateTRTCStreamId} from '@tencentcloud/trtc-cloud-wx' const trtcCloud = TRTCCloud.getTRTCShareInstance() const trtc = trtcCloud.trtc const InterfaceEventEmitter = trtcCloud.InterfaceEventEmitter Component({ properties: { streamId: String }, data: { player: {}, TRTCStreamId: '', soundMode: '' }, lifetimes: { attached() { trtcCloud.logger.info('trtc-player attached', this.data.streamId) if (this.data.streamId) { InterfaceEventEmitter.emit('playerDomReady', {isReady: true, view: this.data.streamId}) } this.data.TRTCStreamId = this.getTRTCStreamId(this.data.streamId) this.bindTRTCCloudEvent() }, detached() { trtcCloud.logger.info('trtc-player detached', this.data.streamId) InterfaceEventEmitter.emit('playerDomReady', {isReady: false, streamId: this.data.streamId}) this.unbindTRTCCloudEvent() } }, methods: { // todo room uniapp 特供,因为 uniapp 打包导致组件传惨变量名更替 trtc-player 组建内部无法识别 setTRTCStreamId(id) { trtcCloud.logger.info('trtc-player setTRTCStreamId', id) return new Promise((resolve, reject) => { try { this.data.streamId = id this.data.TRTCStreamId = this.getTRTCStreamId(id) this.setData({streamId: this.data.streamId}, () => { trtcCloud.logger.info('trtc-player setTRTCStreamId success', id) resolve() InterfaceEventEmitter.emit('playerDomReady', {isReady: true, view: id}) }) } catch (err) { trtcCloud.logger.info('trtc-player setTRTCStreamId fail', id, err) reject(err) } }) }, playerAttributesChange(event) { const {view, playerAttributes, callback} = event trtcCloud.logger.info('trtc-player playerAttributesChange', view, JSON.stringify(playerAttributes), this.data.streamId) if (view === this.data.streamId) { this.setData({player: playerAttributes}, callback) } }, playerAudioRouteChange(event) { const { soundMode, callback } = event trtcCloud.logger.info('trtc-player playerAudioRouteChange', soundMode) this.setData({ soundMode }, callback) }, getTRTCStreamId(streamId) { const tempArray = streamId.split('_') const userId = tempArray.slice(0, -1).join('_') const streamType = Number(tempArray[tempArray.length - 1]) return translateTRTCStreamId(userId, streamType) }, bindTRTCCloudEvent() { InterfaceEventEmitter.on('playerAttributesChange', this.playerAttributesChange, this) InterfaceEventEmitter.on('playerAudioRouteChange', this.playerAudioRouteChange, this) }, unbindTRTCCloudEvent() { InterfaceEventEmitter.off('playerAttributesChange', this.playerAttributesChange) InterfaceEventEmitter.off('playerAudioRouteChange', this.playerAudioRouteChange) }, // 请保持跟 wxml 中绑定的事件名称一致 _playerStateChange(event) { trtc.playerEventHandler(event) }, _playerFullscreenChange(event) { trtc.playerFullscreenChange(event) }, _playerNetStatus(event) { trtc.playerNetStatus(event) }, _playerAudioVolumeNotify(event) { try { event.currentTarget.dataset.streamid = this.data.player.streamID trtc.playerAudioVolumeNotify(event) } catch (err) { trtcCloud.logger.warn(err) } }, } })