Flutter: Flutter Video Player 无法播放一个视频文件TWICE [Video controller disposed后无法使用]~

Flutter: Flutter Video Player cannot play a video file TWICE [Video controller cannot be used after disposed]~

我正在为我的 flutter 应用程序使用官方 Flutter 视频播放器包,但是显示器完全是黑色的,我在屏幕上显示它时也遇到了这个错误

E/flutter (10774): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: A VideoPlayerController was used after being disposed.
E/flutter (10774): Once you have called dispose() on a VideoPlayerController, it can no longer be used.

或者有时这样:

V/LGCodecAdapter(  515): LG Codec Adapter start
I/ACodec  (  515): Now uninitialized
I/ACodec  (  515): onAllocateComponent
I/OMXClient(  515): MuxOMX ctor
I/ACodec  (  515): [OMX.qcom.video.decoder.avc] Now Loaded
I/ACodec  (  515): Now uninitialized
W/MediaCodecRenderer(  515): Failed to initialize decoder: OMX.qcom.video.decoder.avc
W/MediaCodecRenderer(  515):   android.media.MediaCodec$CodecException: Error 0xfffffff4
W/MediaCodecRenderer(  515):       at android.media.MediaCodec.native_configure(Native Method)
W/MediaCodecRenderer(  515):       at android.media.MediaCodec.configure(MediaCodec.java:1895)
W/MediaCodecRenderer(  515):       at com.google.android.exoplayer2.mediacodec.SynchronousMediaCodecAdapter.configure(SynchronousMediaCodecAdapter.java:61)
W/MediaCodecRenderer(  515):       at com.google.android.exoplayer2.video.MediaCodecVideoRenderer.configureCodec(MediaCodecVideoRenderer.java:620)
W/MediaCodecRenderer(  515):       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.initCodec(MediaCodecRenderer.java:1110)
W/MediaCodecRenderer(  515):       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.maybeInitCodecWithFallback(MediaCodecRenderer.java:1011)
W/MediaCodecRenderer(  515):       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.maybeInitCodecOrBypass(MediaCodecRenderer.java:578)
W/MediaCodecRenderer(  515):       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.onInputFormatChanged(MediaCodecRenderer.java:1420)
W/MediaCodecRenderer(  515):       at com.google.android.exoplayer2.video.MediaCodecVideoRenderer.onInputFormatChanged(MediaCodecVideoRenderer.java:694)
W/MediaCodecRenderer(  515):       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.readToFlagsOnlyBuffer(MediaCodecRenderer.java:965)
W/MediaCodecRenderer(  515):       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:811)
W/MediaCodecRenderer(  515):       at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:947)
W/MediaCodecRenderer(  515):       at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:477)
W/MediaCodecRenderer(  515):       at android.os.Handler.dispatchMessage(Handler.java:98)
W/MediaCodecRenderer(  515):       at android.os.Looper.loop(Looper.java:154)
W/MediaCodecRenderer(  515):       at android.os.HandlerThread.run(HandlerThread.java:61)
E/ExoPlayerImplInternal(  515): Playback error
E/ExoPlayerImplInternal(  515):   com.google.android.exoplayer2.ExoPlaybackException: MediaCodecVideoRenderer error, index=0, format=Format(1, null, null, video/avc, avc1.428028, -1, null, [1920, 1080, 28.391167], [-1, -1]), format_supported=YES
E/ExoPlayerImplInternal(  515):       at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:554)
E/ExoPlayerImplInternal(  515):       at android.os.Handler.dispatchMessage(Handler.java:98)
E/ExoPlayerImplInternal(  515):       at android.os.Looper.loop(Looper.java:154)
E/ExoPlayerImplInternal(  515):       at android.os.HandlerThread.run(HandlerThread.java:61)
E/ExoPlayerImplInternal(  515):   Caused by: com.google.android.exoplayer2.mediacodec.MediaCodecRenderer$DecoderInitializationException: Decoder init failed: OMX.qcom.video.decoder.avc, Format(1, null, null, video/avc, avc1.428028, -1, null, [1920, 1080, 28.391167], [-1, -1])

这是播放器的代码:

late final VideoPlayerController _videoCtrl;
late final Future<void> _videoCtrlInitializationFuture;

  @override
  void initState() {
    super.initState();

    _initVideoCtrl();
  }

  void _initVideoCtrl() {
    _videoCtrl = VideoPlayerController.file(widget.videoFile);
    _videoCtrlInitializationFuture = _videoCtrl.initialize();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _videoCtrlInitializationFuture,
      builder: (BuildContext context, AsyncSnapshot videoCtrlInitSnap) {
        if (videoCtrlInitSnap.connectionState == ConnectionState.done) {
          return AspectRatio(
            aspectRatio: _videoCtrl.value.aspectRatio,
            child: VideoPlayer(_videoCtrl),
          );
        } else {
          return SizedBox.shrink();
        }
      },
    );
  }

  @override
  void dispose() {
    _videoCtrl.dispose();

    super.dispose();
  }

我尝试使用 setState(() {}) 并向我的 VideoPlayer 小部件传递一个新的 UniqueKey() 以创建新视频,但出现错误,显示控制器无法使用dispose,虽然是完全不同的两个widget。

尝试像下面那样编辑您的 initState,并告诉我状态。

  @override
  void initState() {
    _videoCtrl = VideoEditorController.file(widget.file)
      ..initialize().then((_) => setState(() {}));
    super.initState();
  }

这个bug是由build.gradle文件(应用级别)的targetSdkVersion引起的,所以我需要添加一个额外的权限

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

在我的 manifest.xml 文件中,还将 podfile 中的 ios 版本设置为 10(或更高),完成!