|
| 1 | +--- |
| 2 | +sidebar_position: 4 |
| 3 | +--- |
| 4 | + |
| 5 | +# First App |
| 6 | + |
| 7 | +## Step 1 |
| 8 | + |
| 9 | +Create or use an existing flutter app project and add `flutter_webrtc` to your `pubspec.yaml` file |
| 10 | + |
| 11 | +```shell |
| 12 | +flutter create myapp |
| 13 | +``` |
| 14 | + |
| 15 | +- Add `flutter_webrtc` to your `pubspec.yaml` file |
| 16 | + |
| 17 | +```shell |
| 18 | +flutter pub add flutter_webrtc |
| 19 | +``` |
| 20 | + |
| 21 | +## Step 2 |
| 22 | + |
| 23 | +Setup required permissions for audio and video, link to [Project Settings](./project-settings) |
| 24 | + |
| 25 | +Using `navigator.mediaDevices.getUserMedia` to get access to the camera and microphone. |
| 26 | + |
| 27 | +you can view getUserMedia docs [here](./api-docs/get-user-media) |
| 28 | + |
| 29 | +```dart |
| 30 | +class _MyHomePageState extends State<MyHomePage> { |
| 31 | + RTCVideoRenderer? _renderer; |
| 32 | + MediaStream? _stream; |
| 33 | +
|
| 34 | + void _openCamera() async { |
| 35 | + // create and initialize renderer |
| 36 | + _renderer ??= RTCVideoRenderer(); |
| 37 | + await _renderer!.initialize(); |
| 38 | +
|
| 39 | + // |
| 40 | + try { |
| 41 | + _stream = await navigator.mediaDevices |
| 42 | + .getUserMedia({'audio': false, 'video': true}); |
| 43 | + } catch (e) { |
| 44 | + //if you get an error, please check the permissions in the project settings. |
| 45 | + print(e.toString()); |
| 46 | + } |
| 47 | +
|
| 48 | + // set the MediaStream to the video renderer |
| 49 | + _renderer!.srcObject = _stream; |
| 50 | + setState(() {}); |
| 51 | + } |
| 52 | +``` |
| 53 | + |
| 54 | +## Step 3 |
| 55 | + |
| 56 | +render the video renderer in the widget tree |
| 57 | + |
| 58 | +```dart |
| 59 | + @override |
| 60 | + Widget build(BuildContext context) { |
| 61 | + return Scaffold( |
| 62 | + appBar: AppBar( |
| 63 | + backgroundColor: Theme.of(context).colorScheme.inversePrimary, |
| 64 | + title: Text(widget.title), |
| 65 | + ), |
| 66 | + body: Center( |
| 67 | + child: SizedBox( |
| 68 | + width: 320, |
| 69 | + height: 240, |
| 70 | + // render the video renderer in the widget tree |
| 71 | + child: _renderer != null ? RTCVideoView(_renderer!) : Container(), |
| 72 | + ), |
| 73 | + ), |
| 74 | + floatingActionButton: FloatingActionButton( |
| 75 | + onPressed: _setup, |
| 76 | + tooltip: 'open camera', |
| 77 | + child: const Icon(Icons.camera_alt), |
| 78 | + ), |
| 79 | + ); |
| 80 | + } |
| 81 | +``` |
| 82 | + |
0 commit comments