I'm creating a channel from agora web demo as host and trying to join the channel from the flutter app as an audience i made sure both use the same channel name, app id, and I'm generating tokens through a server and Fech them to the app.
here's my code:
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/${0}/?expiry=7000/'),
);
if (response.statusCode == 200) {
setState(() {
_rtcToken = response.body;
_rtcToken = jsonDecode(_rtcToken!)['rtcToken'];
});
print('rtc: ' + _rtcToken!);
} else {
print('Failed to fetch the token');
}
}
}
i get this error:
#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>
- what am i doing wrong?
- do i need to set up the RTM channel/client, when I'm just wanting video streaming?
- What does the number -17 in the error mean?