项目作者: adityathakurxd

项目描述 :
1 on 1 Video Call using Agora in Flutter
高级语言: Makefile
项目地址: git://github.com/adityathakurxd/Flutter1on1VideoCall.git
创建时间: 2021-05-21T12:49:12Z
项目社区:https://github.com/adityathakurxd/Flutter1on1VideoCall

开源协议:

下载


Flutter Video Call: 1 on 1 Video Call in Flutter

Presentation

Link:

Steps

Create a new Flutter Project:

Clear out code

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. const MyApp({Key? key}) : super(key: key);
  7. // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. primarySwatch: Colors.blue,
  14. ),
  15. home: const HomePage(),
  16. );
  17. }
  18. }

Define a HomePage Widget
It is a stateful widget and renders a view on screen which for now returns empty containers.

  1. class HomePage extends StatefulWidget {
  2. const HomePage({Key? key}) : super(key: key);
  3. @override
  4. _HomePageState createState() => _HomePageState();
  5. }
  6. class _HomePageState extends State<HomePage> {
  7. late int _remoteUid;
  8. @override
  9. Widget build(BuildContext context) {
  10. return Scaffold(
  11. appBar: AppBar(
  12. title: Text('Video Call in Flutter'),
  13. ),
  14. body: Stack(
  15. children: [
  16. Center(
  17. child: _renderRemoteVideo(),
  18. ),
  19. Align(
  20. alignment: Alignment.topLeft,
  21. child: Container(
  22. width: 100,
  23. height: 100,
  24. child: Center(
  25. child: _renderLocalPreview(),
  26. ),
  27. ),
  28. )
  29. ],
  30. ),
  31. );
  32. }
  33. Widget _renderLocalPreview(){
  34. return Container();
  35. }
  36. Widget _renderRemoteVideo(){
  37. if(_remoteUid != null){
  38. return Container();
  39. }
  40. else {
  41. return Text(
  42. 'User will appear here!',
  43. textAlign: TextAlign.center,
  44. );
  45. }
  46. }
  47. }

Agora

Create a Agora.io Account

Head over to: https://console.agora.io/

Create a New project

Get an App id and Temporary Token

  1. const appId = "";
  2. const token = "";

Add and import packages for integration in Flutter to pubspec.yaml

  1. agora_rtc_engine: ^4.0.1
  2. permission_handler: ^8.0.0

Import the packages to main.dart

  1. import 'package:permission_handler/permission_handler.dart';
  2. import 'package:agora_rtc_engine/rtc_engine.dart';

Define the engine RtcEngine _engine;

Initialise Agora by creating a new function and calling it in initState()

  1. @override
  2. void initState() {
  3. initForAgora();
  4. super.initState();
  5. }
  6. Future<void> initForAgora() async {
  7. await [Permission.camera, Permission.microphone].request();
  8. _engine = await RtcEngine.createWithConfig(RtcEngineConfig(appId));
  9. await _engine.enableVideo();
  10. _engine.setEventHandler(
  11. RtcEngineEventHandler(
  12. joinChannelSuccess: (String channel, int uid, int elapsed){
  13. print("Local User $uid joined");
  14. },
  15. userJoined: (int uid, int elapsed){
  16. print("Remote User $uid joined");
  17. setState(() {
  18. _remoteUid = uid;
  19. });
  20. },
  21. userOffline: (int uid, UserOfflineReason reason){
  22. print("Remote User $uid left channel");
  23. setState(() {
  24. _remoteUid = null;
  25. });
  26. }
  27. )
  28. );
  29. await _engine.joinChannel(token, "channelname", null, 0);
  30. }

Modify the views

Add imports

  1. import 'package:agora_rtc_engine/rtc_local_view.dart' as RtcLocalView;
  2. import 'package:agora_rtc_engine/rtc_remote_view.dart' as RtcRemoteView;

Modify Functions

  1. Widget _renderLocalPreview(){
  2. return RtcLocalView.SurfaceView();
  3. }
  4. Widget _renderRemoteVideo(){
  5. if(_remoteUid != null){
  6. return RtcRemoteView.SurfaceView(uid: _remoteUid,);
  7. }
  8. else {
  9. return Text(
  10. 'User will appear here!',
  11. textAlign: TextAlign.center,
  12. );
  13. }
  14. }

Phone Screenshot