Index
zoomなどのweb会議システムを簡単に作れるように便利な機能が詰まったオープンソースであるwebrtcをflutter用のプラグインにしてくれたもの。
KDDIの記事がわかりやすいと思います。
flutter_webrtcの詳細はこちら
今回作ったサンプルの動作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_webrtc/flutter_webrtc.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { MediaStream? _localStream; final RTCVideoRenderer _localRenderer = RTCVideoRenderer(); bool _isCalling = false; DesktopCapturerSource? selected_source_; @override void initState() { super.initState(); initRrenderers(); } @override void deactivate() { super.deactivate(); if (_isCalling) { _stop(); } _localRenderer.dispose(); } Future<void> initRrenderers() async { await _localRenderer.initialize(); } Future<void> selectScreenSourceDialog(BuildContext context) async { await _makeCall(null); } Future<void> _makeCall(DesktopCapturerSource? source) async { setState(() { selected_source_ = source; }); try { var stream = await navigator.mediaDevices.getDisplayMedia(<String, dynamic>{ 'video': selected_source_ == null ? true : { 'devideId': {'exact': selected_source_!.id}, 'mandatory': {'frameRate': 30.0} } }); stream.getVideoTracks()[0].onEnded = () { print( 'By adding a listener on onEnded you can:1) catch stop video sharing on Web'); }; _localStream = stream; _localRenderer.srcObject = _localStream; } catch (e) { print(e.toString()); } if (!mounted) return; setState(() { _isCalling = true; }); } Future<void> _stop() async { try { if (kIsWeb) { _localStream?.getAudioTracks().forEach((track) => track.stop()); } await _localStream?.dispose(); _localStream = null; _localRenderer.srcObject = null; } catch (e) { print(e.toString()); } } Future<void> _hungup() async { await _stop(); setState(() { _isCalling = false; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Container( width: MediaQuery.of(context).size.width, color: Colors.white10, child: Stack( fit: StackFit.expand, children: <Widget>[ if (_isCalling) Container( margin: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0), width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.height, decoration: const BoxDecoration(color: Colors.black54), child: RTCVideoView(_localRenderer), ), ], ), ), ), floatingActionButton: FloatingActionButton( onPressed: () { _isCalling ? _hungup() : selectScreenSourceDialog(context); }, tooltip: _isCalling ? 'hungup' : 'Call', child: Icon(_isCalling ? Icons.call_end : Icons.phone), ), ); } } |