flutter 使用video_stream包启动摄像机导致运行时错误,应用程序因抖动而崩溃

cld4siwp  于 2023-02-25  发布在  Flutter
关注(0)|答案(2)|浏览(345)

我正在尝试直播,请遵循以下文档:https://blog.flutterflow.io/flutter-mux-live-streaming/
但是,当点击我的相机按钮启动相机预览屏幕时,我收到一个运行时错误:

[VERBOSE-3:platform_message_handler_ios.mm(106)] Check failed: task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread().
* thread #6, queue = 'com.marshalltechnology.video_stream.dispatchqueue', stop reason = signal SIGABRT
frame #0: 0x00000001d9339b38 libsystem_kernel.dylib`__pthread_kill + 8
libsystem_kernel.dylib`__pthread_kill:
->  0x1d9339b38 <+8>:  b.lo   0x1d9339b58               ; <+40>
0x1d9339b3c <+12>: pacibsp
0x1d9339b40 <+16>: stp    x29, x30, [sp, #-0x10]!
0x1d9339b44 <+20>: mov    x29, sp
Target 0: (Runner) stopped.
Lost connection to device.

我怀疑这是video_stream包的问题
当前使用这些软件包依赖项Permission HandlerVideo_stream
这是以前在旧版本上使用的
我的Mac当前运行在
Flutter稳定3.0.5,Xcode 13.4.1
下面是我的代码片段,与上面提供的文档非常相似
Main.dart

void main() async { 
  WidgetsFlutterBinding.ensureInitialized();
  
  // Get the available device cameras
  try {
    cameras = await availableCameras();
    } on CameraException catch (e) {
    debugPrint(e.toString());
  }
  runApp(const MyApp());
}

Livestream.dart

class LivestreamScreen extends StatefulWidget {

  @override
  State<LivestreamScreen> createState() => _LivestreamScreenState();
}

class _LivestreamScreenState extends State<LivestreamScreen> {
  CameraController? _controller;

  bool _isCameraPermissionGranted = false;
  bool _isCameraInitialized = false;
  bool _isInitializing = false;
  bool _isStreaming = false;
  bool _isFrontCamSelected = true;

 _getPermissionStatus() async {
 // Get the camera permission, if granted start initializing it
 await Permission.camera.request();
 var status = await Permission.camera.status;

 if (status.isGranted) {
    log('Camera Permission: GRANTED');
    setState(() {
     _isCameraPermissionGranted = true;
    });

  // Set and initialize the new camera with front camera
  // camera[0]: Back camera of the device.
  // camera[1]: Front camera of the device.
  _onNewCameraSelected(cameras[1]);
} else {
  log('Camera Permission: DENIED');
    }
} 

void _onNewCameraSelected(CameraDescription cameraDescription) async {
// Initialize a new camera
setState(() {
  _isCameraInitialized = false;
});

final previousCameraController = _controller;

final CameraController cameraController = CameraController(
  cameraDescription,
  ResolutionPreset.high,
  enableAudio: true,
  androidUseOpenGL: true,
);

await previousCameraController?.dispose();

if (mounted) {
setState(() {
    _controller = cameraController;
  });
}

_controller!.addListener(() {
  // _isStreaming = _controller!.value.isStreamingVideoRtmp;
  _isCameraInitialized = _controller!.value.isInitialized;

  if (mounted) setState(() {});
});

try {
  await cameraController.initialize();
} on CameraException catch (e) {
  log('Error initializing camera: $e');
}

    if (mounted) {
      setState(() {
      _isCameraInitialized = _controller!.value.isInitialized;
     });
   }
 } 

@override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // App state changed before we got the chance to initialize.
    if (_controller == null || !_controller!.value.isInitialized) {
      return;
    }
    if (state == AppLifecycleState.inactive) {
      _controller?.dispose();
    } else if (state == AppLifecycleState.resumed) {
      if (_controller != null) {
        _onNewCameraSelected(_controller!.description!);
      }
    }
  }   
@override
  void initState() {
    _getPermissionStatus();

    super.initState();
  }

  @override
      void dispose() {
        _controller?.dispose();

        super.dispose();
      }

 
@override
Widget build (BuildContext context) {
 return Scaffold(
appBar: AppBar{
actions: [
IconButton(
    onPressed: () {
       _isFrontCamSelected
           ? _onNewCameraSelected(cameras[0])
           : _onNewCameraSelected(cameras[1]);

       setState(() {
         _isFrontCamSelected = !_isFrontCamSelected;
       }
      );
     },
    ],
body: Stack(
  children: [
     _isCameraPermissionGranted
        ? _isCameraInitialized
          ? Stack(
             children: [
              ClipRRect( 
                child: AspectRatio(
                  aspectRatio: _controller!.value.aspectRatio,
                  child: CameraPreview(_controller!),
                ),
               ),
            // UI body code
           ],
          : Center(
              child: CircularProgressIndicator(),
            )
        : Column(
        Text(
          'Permission denied',
        ),
       ElevatedButton(
         onPressed: () {
          _getPermissionStatus();
         },
         child: Text(
           'Give permission'
           ),
          }
         )
        ],
       ),
      ],
     ),
    ),   
   }

感谢您的帮助

gcxthw6b

gcxthw6b1#

我试着把flutter降级到2.10.0来测试它是否是软件包的兼容性问题,结果确实有效。我猜想在依赖性方面有一些破坏性的变化。同时我会用另一个软件包来寻找解决办法

s3fp2yjn

s3fp2yjn2#

您可以找到一个好的RTMP发布者here
它与最新的Flutter版本兼容,并修复了您所提到的问题。

相关问题