使用Flutter WebRTC切换流似乎失败

rjee0c15  于 2023-03-04  发布在  Flutter
关注(0)|答案(1)|浏览(171)

我试图从正面切换到背面摄像头使用FlutterWebRTC,但无法让它工作。
我有以下几点

// 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);

如果我将try catch放在它们周围,则会出现以下错误

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'.
polhcujo

polhcujo1#

我是这样做的:

MediaStream localStream;
if (localStream != null) {
   await localStream!.getVideoTracks()[0].switchCamera();
}

相关问题