我正在尝试加入频道,但出现 PlatformException(-17,加入频道的请求被拒绝,null,null)
i'm trying to join a channel but i get PlatformException(-17, request to join channel is rejected, null, null)
我正在从 agora web demo 创建一个频道作为主持人,并尝试从 flutter 应用程序作为观众加入频道我确保两者都使用相同的频道名称、应用程序 ID,并且我正在生成通过服务器获取令牌并将它们传送到应用程序。
这是我的代码:
import 'package:agora_rtc_engine/rtc_engine.dart';
import 'package:agora_rtc_engine/rtc_remote_view.dart' as rtc_remote_view;
import 'package:agora_rtm/agora_rtm.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import '../../utilities/agora_app_id.dart';
class StreamScreen extends StatefulWidget {
static const routeName = '/stream';
final String channelName;
final String userName;
final int userID;
const StreamScreen({
Key? key,
required this.channelName,
required this.userName,
required this.userID,
}) : super(key: key);
@override
State<StreamScreen> createState() => _StreamScreenState();
}
class _StreamScreenState extends State<StreamScreen> {
final List<int> _audience = [];
late RtcEngine _rtcEngine;
AgoraRtmClient? _rtmClient;
AgoraRtmChannel? _rtmChannel;
String? _rtmToken;
String? _rtcToken;
Future<void> initAgora() async {
_rtcEngine =
await RtcEngine.createWithContext(RtcEngineContext(agoraAppID));
_rtmClient = await AgoraRtmClient.createInstance(agoraAppID);
await getRTMToken();
await _rtcEngine.enableVideo();
await _rtcEngine.startPreview();
await _rtcEngine.setChannelProfile(ChannelProfile.LiveBroadcasting);
await _rtcEngine.setClientRole(ClientRole.Audience);
await getRTCToken();
//join the RTC & RTM Channels
await _rtcEngine.joinChannel(
_rtcToken,
widget.channelName,
null,
0,
);
//event handlers for RTC Engine
_rtcEngine.setEventHandler(RtcEngineEventHandler(
joinChannelSuccess: (channel, uid, elapsed) {
//TODO: add joinChannel logic
print('joinChannelSuccess $channel $uid');
},
userJoined: (uid, elapsed) {
print('User $uid joined');
},
leaveChannel: (rtcStats) {
setState(() {
_audience.clear();
});
},
tokenPrivilegeWillExpire: (rtcToken) async {
await getRTCToken();
await _rtcEngine.renewToken(rtcToken);
},
));
//event handlers for RTM Client
_rtmClient?.onConnectionStateChanged = (state, reason) {
print('Connection State changed: ' +
state.toString() +
', reason: ' +
reason.toString());
if (state == 5) {
_rtmChannel?.leave();
_rtmClient?.logout();
_rtmClient?.destroy();
print('logged out.');
}
};
//event handlers for RTM Channel
_rtmChannel?.onMemberJoined = (member) {
print('member joined: ' + member.userId + 'channel: ' + member.channelId);
};
_rtmChannel?.onMemberLeft = (member) {
print('member left: ' + member.userId + 'channel: ' + member.channelId);
};
}
@override
void initState() {
super.initState();
initAgora();
}
@override
void dispose() {
_audience.clear();
_rtcEngine.leaveChannel();
_rtcEngine.destroy();
_rtmChannel?.leave();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: rtc_remote_view.SurfaceView(
channelId: widget.channelName,
uid: widget.userID,
zOrderOnTop: true,
zOrderMediaOverlay: true,
),
// const Toolbar(),
),
);
}
Future<void> getRTMToken() async {
final response = await http.get(
Uri.parse(
'https://*********************.com/rtm/${widget.userID}/?expiry=7000'),
);
if (response.statusCode == 200) {
setState(() {
_rtmToken = response.body;
_rtmToken = jsonDecode(_rtmToken!)['rtmToken'];
});
print('rtm: ' + _rtmToken!);
} else {
print('Failed to fetch the token');
}
}
Future<void> getRTCToken() async {
final response = await http.get(
Uri.parse(
'https://******************.com/rtc/${widget.channelName}/audience/uid/[=10=]/?expiry=7000/'),
);
if (response.statusCode == 200) {
setState(() {
_rtcToken = response.body;
_rtcToken = jsonDecode(_rtcToken!)['rtcToken'];
});
print('rtc: ' + _rtcToken!);
} else {
print('Failed to fetch the token');
}
}
}
我收到这个错误:
#0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:177:18)
<asynchronous suspension>
#2 _StreamScreenState.initAgora (package:malhamti_app/screens/stream/stream_screen.dart:58:5)
<asynchronous suspension>
- 我做错了什么?
- 我是否需要设置 RTM channel/client,当我只是想
视频流?
- 错误中的数字-17是什么意思?
当用户已经在频道中,您尝试再次加入频道时,会出现错误码17。请务必在离开频道前调用 leaveChannel()
方法。
我正在从 agora web demo 创建一个频道作为主持人,并尝试从 flutter 应用程序作为观众加入频道我确保两者都使用相同的频道名称、应用程序 ID,并且我正在生成通过服务器获取令牌并将它们传送到应用程序。
这是我的代码:
import 'package:agora_rtc_engine/rtc_engine.dart';
import 'package:agora_rtc_engine/rtc_remote_view.dart' as rtc_remote_view;
import 'package:agora_rtm/agora_rtm.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import '../../utilities/agora_app_id.dart';
class StreamScreen extends StatefulWidget {
static const routeName = '/stream';
final String channelName;
final String userName;
final int userID;
const StreamScreen({
Key? key,
required this.channelName,
required this.userName,
required this.userID,
}) : super(key: key);
@override
State<StreamScreen> createState() => _StreamScreenState();
}
class _StreamScreenState extends State<StreamScreen> {
final List<int> _audience = [];
late RtcEngine _rtcEngine;
AgoraRtmClient? _rtmClient;
AgoraRtmChannel? _rtmChannel;
String? _rtmToken;
String? _rtcToken;
Future<void> initAgora() async {
_rtcEngine =
await RtcEngine.createWithContext(RtcEngineContext(agoraAppID));
_rtmClient = await AgoraRtmClient.createInstance(agoraAppID);
await getRTMToken();
await _rtcEngine.enableVideo();
await _rtcEngine.startPreview();
await _rtcEngine.setChannelProfile(ChannelProfile.LiveBroadcasting);
await _rtcEngine.setClientRole(ClientRole.Audience);
await getRTCToken();
//join the RTC & RTM Channels
await _rtcEngine.joinChannel(
_rtcToken,
widget.channelName,
null,
0,
);
//event handlers for RTC Engine
_rtcEngine.setEventHandler(RtcEngineEventHandler(
joinChannelSuccess: (channel, uid, elapsed) {
//TODO: add joinChannel logic
print('joinChannelSuccess $channel $uid');
},
userJoined: (uid, elapsed) {
print('User $uid joined');
},
leaveChannel: (rtcStats) {
setState(() {
_audience.clear();
});
},
tokenPrivilegeWillExpire: (rtcToken) async {
await getRTCToken();
await _rtcEngine.renewToken(rtcToken);
},
));
//event handlers for RTM Client
_rtmClient?.onConnectionStateChanged = (state, reason) {
print('Connection State changed: ' +
state.toString() +
', reason: ' +
reason.toString());
if (state == 5) {
_rtmChannel?.leave();
_rtmClient?.logout();
_rtmClient?.destroy();
print('logged out.');
}
};
//event handlers for RTM Channel
_rtmChannel?.onMemberJoined = (member) {
print('member joined: ' + member.userId + 'channel: ' + member.channelId);
};
_rtmChannel?.onMemberLeft = (member) {
print('member left: ' + member.userId + 'channel: ' + member.channelId);
};
}
@override
void initState() {
super.initState();
initAgora();
}
@override
void dispose() {
_audience.clear();
_rtcEngine.leaveChannel();
_rtcEngine.destroy();
_rtmChannel?.leave();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: rtc_remote_view.SurfaceView(
channelId: widget.channelName,
uid: widget.userID,
zOrderOnTop: true,
zOrderMediaOverlay: true,
),
// const Toolbar(),
),
);
}
Future<void> getRTMToken() async {
final response = await http.get(
Uri.parse(
'https://*********************.com/rtm/${widget.userID}/?expiry=7000'),
);
if (response.statusCode == 200) {
setState(() {
_rtmToken = response.body;
_rtmToken = jsonDecode(_rtmToken!)['rtmToken'];
});
print('rtm: ' + _rtmToken!);
} else {
print('Failed to fetch the token');
}
}
Future<void> getRTCToken() async {
final response = await http.get(
Uri.parse(
'https://******************.com/rtc/${widget.channelName}/audience/uid/[=10=]/?expiry=7000/'),
);
if (response.statusCode == 200) {
setState(() {
_rtcToken = response.body;
_rtcToken = jsonDecode(_rtcToken!)['rtcToken'];
});
print('rtc: ' + _rtcToken!);
} else {
print('Failed to fetch the token');
}
}
}
我收到这个错误:
#0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:177:18)
<asynchronous suspension>
#2 _StreamScreenState.initAgora (package:malhamti_app/screens/stream/stream_screen.dart:58:5)
<asynchronous suspension>
- 我做错了什么?
- 我是否需要设置 RTM channel/client,当我只是想 视频流?
- 错误中的数字-17是什么意思?
当用户已经在频道中,您尝试再次加入频道时,会出现错误码17。请务必在离开频道前调用 leaveChannel()
方法。