How to Use Twilio Video Plugin for Android with Flutter?

· 12 min read
How to Use Twilio Video Plugin for Android with Flutter
How to Use Twilio Video Plugin for Android with Flutter

Many app developers use the platform to create impressive applications that meet clients’ demands. Flutter is the most demanding platform for many developers to create a perfect-looking app quickly. It comes up with various plugins that help developers in multiple forms. If you need to use such a technology, hire Flutter developer and gain perfect guidance to build an efficient app. Flutter allows developers to finish the project on time.

Flutter is an open-source and free toolkit to create an application that works well on the web, mobile, and desktop. You can build a flutter app that uses a Flutter package with Twilio video. Users use the app to host the call and join others.

Whether you are willing to use Twilio programmable video, you can spend time over the web and access guides. First, you must set up a programmable video demo and start the project. The platform aids you in making quality, featured, and open-source video applications.

Easy to add audio and video chat:

Twilio programmable video is helpful for flutter developers to build an app. It acts as a cloud platform and helps developers integrate audio and video chat to android, ios, and web applications. In addition, you can take pleasure from different things in a package like SDKs, REST APIs, and helper tools.

These make the process easier to distribute, capture, record, and deliver quality video, audio, and screen share. The video application needs a Twilio programmable video platform. You need to be aware of the main components to build a video application, like,

1. Twilio account:

You can create a Twilio account that is free. Once you set up an account, you will obtain the proper credential that enables you to access the Twilio service.

2. Server application:

The server application works well on the application server. It requires a Twilio account credential to permit access to the video service. Server application needs video REST API to keep a real-time communication system. Developers may download helper libraries for the video REST API and use different platforms like PHP, python, java, C#, ruby, and others.

3. Client application:

The client application is carried out the mobile or web clients and needs Twilio client SDKs to build, distribute, subscribe and render accurate time communication information. You can access Twilio video SDK in client platforms like android, ios, and javascript.

Integrate plugin properly:

The package helps developers a lot to develop video calling apps. When it comes to a new flutter project, you can build a flutter plugin. With the help of the Flutter app development company, you can handle every process without any difficulty.

Developers need to provide important information like project name, location, description, and others. On the other hand, you must select a company domain and identify the platform channel language. Finally, you can understand the following code to set up the plugin in a flutter.

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

typedef void VideoCreatedCallback(VideoController controller);

class TwilioVideoTutorial extends StatefulWidget {
  TwilioVideoTutorial({
    Key key,
    this.twilioToken,
    this.onVideoCreated,
  }) : super(key: key);
  final String twilioToken;
  final VideoCreatedCallback onVideoCreated;

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

class _TwilioVideoTutorialState extends State<TwilioVideoTutorial> {
  VideoController _controller;

  @override
  void initState() {
    super.initState();
    _controller = VideoController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: AndroidView(
          viewType: 'twilioVideoPlugin',
          onPlatformViewCreated: _onPlatformCreated,
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: FloatingActionButton(
        heroTag: null,
        backgroundColor: Colors.red.shade700,
        child: Icon(Icons.call_end, size: 32),
        onPressed: () async {
          try {
            await _controller.hangup();
            Navigator.pop(context);
          } catch (error) {
            print("Error hanging up: ${error.message}");
          }
        },
      ),
    );
  }

  void _onPlatformCreated(int id) {
    if (_onVideoCreated == null) {
      return;
    }
    _onVideoCreated();
  }

  void _onVideoCreated() {
    _controller.init(widget.twilioToken);
  }
}

class VideoController {
  MethodChannel _methodChannel = new MethodChannel("twilioVideoPlugin");

  Future<void> init(String token) {
    assert(token != null);
    return _methodChannel.invokeMethod('init', {'token': "tokentoken"});
  }

  Future<bool> hangup() {
    return _methodChannel.invokeMethod('hangup');
  }
}

Source: Github.com
Flutter utilizes a channel to initiate communication between native platforms. Therefore, Channel is ideal for sending and receiving a message between native platform and flutter. Moreover, it makes the process effective and straightforward.

Video controllers deal with all things relevant to video with the native platform. The basic container takes height and width to host video calls. Developers implement important things by considering an operating system. It is mandatory to pass the Twilio token via the plugin.

import android.content.Context
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import com.twilio.video.*
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.platform.PlatformView

class TwilioVideoTutorialView internal constructor(private var context: Context, twilioVideoTutorialPlugin: TwilioVideoTutorialPlugin, messenger: BinaryMessenger) : PlatformView, MethodCallHandler {
  private val methodChannel: MethodChannel = MethodChannel(messenger, "twilioVideoPlugin")
  // Initialize the cameraCapturer and default it to the front camera
  private val cameraCapturer: CameraCapturer = CameraCapturer(context, CameraCapturer.CameraSource.FRONT_CAMERA)
  // Create a local video track with the camera capturer
  private val localVideoTrack: LocalVideoTrack = LocalVideoTrack.create(context, true, cameraCapturer)!!

  var localParticipant: LocalParticipant? = null
  // The twilio room set up for the call
  private var room: Room? = null
  var roomName: String? = null
  // The twilio token passed through the method channel
  private var token: String? = null
  private val primaryVideoView: VideoView = VideoView(context)
  // Create the parent view, this will be used for the primary and future thumbnail video views
  private val view: FrameLayout = FrameLayout(context)

