You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class _MyAppState extends State {
late RtcEngine agoraEngine; // Agora engine instance
String channelName = "ARyhan";
final GlobalKey scaffoldMessengerKey =
GlobalKey(); // Global key to access the scaffold
String token = "9088f484799548efba2ac5f987c63df9";
int uid = 0; // uid of the local user
bool _isHost =
true; // Indicates whether the user has joined as a host or audience
bool _isJoined = false; // Indicates if the local user has joined the channel
int? _remoteUid; // uid of the remote user
Future 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(const 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;
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();
}
LateError (LateInitializationError: Field 'requestPort' has not been initialized
when i try this code
`import 'dart:async';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:flutter/material.dart';
const String appId = "756d0451e027460dbb9212a2f46c141a";
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@OverRide
State createState() => _MyAppState();
}
class _MyAppState extends State {
late RtcEngine agoraEngine; // Agora engine instance
String channelName = "ARyhan";
final GlobalKey scaffoldMessengerKey =
GlobalKey(); // Global key to access the scaffold
String token = "9088f484799548efba2ac5f987c63df9";
int uid = 0; // uid of the local user
bool _isHost =
true; // Indicates whether the user has joined as a host or audience
bool _isJoined = false; // Indicates if the local user has joined the channel
int? _remoteUid; // uid of the remote user
@OverRide
void dispose() async {
await agoraEngine.leaveChannel();
super.dispose();
}
@OverRide
void initState() {
super.initState();
setupVideoSDKEngine();
}
showMessage(String message) {
scaffoldMessengerKey.currentState?.showSnackBar(SnackBar(
content: Text(message),
));
}
Future setupVideoSDKEngine() async {
// retrieve or request camera and microphone permissions
await [Permission.microphone, Permission.camera].request();
}
void join() async {
// Set channel options
ChannelMediaOptions options;
}
void leave() {
setState(() {
_isJoined = false;
_remoteUid = null;
});
agoraEngine.leaveChannel();
}
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();
}
@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: [
Radio(
value: true,
groupValue: _isHost,
onChanged: (value) => _handleRadioValueChange(value),
),
const Text('Host'),
Radio(
value: false,
groupValue: _isHost,
onChanged: (value) => _handleRadioValueChange(value),
),
const Text('Audience'),
]),
// Button Row
Row(
children: [
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
],
)),
);
}
}
`
The text was updated successfully, but these errors were encountered: