流式传输来自Firebase存储的视频

时间:2019-06-22 07:42:47

标签: firebase flutter firebase-storage

要在Flutter中播放视频,必须在init()方法中实例化其Url。 那么,如何从Firestore获取视频网址列表? 如果我以Future的形式从Firestore中检索Url,则视频播放器将不接受它,因为它仅接受String而不接受Future对象。

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:chewie/chewie.dart';


class VideoScreen extends StatefulWidget {
  VideoScreen({this.studentName});
  final String studentName;
  @override
  _VideoScreenState createState() => _VideoScreenState();
}

class _VideoScreenState extends State<VideoScreen> {

  VideoPlayerController videoPlayerController;
  @override
  void initState() {
   videoPlayerController = VideoPlayerController.network('${getU()}');

    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    final chewieController = ChewieController(
      videoPlayerController: videoPlayerController,
      aspectRatio: 3 / 2,
      autoPlay: true,
      looping: true,
    );
    final playerWidget = Chewie(
      controller: chewieController,
    );
    return playerWidget;
  }
  Future<String> getU() async {
    var document = await Firestore.instance.collection('Students')
        .document('Badri');
    var url = document.get().then((documents) {
      print('video url: ${documents['video']}');
      return documents['video'];
    }
    );
    return url;
  }
}

错误来了-

W/ykiddypijourna(28557): Accessing hidden method Landroid/media/AudioTrack;->getLatency()I (light greylist, reflection)
I/ExoPlayerImpl(28557): Init 77e7389 [ExoPlayerLib/2.9.6] [beryllium, POCO F1, Xiaomi, 28]
E/ExoPlayerImplInternal(28557): Source error.
E/ExoPlayerImplInternal(28557): com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Unable to connect to Instance of 'Future<String>'
E/ExoPlayerImplInternal(28557):     at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:281)
E/ExoPlayerImplInternal(28557):     at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83)
E/ExoPlayerImplInternal(28557):     at com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractingLoadable.load(ExtractorMediaPeriod.java:885)
E/ExoPlayerImplInternal(28557):     at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:381)
E/ExoPlayerImplInternal(28557):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/ExoPlayerImplInternal(28557):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/ExoPlayerImplInternal(28557):     at java.lang.Thread.run(Thread.java:764)
E/ExoPlayerImplInternal(28557): Caused by: java.net.MalformedURLException: no protocol: Instance of 'Future<String>'
E/ExoPlayerImplInternal(28557):     at java.net.URL.<init>(URL.java:601)
E/ExoPlayerImplInternal(28557):     at java.net.URL.<init>(URL.java:498)
E/ExoPlayerImplInternal(28557):     at java.net.URL.<init>(URL.java:447)
E/ExoPlayerImplInternal(28557):     at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.makeConnection(DefaultHttpDataSource.java:426)
E/ExoPlayerImplInternal(28557):     at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:279)
E/ExoPlayerImplInternal(28557):     ... 6 more

2 个答案:

答案 0 :(得分:0)

您写 freq1 freq2 ma phd 0 1 8 a a 1 2 9 b b 的方式对我来说有点奇怪。你可以试试吗?

getU

更改:

  • 无需Future<String> getU() async { var docRef = Firestore.instance.collection('Students').document('Badri'); var doc = await docRef.get(); return doc['video']; } 创建对文档的引用。这是一个同步操作,因为那里尚未读取任何数据。
  • 我在对await的呼叫中使用await来解开get()

答案 1 :(得分:0)

回答您的问题可能为时已晚,但是有一个非常简单的解决方案。我为即将到来的网站制作了这样的页面,其中存储了许多视频。我在这里更改了代码,使其可以使用视频播放器而不是iFrame,并且删除了很多无用的东西,例如appBar和抽屉

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:xspectre/Community.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:xspectre/Squad.dart';
import 'package:xspectre/T&C.dart';

import 'AddVideos.dart';
import 'Home.dart';
import 'JoinUs.dart';
import 'Profile.dart';
import 'help/IsPhone.dart';
class VideoPage extends StatefulWidget {
  @override
  _VideoPageState createState() => _VideoPageState();
}
List<Widget> WidgetList = [Center(child:CircularProgressIndicator(),)];

class _VideoPageState extends State<VideoPage> {
  String creator ='';
  String videos = '';
  String game = '';
  final _scrollController = ScrollController();

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

  @override
  Widget build(BuildContext context) {
    return Container(
        child: Scrollbar(
          controller: _scrollController,
          isAlwaysShown: true,
          child: SingleChildScrollView(
            controller: _scrollController,
            child: Column(
              children: WidgetList
            ),
          ),
        ),
      );
  }
  void makeWidgetList() async{

    Firestore.instance.collection("Videos").getDocuments().then((querySnapshot){
      WidgetList = [];
      List<Widget> RowWidgetList = [];
      int count = 1;
      var result;
      for (result in querySnapshot.documents) {
        if(true) {
          if (isLaptop(context)) {
            WidgetList.add(SizedBox(height: 20));
            RowWidgetList.add(
              Container(
                  height: 200,
                  width: MediaQuery
                      .of(context)
                      .size
                      .width * 0.3,
                  child: videoBox(result['Link'])
              ),
            );
            if (count < 3) {
              count += 1;
            }
            else {
              WidgetList.add(Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: RowWidgetList,
              ));
              WidgetList.add(SizedBox(height: 25.0,));
              RowWidgetList = [];
              count = 1;
              setState(() {});
            }
          }
          else{
            WidgetList.add(
              Container(
                  height: 200,
                  width: MediaQuery
                      .of(context)
                      .size
                      .width * 0.96,
                  child: videoBox(result['Link'])
              ),
            );
            WidgetList.add(
              SizedBox(height:20)
            );
          }
        }
      }
      setState(() {
      });
    });
  }

}
class videoBox extends StatefulWidget {
  String Video;
  videoBox(this.Video);
  @override
  _videoBoxState createState() => _videoBoxState(Video);
}

class _videoBoxState extends State<videoBox> {
  String Video;
  VideoPlayerController videoPlayerController;
  ChewieController chewieController;
  _videoBoxState(this.Video);
  Video
  @override
  void initState(){
     super.initState();
     videoPlayerController = VideoPlayerController.network(Video);
    final chewieController = ChewieController(
      videoPlayerController: videoPlayerController,
      aspectRatio: 3 / 2,
      autoPlay: true,
      looping: true,
      autoInitialize:true
    );
        
  }
  @override
  Widget build(BuildContext context) {
    return Container(
          child: Chewie(
             controller:chewieController,
        ),
    ) ;
  }
}
String type1 = '';
String platform = '';

@FrankVanPuffelen犯的错误是他使用getU函数返回了将来的字符串,这当然会引起问题。我们需要使用另一种明智的策略。您可以使用它来创建它。您可以在https://xspectre-9a3b3.web.app/#/上查看我的版本,然后转到视频。我还没有发布完整的代码,但是给出了您需要的