在 Flutter 中,获取 URL 但无法在 VideoPlayer 上显示视频并出现错误

Posted

技术标签:

【中文标题】在 Flutter 中,获取 URL 但无法在 VideoPlayer 上显示视频并出现错误【英文标题】:In Flutter, getting URL but not able to display video on VideoPlayer and getting Error 【发布时间】:2021-07-28 01:37:07 【问题描述】:

我无法在 UI 中显示视频,但我正在获取 URL,它显示一些“未处理的异常:错误状态:未来已经完成”

   List<String> videoList = [];
   
   VideoPlayerController _controller;

    @override
   void initState() 
super.initState();
getVideos(context);
  

  @override
  Future<void> dispose() async 
  super.dispose();
  if (videoList != null && mounted) 
  await _controller.dispose();

   

我要显示视频的代码:

        Container(
              height: 200.0,
              padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 15.0),
              child: ListView(
                //shrinkWrap: true,
                scrollDirection: Axis.horizontal,
                children: [
                  FutureBuilder (
                      future: hasInitialized,
                      builder: (context, snapshot)
                        if (snapshot.connectionState == ConnectionState.done) 
                          return new ListView.builder(
                            shrinkWrap: true,
                            scrollDirection: Axis.horizontal,
                              itemCount:videoList.length,
                              itemBuilder: (BuildContext context,int index)
                              return Container(
                                height: 180,
                                width: 180,
                                padding: EdgeInsets.all(10),
                                child: _controller.value.isInitialized ?

                                    Stack(
                                      children: [
                                        VideoPlayer(_controller),
                                        _ControlsOverlay(_controller),
                                        // AspectRatioVideo(_controller),
                                      ],
                                    )
                                    :Container()
                              );
                            
                            );
                         else 
                          return
                            //Text("No video");
                          Center(child: CircularProgressIndicator(),);
                        
                      
                  )
                ],
              ),

            ),

我从中获取视频网址的代码:

                Future  hasInitialized;
         Future<List<String>> getVideos(context) async 
       var document = await          FirebaseFirestore.instance.collection("users").
         doc(auth.currentUser.uid).get();
        List<String> videos = [];
      for (var item in document.data()['videos']) 
        videos.add(item);
             
       setState(() 
         _controller =    VideoPlayerController.network(videoList.toString());
        // _controller =  VideoPlayerController.network(videoList[videos]);
       _controller.addListener(() 
          setState(() );
         );
       hasInitialized = _controller.initialize();
       _controller.setLooping(false);
         _controller.initialize();
        );

      videoList= videos;
     print(videoList.length);
    print(videoList);
     return videos;
      

我得到的错误:

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理异常:错误状态:Future 已经完成 E/flutter (9697): #0 _AsyncCompleter.complete (dart:async/future_impl.dart:45:31) E/flutter (9697):#1 VideoPlayerController.initialize (package:video_player/video_player.dart:310:25) E/颤动​​(9697): E/颤动​​(9697): E/ExoPlayerImplInternal(9697):播放错误 E/ExoPlayerImplInternal(9697):com.google.android.exoplayer2.ExoPlaybackException:源错误 E/ExoPlayerImplInternal(9697):在 com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:554) E/ExoPlayerImplInternal(9697):在 android.os.Handler.dispatchMessage(Handler.java:102) E/ExoPlayerImplInternal(9697):在 android.os.Looper.loop(Looper.java:223) E/ExoPlayerImplInternal(9697):在 android.os.HandlerThread.run(HandlerThread.java:67) E/ExoPlayerImplInternal(9697):由:com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException:com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException:java.io.FileNotFoundException:[]:打开失败: ENOENT(没有这样的文件或目录) E/ExoPlayerImplInternal(9697):在 com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:97) E/ExoPlayerImplInternal(9697):在 com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:199) E/ExoPlayerImplInternal(9697):在 com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:84) E/ExoPlayerImplInternal(9697):在 com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:1013) E/ExoPlayerImplInternal(9697):在 com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:415) E/ExoPlayerImplInternal(9697):在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) E/ExoPlayerImplInternal(9697):在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) E/ExoPlayerImplInternal(9697):在 java.lang.Thread.run(Thread.java:923) E/ExoPlayerImplInternal(9697):引起:com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException:java.io.FileNotFoundException:[]:打开失败:ENOENT(没有这样的文件或目录) E/ExoPlayerImplInternal(9697):在 com.google.android.exoplayer2.upstream.FileDataSource.openLocalFile(FileDataSource.java:166) E/ExoPlayerImplInternal(9697):在 com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:89) E/ExoPlayerImplInternal(9697): ... 还有 7 个 E / ExoPlayerImplInternal(9697):引起:java.io.FileNotFoundException:[]:打开失败:ENOENT(没有这样的文件或目录) E/ExoPlayerImplInternal(9697):在 libcore.io.IoBridge.open(IoBridge.java:492) E/ExoPlayerImplInternal(9697):在 java.io.RandomAccessFile.(RandomAccessFile.java:289) E/ExoPlayerImplInternal(9697):在 java.io.RandomAccessFile.(RandomAccessFile.java:152) E/ExoPlayerImplInternal(9697):在 com.google.android.exoplayer2.upstream.FileDataSource.openLocalFile(FileDataSource.java:155) E/ExoPlayerImplInternal(9697): ... 还有 8 个 E / ExoPlayerImplInternal(9697):引起:android.system.ErrnoException:打开失败:ENOENT(没有这样的文件或目录) E/ExoPlayerImplInternal(9697):在 libcore.io.Linux.open(本机方法) E/ExoPlayerImplInternal(9697):在 libcore.io.ForwardingOs.open(ForwardingOs.java:166) E/ExoPlayerImplInternal(9697):在 libcore.io.BlockGuardOs.open(BlockGuardOs.java:254) E/ExoPlayerImplInternal(9697):在 libcore.io.ForwardingOs.open(ForwardingOs.java:166) E/ExoPlayerImplInternal(9697):在 android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7542) E/ExoPlayerImplInternal(9697):在 libcore.io.IoBridge.open(IoBridge.java:478) E/ExoPlayerImplInternal(9697): ... 11 更多 E/ExoPlayerImplInternal(9697): 播放错误

【问题讨论】:

【参考方案1】:

_controller.initialize(); 我猜会返回一个 Future。 您正在调用它两次:

hasInitialized = _controller.initialize();
_controller.setLooping(false);
_controller.initialize();

这就是你得到的原因:Future already completed

【讨论】:

删除了,还是没有区别。

以上是关于在 Flutter 中,获取 URL 但无法在 VideoPlayer 上显示视频并出现错误的主要内容,如果未能解决你的问题,请参考以下文章

在 Flutter 中,我如何使用 API 和 GetX 在 JSON 中获取数据

Flutter 无法上传从 multi_image_picker 获取的多张图片

Flutter - 无法通过 http.get 从 api 获取数据

无法在容器 Flutter Firebase 中获取 List<Text> 的值

Dart/Flutter WebSocket 服务器/客户端在同一侧无法正常工作

URL 启动器在某些 Android 设备中无法与 Flutter Web 一起使用