forked from Martinzz/react-native-trtc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.js
159 lines (150 loc) · 5.27 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React, { Component, useEffect, useRef } from "react";
import TRTCEngine, {
TRTCLocalView, Scene, Role, RenderMode,
VideoRotation, TRTCRemoteView, VideoResolution, LogLevel
} from 'rn-trtc';
import {StyleSheet, View, TouchableOpacity, Text, SafeAreaView} from "react-native";
const sdkAppId = 0;
const userId = '';
const roomId = 0;
const userSig = ''
const Example = () => {
const [roomState, setRoomState] = React.useState({
linkMic: false,
remoteUsers: [],
});
const engineRef = useRef(null);
const setState = state => {
setRoomState(oldState => Object.assign({}, oldState, state));
}
useEffect(()=>{
TRTCEngine.setLogLevel(LogLevel.TRTCLogLevelInfo); // 设置日志级别
// init
const engine = TRTCEngine.create({
videoResolution: VideoResolution.TRTC_VIDEO_RESOLUTION_120_120,
videoFps: 15,
videoBitrate: 1200,
});
engine.setBeautyStyle(1); // 设置美颜风格
engine.setBeautyLevel(5); // 设置美颜级别
engine.setWhitenessLevel(1); // 设置美白级别
engineRef.current = engine;
// error event
engine.addListener("onError", (args) => {
console.error(args)
});
engine.addListener("onUserVideoAvailable", args => {
console.log(args)
const {userId, available} = args;
if(available){
setRoomState(oldState=>{
const index = oldState.remoteUsers.indexOf(userId);
if(index > -1)return oldState;
return Object.assign({}, oldState, {remoteUsers: [...oldState.remoteUsers, userId]})
})
}else{
setRoomState(oldState=>{
const index = oldState.remoteUsers.indexOf(userId);
if(index === -1)return oldState;
oldState.remoteUsers.splice(index, 1);
return Object.assign({}, oldState);
})
}
})
// enter room event
engine.addListener("onEnterRoom", (args) => {
const {result} = args;
if(result > 0){
console.log(`进房成功,总计耗时${result}ms`)
}else{
console.error(`进房失败,错误码${result}`)
}
});
// enter room
engine.enterRoom({
sdkAppId: sdkAppId, // number类型
userId: userId, // string 类型
roomId: roomId, // number 类型
userSig: userSig,
role: Role.TRTCRoleAudience, // 加入房间的角色
}, Scene.TRTC_APP_SCENE_LIVE)
return()=>{
// 清空数据
setState({
linkMic: false,
remoteUsers: [],
})
// destroy
engine.destroy();
}
},[])
const switchCamera = () => {
engineRef.current.switchCamera();
};
const onLinkMic = () => {
// 下麦
if(roomState.linkMic){
engineRef.current.switchRole(Role.TRTCRoleAudience);
engineRef.current.stopLocalAudio();
}else{ // 上麦
engineRef.current.switchRole(Role.TRTCRoleAnchor);
engineRef.current.startLocalAudio();
}
setState({linkMic: !roomState.linkMic}); // 设置显示本地预览视频
};
return(
<SafeAreaView style={styles.container}>
<View style={styles.video}>
{roomState.linkMic && <TRTCLocalView renderMode={RenderMode.TRTC_VIDEO_RENDER_MODE_FILL}
style={styles.videoContainer}/>}
{roomState.remoteUsers.map((item, index)=><TRTCRemoteView uid={item}
key={`trtc-${index}`}
renderMode={RenderMode.TRTC_VIDEO_RENDER_MODE_FILL}
style={styles.videoContainer}/>)}
</View>
<View style={styles.buttonHolder}>
<TouchableOpacity
onPress={switchCamera}
style={styles.button}>
<Text style={styles.buttonText}>切换摄像头</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={onLinkMic}
style={styles.button}>
<Text style={styles.buttonText}>{roomState.linkMic ? '下麦' : '上麦'}</Text>
</TouchableOpacity>
</View>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
video: {
flexDirection: 'row',
flexWrap:'wrap'
},
buttonHolder: {
alignItems: 'center',
flex: 1,
flexDirection: 'row',
},
videoContainer:{
height: 150,
width: 150,
margin: 10,
overflow: 'hidden',
backgroundColor:'#000'
},
button: {
paddingHorizontal: 20,
paddingVertical: 10,
backgroundColor: '#0093E9',
borderRadius: 25,
},
buttonText: {
color: '#fff',
},
});
export default Example;