0

I'm trying to switch from the front to back camera using Flutter WebRTC but cannot get it working.

I have the following

      // Stop the current stream and remove the tracks
      await Future.forEach(videoStream!.getVideoTracks(), (MediaStreamTrack? track) async {
        if (track != null) {
          try {
            await track.stop();
            await videoStream!.removeTrack(track);
          } catch (e) {
            if (kDebugMode) {
              print(e);
            }
          }
        }
      });

      videoStream!.getVideoTracks().forEach((track) {
        track.stop();
        videoStream!.removeTrack(track, removeFromNative: true);
      });

      final mediaConstraints = {
        'audio': false, // NO need to capture audio again
        'video': {
          'deviceId': videoInputDeviceId,
        }
      };
      MediaStream newStream = await navigator.mediaDevices.getUserMedia(mediaConstraints);

      final newTrack = newStream.getVideoTracks()[0];
      await videoStream!.addTrack(newTrack, addToNative: true);

Getting the following errors if I place try catch around them

flutter: PlatformException(mediaStreamRemoveTrack: Track is nil, null, null, null)
flutter: !--- Event: Failed to enable webcam
flutter: Concurrent modification during iteration: Instance(length:0) of '_GrowableList'.
Oliver Dixon
  • 7,012
  • 5
  • 61
  • 95

1 Answers1

0

i have done it like this:

MediaStream localStream;
if (localStream != null) {
   await localStream!.getVideoTracks()[0].switchCamera();
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 07 '23 at 00:31