在我的flutter应用程序中使用Agora rtc引擎时遇到问题

tktrz96b  于 2022-11-17  发布在  Flutter
关注(0)|答案(1)|浏览(172)

我在使用agora rtc引擎时遇到了麻烦。每次我启动项目时都会收到这个错误消息
https://pastebin.com/DuniUQYd
我不知道问题出在哪里,因为错误信息对我来说不是很清楚。在代码中,我还发布了我正在使用的频道名称app Id和令牌,我也生成了它们
下面也是代码

import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

class LiveStreaming extends StatefulWidget {
 const LiveStreaming({Key? key}) : super(key: key);

 @override
 _LiveStreamingState createState() => _LiveStreamingState();
}

class _LiveStreamingState extends State<LiveStreaming> {
 String appId = '4235f9d46dd349e0b9df3ad2ea011b7d';
 String channelName = "ajanba";
 String token = "007eJxTYHCe9kzt0Gtu0ZfqVv99H1YoVC06Yltmoth98HnQfaFEEX4FBhMjY9M0yxQTs5QUYxPLVIMky5Q048QUo9REA0PDJPOUtXmJyQ2BjAxxB+cyMzJAIIjPxpCYlZiXlMjAAABaqB+p";

 int uid = 0; // uid of the local user

 int? _remoteUid; // uid of the remote user
 bool _isJoined = false; // Indicates if the local user has joined the channel
 bool _isHost =
     true; // Indicates whether the user has joined as a host or audience
 late RtcEngine agoraEngine; // Agora engine instance

 final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey =
     GlobalKey<ScaffoldMessengerState>(); // Global key to access the scaffold

 showMessage(String message) {
   scaffoldMessengerKey.currentState?.showSnackBar(SnackBar(
     content: Text(message),
   ));
 }

 @override
 void initState() {
   super.initState();
   // Set up an instance of Agora engine
   setupVideoSDKEngine();
 }

 Future<void> setupVideoSDKEngine() async {
   // retrieve or request camera and microphone permissions
   await [Permission.microphone, Permission.camera].request();

   //create an instance of the Agora engine
   agoraEngine = createAgoraRtcEngine();
   await agoraEngine.initialize(RtcEngineContext(appId: appId));

   await agoraEngine.enableVideo();

   // Register the event handler
   agoraEngine.registerEventHandler(
     RtcEngineEventHandler(
       onJoinChannelSuccess: (RtcConnection connection, int elapsed) {
         showMessage(
             "Local user uid:${connection.localUid} joined the channel");
         setState(() {
           _isJoined = true;
         });
       },
       onUserJoined: (RtcConnection connection, int remoteUid, int elapsed) {
         showMessage("Remote user uid:$remoteUid joined the channel");
         setState(() {
           _remoteUid = remoteUid;
         });
       },
       onUserOffline: (RtcConnection connection, int remoteUid,
           UserOfflineReasonType reason) {
         showMessage("Remote user uid:$remoteUid left the channel");
         setState(() {
           _remoteUid = null;
         });
       },
     ),
   );
 }

 void join() async {
   // Set channel options
   ChannelMediaOptions options;

   // Set channel profile and client role
   if (_isHost) {
     options = const ChannelMediaOptions(
       clientRoleType: ClientRoleType.clientRoleBroadcaster,
       channelProfile: ChannelProfileType.channelProfileLiveBroadcasting,
     );
     await agoraEngine.startPreview();
   } else {
     options = const ChannelMediaOptions(
       clientRoleType: ClientRoleType.clientRoleAudience,
       channelProfile: ChannelProfileType.channelProfileLiveBroadcasting,
     );
   }

   await agoraEngine.joinChannel(
     token: token,
     channelId: channelName,
     options: options,
     uid: uid,
   );
 }

 @override
 void dispose() async {
   await agoraEngine.leaveChannel();
   super.dispose();
 }

 void leave() {
   setState(() {
     _isJoined = false;
     _remoteUid = null;
   });
   agoraEngine.leaveChannel();
 }

 @override
 Widget build(BuildContext context) {
   return MaterialApp(
     scaffoldMessengerKey: scaffoldMessengerKey,
     home: Scaffold(
         appBar: AppBar(
           title: const Text('Get started with Interactive Live Streaming'),
         ),
         body: ListView(
           padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
           children: [
             // Container for the local video
             Container(
               height: 240,
               decoration: BoxDecoration(border: Border.all()),
               child: Center(child: _videoPanel()),
             ),
             // Radio Buttons
             Row(children: <Widget>[
               Radio<bool>(
                 value: true,
                 groupValue: _isHost,
                 onChanged: (value) => _handleRadioValueChange(value),
               ),
               const Text('Host'),
               Radio<bool>(
                 value: false,
                 groupValue: _isHost,
                 onChanged: (value) => _handleRadioValueChange(value),
               ),
               const Text('Audience'),
             ]),
             // Button Row
             Row(
               children: <Widget>[
                 Expanded(
                   child: ElevatedButton(
                     child: const Text("Join"),
                     onPressed: () => {join()},
                   ),
                 ),
                 const SizedBox(width: 10),
                 Expanded(
                   child: ElevatedButton(
                     child: const Text("Leave"),
                     onPressed: () => {leave()},
                   ),
                 ),
               ],
             ),
             // Button Row ends
           ],
         )),
   );
 }

 Widget _videoPanel() {
   if (!_isJoined) {
     return const Text(
       'Join a channel',
       textAlign: TextAlign.center,
     );
   } else if (_isHost) {
     // Local user joined as a host
     return AgoraVideoView(
       controller: VideoViewController(
         rtcEngine: agoraEngine,
         canvas: VideoCanvas(uid: uid),
       ),
     );
   } else {
     // Local user joined as audience
     if (_remoteUid != null) {
       return AgoraVideoView(
         controller: VideoViewController.remote(
           rtcEngine: agoraEngine,
           canvas: VideoCanvas(uid: _remoteUid),
           connection: RtcConnection(channelId: channelName),
         ),
       );
     } else {
       return const Text(
         'Waiting for a host to join',
         textAlign: TextAlign.center,
       );
     }
   }
 }

// Set the client role when a radio button is selected
 void _handleRadioValueChange(bool? value) async {
   setState(() {
     _isHost = (value == true);
   });
   if (_isJoined) leave();
 }
}
xbp102n0

xbp102n01#

我刚刚完成了我的项目与agora所以也许我可以帮助你,但错误消息链接你粘贴与上述问题是不工作,请尽快更新它快速解决

相关问题