本文档主要讲解了如何使用融云 RTC Wrapper Plugin,基于 融云 iOS/Android 平台的 RTCLib SDK
融云官网申请开发者账号
通过管理后台的 "基本信息"->"App Key" 获取 AppKey
通过管理后台的 "IM 服务"—>"API 调用"->"用户服务"->"获取 Token",通过用户 id 获取 IMToken
管理后台的 "音视频服务"->"服务设置" 开通音视频 RTC 3.0 ,开通两个小时后生效
在项目的 pubspec.yaml
中写如下依赖
dependencies:
flutter:
sdk: flutter
rongcloud_rtc_wrapper_plugin: 5.12.0
iOS 需要在 Info.plist 中需要加入对相机和麦克风的权限申请
<key>NSCameraUsageDescription</key>
<string>使用相机</string>
<key>NSMicrophoneUsageDescription</key>
<string>使用麦克风</string>
还需要添加字段 io.flutter.embedded_views_preview
值为 YES
Android 需要在 AndroidManifest.xml 文件中声明对相机和麦克风的权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
RongIMClient.init(RongAppKey);
RongIMClient.connect(IMToken, (code, userId) {
});
engine = await RCRTCEngine.create();
// 设置加入 RTC 房间事件监听
engine.onRoomJoined = (int code, String? message) {
};
// 加入 RTC 房间
RCRTCRoomSetup setup = RCRTCRoomSetup.create(
type: RCRTCMediaType.audio_video,
role: RCRTCRole.meeting_member,
);
engine.joinRoom(id, setup);
引擎默认开启音频采集
engine.enableMicrophone(true);
engine.enableCamera(true);
RCRTCView view = await RCRTCView.create();
engine.setLocalView(view);
engine.publish(RCRTCMediaType.audio_video);
当用户加入的时候,不要做订阅渲染的处理
,因为此时该用户可能刚加入房间成功,但是尚未发布资源
engine.onUserJoined = (roomId, userId) {
};
engine.onRemotePublished = (roomId, userId, type) {
};
engine.subscribe(userId, type);
RCRTCView view = await RCRTCView.create();
engine.setRemoteView(userId, view);
// 设置加入 RTC 房间事件监听
engine.onRoomJoined = (int code, String? message) {
};
// 加入 RTC 房间
RCRTCRoomSetup setup = RCRTCRoomSetup.create(
type: RCRTCMediaType.audio_video,
role: RCRTCRole.live_broadcaster,
);
engine.joinRoom(id, setup);
引擎默认开启音频采集
engine.enableMicrophone(true);
engine.enableCamera(true);
RCRTCView view = await RCRTCView.create();
engine.setLocalView(view);
engine.publish(RCRTCMediaType.audio_video);
当主播加入的时候,不要做订阅渲染的处理
,因为此时该主播可能刚加入房间成功,但是尚未发布资源
engine.onUserJoined = (roomId, userId) {
};
engine.onRemotePublished = (roomId, userId, type) {
};
engine.subscribe(userId, type);
RCRTCView view = await RCRTCView.create();
engine.setRemoteView(userId, view);
// 设置加入 RTC 房间事件监听
engine.onRoomJoined = (int code, String? message) {
};
// 加入 RTC 房间
RCRTCRoomSetup setup = RCRTCRoomSetup.create(
type: RCRTCMediaType.audio_video,
role: RCRTCRole.live_audience,
);
engine.joinRoom(id, setup);
engine.onRemoteLiveMixPublished = (type) {
};
engine.subscribeLiveMix(type);
RCRTCView view = await RCRTCView.create();
engine.setLiveMixView(view);
// 设置加入 RTC 房间事件监听
engine.onRoomJoined = (int code, String? message) {
};
// 加入 RTC 房间
RCRTCRoomSetup setup = RCRTCRoomSetup.create(
type: RCRTCMediaType.audio_video,
role: RCRTCRole.live_audience,
);
engine.joinRoom(id, setup);
当主播加入的时候,不要做订阅渲染的处理
,因为此时该主播可能刚加入房间成功,但是尚未发布资源
engine.onUserJoined = (roomId, userId) {
};
engine.onRemotePublished = (roomId, userId, type) {
};
engine.subscribe(userId, type);
RCRTCView view = await RCRTCView.create();
engine.setRemoteView(userId, view);
engine.leaveRoom();
engine.destroy();