我正在flutter中实现agora视频通话,遇到了下面提到的错误。
我的代码:
import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:chat_in_english/ui/screens/dashboard/video%20broadcoast/example_actions_widget.dart';
import 'package:chat_in_english/ui/screens/dashboard/video%20broadcoast/log_sink.dart';
import 'package:chat_in_english/ui/screens/dashboard/video broadcoast/config.dart'
as config;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
/// MultiChannel Example
class JoinChannelVideo extends StatefulWidget {
/// Construct the [JoinChannelVideo]
const JoinChannelVideo({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _State();
}
class _State extends State<JoinChannelVideo> {
late final RtcEngine _engine;
bool _isReadyPreview = false;
bool isJoined = false, switchCamera = true, switchRender = true;
Set<int> remoteUid = {};
late TextEditingController _controller;
bool _isUseFlutterTexture = false;
bool _isUseAndroidSurfaceView = false;
ChannelProfileType _channelProfileType =
ChannelProfileType.channelProfileLiveBroadcasting;
@override
void initState() {
super.initState();
_controller = TextEditingController(text: 'test channel');
_initEngine();
}
@override
void dispose() {
super.dispose();
_dispose();
}
Future<void> _dispose() async {
await _engine.leaveChannel();
await _engine.release();
}
Future<void> _initEngine() async {
_engine = createAgoraRtcEngine();
await _engine.initialize(RtcEngineContext(
appId: 'b3bee66900fd4bc9af9622016796c74b',
));
_engine.registerEventHandler(RtcEngineEventHandler(
onError: (ErrorCodeType err, String msg) {
logSink.log('[onError] err: $err, msg: $msg');
},
onJoinChannelSuccess: (RtcConnection connection, int elapsed) {
logSink.log(
'[onJoinChannelSuccess] connection: ${connection.toJson()} elapsed: $elapsed');
setState(() {
isJoined = true;
});
},
onUserJoined: (RtcConnection connection, int rUid, int elapsed) {
logSink.log(
'[onUserJoined] connection: ${connection.toJson()} remoteUid: $rUid elapsed: $elapsed');
setState(() {
remoteUid.add(rUid);
});
},
onUserOffline:
(RtcConnection connection, int rUid, UserOfflineReasonType reason) {
logSink.log(
'[onUserOffline] connection: ${connection.toJson()} rUid: $rUid reason: $reason');
setState(() {
remoteUid.removeWhere((element) => element == rUid);
});
},
onLeaveChannel: (RtcConnection connection, RtcStats stats) {
logSink.log(
'[onLeaveChannel] connection: ${connection.toJson()} stats: ${stats.toJson()}');
setState(() {
isJoined = false;
remoteUid.clear();
});
},
));
await _engine.enableVideo();
await _engine.setVideoEncoderConfiguration(
const VideoEncoderConfiguration(
dimensions: VideoDimensions(width: 640, height: 360),
frameRate: 15,
bitrate: 0,
),
);
await _engine.startPreview();
setState(() {
_isReadyPreview = true;
});
}
Future<void> _joinChannel() async {
await _engine.joinChannel(
token: '007eJxTYLi8+O6xxal5ZRuW9B6yMYgKuHIoqmzu2qdf49sDMr4lxDIrMCQZJ6WmmplZGhikpZgkJVsmplmaGRkZGJqZW5olm5sk+RuXJDcEMjJ0+YgwMTJAIIjPw1CSWlyikJyRmJeXmsPAAAAB1SNY',
channelId: _controller.text,
uid: config.uid,
options: ChannelMediaOptions(
channelProfile: _channelProfileType,
clientRoleType: ClientRoleType.clientRoleBroadcaster,
),
);
}
Future<void> _leaveChannel() async {
await _engine.leaveChannel();
}
Future<void> _switchCamera() async {
await _engine.switchCamera();
setState(() {
switchCamera = !switchCamera;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ExampleActionsWidget(
displayContentBuilder: (context, isLayoutHorizontal) {
if (!_isReadyPreview) return Container();
return Stack(
children: [
AgoraVideoView(
controller: VideoViewController(
rtcEngine: _engine,
canvas: const VideoCanvas(uid: 0),
useFlutterTexture: _isUseFlutterTexture,
useAndroidSurfaceView: _isUseAndroidSurfaceView,
),
),
Align(
alignment: Alignment.topLeft,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: List.of(remoteUid.map(
(e) => SizedBox(
width: 120,
height: 120,
child: AgoraVideoView(
controller: VideoViewController.remote(
rtcEngine: _engine,
canvas: VideoCanvas(uid: e),
connection:
RtcConnection(channelId: _controller.text),
useFlutterTexture: _isUseFlutterTexture,
useAndroidSurfaceView: _isUseAndroidSurfaceView,
),
),
),
)),
),
),
)
],
);
},
actionsBuilder: (context, isLayoutHorizontal) {
final channelProfileType = [
ChannelProfileType.channelProfileLiveBroadcasting,
ChannelProfileType.channelProfileCommunication,
];
final items = channelProfileType
.map((e) => DropdownMenuItem(
child: Text(
e.toString().split('.')[1],
),
value: e,
))
.toList();
return Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: _controller,
decoration: const InputDecoration(hintText: 'Channel ID'),
),
if (!kIsWeb &&
(defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS))
Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
if (defaultTargetPlatform == TargetPlatform.iOS)
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
const Text('Rendered by Flutter texture: '),
Switch(
value: _isUseFlutterTexture,
onChanged: isJoined
? null
: (changed) {
setState(() {
_isUseFlutterTexture = changed;
});
},
)
]),
if (defaultTargetPlatform == TargetPlatform.android)
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
const Text('Rendered by Android SurfaceView: '),
Switch(
value: _isUseAndroidSurfaceView,
onChanged: isJoined
? null
: (changed) {
setState(() {
_isUseAndroidSurfaceView = changed;
});
},
),
]),
],
),
const SizedBox(
height: 20,
),
const Text('Channel Profile: '),
DropdownButton<ChannelProfileType>(
items: items,
value: _channelProfileType,
onChanged: isJoined
? null
: (v) {
setState(() {
_channelProfileType = v!;
});
},
),
const SizedBox(
height: 20,
),
Row(
children: [
Expanded(
flex: 1,
child: ElevatedButton(
onPressed: isJoined ? _leaveChannel : _joinChannel,
child: Text('${isJoined ? 'Leave' : 'Join'} channel'),
),
)
],
),
if (defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS) ...[
const SizedBox(
height: 20,
),
ElevatedButton(
onPressed: _switchCamera,
child: Text('Camera ${switchCamera ? 'front' : 'rear'}'),
),
],
],
);
},
),
);
// if (!_isInit) return Container();
}
}
我面临的错误:
注:错误消失时,我这样做了,' flutter酒吧缓存修复',但当我重新运行应用程序的错误再次出现。这是非常耗时的运行flutter酒吧缓存修复每一次运行,请有人帮助我这个。
1条答案
按热度按时间bihw5rsg1#
对于Agora视频调用,需要依赖项:-agora_rtc_engine:^5.3.1或最新版本
在-Agora Website上登录并创建项目
并按照说明如何实施Agora视频通话记录
我已经创建了一个演示的Videocall使用Agora请检查我的github代码Videocall-Flutter