xinelu-doctor-app/pages/Industrialbutler/video.nvue

179 lines
4.9 KiB
Plaintext
Raw Normal View History

2023-11-08 09:10:38 +08:00
<template>
<div class="trtc-container">
<view class="trtc-video-area">
2023-11-08 16:11:43 +08:00
<view class="trtc-video-view1" id='root'>
<trtc-local-view :viewId="userId"></trtc-local-view>
2023-11-08 09:10:38 +08:00
</view>
2023-11-08 16:11:43 +08:00
<view class="trtc-video-view2">
<trtc-remote-view v-if="remoteUserId" :userId="remoteUserId" :viewId="remoteUserId">
2023-11-08 09:10:38 +08:00
</trtc-remote-view>
</view>
</view>
2023-11-08 16:11:43 +08:00
<image class="imgbtn" @click="leave" src="@/static/leave.png"></image>
2023-11-08 09:10:38 +08:00
</div>
</template>
<script>
import permision from "@/TrtcCloud/permission.js";
2023-11-08 16:11:43 +08:00
import TrtcCloud from '@/TrtcCloud/lib/index';
2023-11-08 09:10:38 +08:00
import {
TRTCAppScene,
TRTCVideoStreamType,
TRTCAudioRoute,
TRTCAudioQuality,
TRTCRoleType
} from '@/TrtcCloud/lib/TrtcDefines';
import TrtcLocalView from '@/TrtcCloud/view/TrtcLocalView';
import TrtcRemoteView from '@/TrtcCloud/view/TrtcRemoteView';
2023-11-08 16:11:43 +08:00
2023-11-08 09:10:38 +08:00
export default {
components: {
TrtcLocalView: TrtcLocalView,
TrtcRemoteView: TrtcRemoteView,
},
data() {
return {
sdkAppId: 1600006944,
userId: '',
remoteUserId: '',
userSig: '',
roomId: '',
}
},
onLoad(options) {
this.roomId = options.roomId
this.userId = options.userId
this.userName = options.userName
this.userSig = options.userSig
2023-11-08 16:11:43 +08:00
this.remoteUserId = options.remoteUserId
console.log(this.userSig, this.userId, this.roomId, this.remoteUserId)
2023-11-08 09:10:38 +08:00
},
onShow() {},
onBackPress() {
this.trtcCloud.exitRoom();
},
mounted() {
if (uni.getSystemInfoSync().platform === 'android') {
permision.requestAndroidPermission('android.permission.RECORD_AUDIO');
permision.requestAndroidPermission('android.permission.CAMERA');
}
this.join()
},
methods: {
join() {
// 创建 TRTC 的对象实例
this.trtcCloud = TrtcCloud.createInstance();
2023-11-08 16:11:43 +08:00
this.trtcCloud.on('onWarning', (res) => {
console.log('- onWarning: ', JSON.stringify(res));
});
this.trtcCloud.on('onError', (res) => {
console.log('- onError: ', JSON.stringify(res));
});
2023-11-08 09:10:38 +08:00
// 组装 TRTC 进房参数,请将 TRTCParams 中的各字段都替换成您自己的参数
// Please replace each field in TRTCParams with your own parameters
const params = {
sdkAppId: this.sdkAppId, // Please replace with your own sdkAppId
userId: this.userId, // Please replace with your own userid
roomId: this.roomId, // Please replace with your own room number
userSig: this.userSig, // Please replace with your own userSig
role: TRTCRoleType.TRTCRoleAnchor
};
// 如果您的场景是“在线直播”,请将应用场景设置为 TRTC_APP_SCENE_LIVE
// If your application scenario is a video call between several people, please use "TRTC_APP_SCENE_LIVE"
this.trtcCloud.enterRoom(params, TRTCAppScene.TRTCAppSceneVideoCall);
// 进入房间
2023-11-08 16:11:43 +08:00
this.trtcCloud.on("onEnterRoom", (result) => {
2023-11-08 09:10:38 +08:00
if (result > 0) {
2023-11-08 16:11:43 +08:00
console.log('进房成功');
2023-11-08 09:10:38 +08:00
}
});
2023-11-08 16:11:43 +08:00
// 打开摄像头预览
this.trtcCloud.startLocalPreview(true, this.userId);
//打开麦克风采集
this.trtcCloud.startLocalAudio(TRTCAudioQuality.TRTCAudioQualityDefault);
2023-11-08 09:10:38 +08:00
// 播放远端视频流
this.trtcCloud.startRemoteView(this.remoteUserId, TRTCVideoStreamType.TRTCVideoStreamTypeBig, this
.remoteUserId);
2023-11-08 16:11:43 +08:00
this.trtcCloud.on('onRemoteUserEnterRoom', (userId) => {
console.log('remote user enter room id ', userId);
2023-11-08 09:10:38 +08:00
});
// 视频状态变化通知
this.trtcCloud.on('onUserVideoAvailable', (res) => {
const {
userId,
available
} = res;
console.log('onUserVideoAvailable = ', res);
if (userId && available) {
this.remoteUserId = userId;
}
});
// 音频状态变化通知
this.trtcCloud.on('onUserAudioAvailable', (res) => {
const {
userId,
available
} = res;
console.log('onUserAudioAvailable = ', res);
if (userId && available) {
this.remoteUserId = userId;
}
});
//用户离开房间的通知
this.trtcCloud.on('onRemoteUserLeaveRoom', (res) => {
const {
userId,
reason
} = res;
console.log('remote user leave room ', userId, reason);
this.trtcCloud.stopLocalPreview();
this.trtcCloud.stopLocalAudio();
this.trtcCloud.stopRemoteView(this.remoteUserId, TRTCVideoStreamType.TRTCVideoStreamTypeBig);
this.trtcCloud.exitRoom();
this.trtcCloud.off('*');
uni.navigateBack({
delta: 1
})
});
},
leave() {
this.trtcCloud.exitRoom();
uni.navigateBack({
delta: 1
})
},
},
}
</script>
<style>
.imgbtn {
width: 130rpx;
height: 43rpx;
position: absolute;
bottom: 20rpx;
2023-11-08 16:11:43 +08:00
left: 290rpx;
z-index: 99999;
2023-11-08 09:10:38 +08:00
}
.trtc-container {
2023-11-08 16:11:43 +08:00
width: 100%;
height: 100vh;
2023-11-08 09:10:38 +08:00
}
2023-11-08 16:11:43 +08:00
.trtc-video-view1 {
2023-11-08 09:10:38 +08:00
border-width: 1.92rpx;
border-color: #BBBBBB;
2023-11-08 16:11:43 +08:00
height: 500rpx;
width: 300rpx;
2023-11-08 09:10:38 +08:00
position: fixed;
2023-11-08 16:11:43 +08:00
top: 20rpx;
right: 20rpx;
2023-11-08 09:10:38 +08:00
}
2023-11-08 16:11:43 +08:00
.trtc-video-view2 {
2023-11-08 09:10:38 +08:00
border-width: 1.92rpx;
2023-11-08 16:11:43 +08:00
border-color: #BBBBBB;
height: 70%;
width: 100%;
2023-11-08 09:10:38 +08:00
}
</style>