  // The tag for any logging
  val TAG = "TwilioVideoTutorial"

  override fun getView(): View {
    return view
  }

  init {
    // Initialize the method channel
    methodChannel.setMethodCallHandler(this)
    }


  private val roomListener = object : Room.Listener {
    override fun onConnected(room: Room) {
      localParticipant = room.localParticipant
      roomName = room.name
    }

    override fun onReconnected(room: Room) {
      Log.i("Reconnected", "Participant: $localParticipant")
    }

override fun onReconnecting(room: Room, twilioException: TwilioException) {
      // Send a message to the flutter ui to be displayed regarding this action
      Log.i("Reconnecting", "Participant: $localParticipant")
    }

    override fun onConnectFailure(room: Room, twilioException: TwilioException) {
      Log.e("Connection Failure Room", room.name)
      // Retry initializing the call
      init(token!!)
    }

    override fun onDisconnected(room: Room, twilioException: TwilioException?) {
      if (twilioException != null) {
        throw error("Twilio error on disconnect for room $roomName: ${twilioException.message}")
      }
      localParticipant = null
      Log.i("Disconnected", "room: $roomName")
      // Re init ui if not destroyed
    }

    override fun onParticipantConnected(room: Room, remoteParticipant: RemoteParticipant) {
      Log.i(TAG, "Participant connected")
      // Send a message to the flutter ui to be displayed regarding this action
      Log.i("Participant connected", "Participant: $remoteParticipant")
    }

    override fun onParticipantDisconnected(room: Room, remoteParticipant: RemoteParticipant) {
      // Create function to remove the remote participant properly
      Log.i("Participant disconnect", remoteParticipant.identity)
    }

    override fun onRecordingStarted(room: Room) {
      /** Will not be being implemented */
    }

    override fun onRecordingStopped(room: Room) {
      /** This will not be being implemented */
    }
  }
  

  override fun onMethodCall(methodCall: MethodCall, result: Result) {
    when (methodCall.method) {
      "init" -> {
        try {
          val callOptions: Map<*, *>? = methodCall.arguments as? Map<*, *>
          token = callOptions?.get("token") as String
          init(token!!)
        } catch (exception: Exception) {
          result.error("Twilio Initiation Error: ", "${exception.message}", exception.stackTrace)
        }
      }
      "hangup" -> hangup(result)
      else -> result.notImplemented()
    }
  }

 
  private fun init(token: String) {
    try {
      val connectOptions = ConnectOptions.Builder(token)
      localVideoTrack.let { connectOptions.videoTracks(listOf(it)) }

      room = Video.connect(context, connectOptions.build(), roomListener)
      localVideoTrack.addRenderer(primaryVideoView)
      primaryVideoView.mirror = true
      view.addView(primaryVideoView)
    } catch (exception: Exception) {
      Log.e("Initiation exception", "${exception.message}")
    }
  }


  private fun hangup(result: Result) {
    room?.disconnect()
    localVideoTrack.release()
    result.success(true)
  }

  override fun dispose() {}
}

Source: Github.com

Flutter Example:

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:twilio_video_tutorial/twilio_video_tutorial.dart';

void main() => runApp(
      MaterialApp(
        title: "Twilio Video Call Example",
        home: MyApp(),
      ),
    );

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _twilioToken;

  @override
  void initState() {
    super.initState();
  }

  Future<String> getTwilioToken() async {
    http.Response response =
        await http.post("https://9d6a95da.ngrok.io/twilio/token");
    return response.body;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.video_call),
          onPressed: () async {
            _twilioToken = await getTwilioToken();
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) =>
                    TwilioVideoTutorial(twilioToken: _twilioToken),
              ),
            );
          },
        ),
      ),
    );
  }
}

Source: Github.com

Output:

Twilio Video call example Flutter
Twilio Video call example Flutter

Twilio Video call Flutter
Twilio Video call Flutter

Attributes of Twilio flutter:

Before using the Twilio programmable video, you must understand the attributes of the package. Package perfectly into android and ios applications and aids professionals with Twilio API service. You can learn features and use the package properly to build a video calling app.

  • It brings an ideal pathway for users to send SMS programmatically.
  • Users get access to SMS relevant to the Twilio account.
  • The platform is excellent for gaining more information about every SMS sent from the account.
  • People also send Whatsapp messages quickly.

You have to learn essential matters in the Twilio video and use the package for application development. The advent of the internet allows you to gather details from ideal resources.

1. Room

Symbolize virtual space and allow users to communicate.

2. Participant

Shows the client connects to the room and the participant also connects to one room.

3. Track

Streams of bytes come up with data produced by a source like a camera or a microphone. Participants also give a track.

4. RemotePartcipant

Demonstrated rest of the clients include local participants. The package supports developers very much to add features to the app. Moreover, it is an effective means of handling participants’ connection and disconnection. So, you can feel free to speak with the Flutter Agency and get resources to start and finish the flutter project.

Conclusion:

A proper understanding of the Twilio video platform is essential for developers to create video applications with flutter. In addition, you can hire us to get the required package and its benefits for video calling applications.

At flutteragency.com, we help you integrate necessary components and add required functionalities to develop a real-time call application. So, you can stay in touch with the agency and obtain the support to complete the project.

Get a Free Project Consultation
Click Here

Leave a Reply