Flutter live streaming

后端 未结 1 2040
长发绾君心
长发绾君心 2021-01-31 10:47

I am trying to build a mobile app which streams video from the camera of the device and sends it live to a server. Also, the mobile device should be able to play live video rece

1条回答
  •  天涯浪人
    2021-01-31 11:15

    For WebRTC Please try this package flutter_webrtc
    https://github.com/cloudwebrtc/flutter-webrtc
    and more example link
    https://github.com/cloudwebrtc/flutter-webrtc-demo/

    import 'package:flutter/material.dart';
    import 'package:flutter_webrtc/webrtc.dart';
    import 'dart:core';
    
    /**
     * getUserMedia sample
     */
    class GetUserMediaSample extends StatefulWidget {
      static String tag = 'get_usermedia_sample';
    
      @override
      _GetUserMediaSampleState createState() => new _GetUserMediaSampleState();
    }
    
    class _GetUserMediaSampleState extends State {
      MediaStream _localStream;
      final _localRenderer = new RTCVideoRenderer();
      bool _inCalling = false;
    
      @override
      initState() {
        super.initState();
        initRenderers();
      }
    
      @override
      deactivate() {
        super.deactivate();
        if (_inCalling) {
          _hangUp();
        }
      }
    
      initRenderers() async {
        await _localRenderer.initialize();
      }
    
      // Platform messages are asynchronous, so we initialize in an async method.
      _makeCall() async {
        final Map mediaConstraints = {
          "audio": true,
          "video": {
            "mandatory": {
              "minWidth":'640', // Provide your own width, height and frame rate here
              "minHeight": '480',
              "minFrameRate": '30',
            },
            "facingMode": "user",
            "optional": [],
          }
        };
    
        try {
          var stream = await navigator.getUserMedia(mediaConstraints);
          _localStream = stream;
          _localRenderer.srcObject = _localStream;
        } catch (e) {
          print(e.toString());
        }
        if (!mounted) return;
    
        setState(() {
          _inCalling = true;
        });
      }
    
      _hangUp() async {
        try {
          await _localStream.dispose();
          _localRenderer.srcObject = null;
        } catch (e) {
          print(e.toString());
        }
        setState(() {
          _inCalling = false;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text('GetUserMedia API Test'),
          ),
          body: new OrientationBuilder(
            builder: (context, orientation) {
              return new Center(
                child: new Container(
                  margin: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                  width: MediaQuery.of(context).size.width,
                  height: MediaQuery.of(context).size.height,
                  child: RTCVideoView(_localRenderer),
                  decoration: new BoxDecoration(color: Colors.black54),
                ),
              );
            },
          ),
          floatingActionButton: new FloatingActionButton(
            onPressed: _inCalling ? _hangUp : _makeCall,
            tooltip: _inCalling ? 'Hangup' : 'Call',
            child: new Icon(_inCalling ? Icons.call_end : Icons.phone),
          ),
        );
      }
    }
    

    0 讨论(0)
提交回复
热议问题