Flutter在另一个隔离中初始化视频

时间:2018-09-16 15:12:30

标签: flutter

我有4个视频,并在应用启动时播放第一个视频。在播放第一个视频时,我想初始化其他三个视频,以便顺利播放。我在同一个隔离中初始化所有四个视频,然后有时UI冻结。因此,为避免出现这种情况,我想在其他隔离区(不是主隔离区)中初始化其他三个视频,以使UI不会冻结。我这样尝试过。

代码:

  VideoPlayerController _firstController;
  VideoPlayerController _secondController;
  VideoPlayerController _thirdController;
  VideoPlayerController _fourthController;
  static List<VideoPlayerController> _controllers;
  List<String> urls = [url1, url2, url3, url4];

  @override
  void initState() {
     _controllers = [
      _firstController,
      _secondController,
      _thirdController,
      _fourthController,
    ];
    _initVideo();
    _initFirstThree();
    super.initState();
  }

 _initVideo() async {
    _controllers[0] = new VideoPlayerController.network(urls[0]);
    await _controllers[0].initialize();
    await _controllers[0].setLooping(true);
    await _controllers[0].play();
    setState(() {});
  }

 _initFirstThree() async {
    for (int i = 1; i < urls.length; i++) {
        await initializeVideo(i, urls[i]);
    }
  }

Future initializeVideo(int index, String url) async {
    final ReceivePort receivePort = ReceivePort();
    await Isolate.spawn(isolateInit, receivePort.sendPort);

    final SendPort sendPort = await receivePort.first;
    final VideoPlayerController result = await sendReceive(sendPort, url);

    _controllers[index] = result;
    await result.dispose(); 
  }

static isolateInit(SendPort sendPort) async {
    final ReceivePort receivePort = ReceivePort();
    sendPort.send(receivePort.sendPort);

    final msg = await receivePort.first;

      final String url = msg[0];
      final SendPort replyPort = msg[1];

      final VideoPlayerController controller = new VideoPlayerController.network(url);
      await controller.initialize();
      await controller.setLooping(true);

      replyPort.send(controller);

  }

  Future sendReceive(SendPort send, message) {
    final ReceivePort receivePort = ReceivePort();
    send.send([message, receivePort.sendPort]);
    return receivePort.first;
  }

但是在第一个视频成功初始化之后,那些异常就会出现。
这是抖动错误还是我错过了什么?
我整天都在挣扎,请帮助我,任何帮助都将不胜感激!

  

[VERBOSE-2:dart_isolate.cc(813)]隔离(56689054)'main.dart:isolateInit()'退出并出现错误
  [VERBOSE-2:shell.cc(181)] Dart错误:未处理的异常:   NoSuchMethodError:方法'addObserver'在null上调用。
  接收者:null   尝试调用:addObserver(“ _ VideoAppLifeCycleObserver”的实例)    Object.noSuchMethod(dart:core / runtime / libobject_patch.dart:48:5)
   _VideoAppLifeCycleObserver.initialize(package:video_player / video_player.dart:398:29)
   VideoPlayerController.initialize(package:video_player / video_player.dart:187:24)
     _SwiperScreenState。孤立初始化(package:komama / video_swiper.dart:115:24)       _startIsolate。 (dart:isolate / runtime / libisolate_patch.dart:292:17)    _RawReceivePortImpl._handleMessage(dart:isolate / runtime / libisolate_patch.dart:171:12)

0 个答案:

没有答案