【问题标题】:fetch video from firebase从 firebase 获取视频
【发布时间】:2021-05-25 23:28:00
【问题描述】:

我正在尝试直接从 firebase 云流式传输视频,并且我设法在控制台中打印了视频 URL,但是当我尝试将值分配给 videoplayercontroller 时,我得到的只是空白屏幕。下面是代码:

class VideoApp extends StatefulWidget {
      @override
      _VideoAppState createState() => _VideoAppState();
    }
    
    class _VideoAppState extends State<VideoApp> {
      VideoPlayerController _controller;
      Future<void> _initializeVideoPlayerFuture;
      String newURL;
    
      Future<String> getVideos() async {
        final videos = await FirebaseFirestore.instance.collection('videos').get();
    
        for (var video in videos.docs) {
          final newUrl = video.get('url');
          final name = video.get('name');
          print(name);
          newURL =await newUrl;
        }
    print(newURL);
        return newURL;
      }
    
    
      @override
      void initState() {
        getVideos();
        try {
          _controller = VideoPlayerController.network('https://www.youtube.com/watch?v=wUjPXXTeTFo'
             );
          _initializeVideoPlayerFuture = _controller.initialize();
          _controller.setLooping(true);
          _controller.setVolume(1);
          super.initState();
        } catch (e) {
          print(e);
        }
      }
    
      @override
      void dispose() {
        super.dispose();
        _controller.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            title: 'Video Demo',
            home: Scaffold(
              appBar: AppBar(
                title: Text("Video Demo"),
              ),
              body: FutureBuilder(
                future: _initializeVideoPlayerFuture,
                builder: (context, snapshot) {
                  if (snapshot.connectionState == ConnectionState.done) {
                    return Center(
                      child: AspectRatio(
                        aspectRatio: _controller.value.aspectRatio,
                        child: VideoPlayer(_controller),
                      ),
                    );
                  } else {
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  }
                },
              ),
              floatingActionButton: FloatingActionButton(
                onPressed: () {
                  setState(() {
                    if (_controller.value.isPlaying) {
                      _controller.pause();
                    } else {
                      _controller.play();
                    }
                  });
                },
                child: Icon(
                    _controller.value.isPlaying ? Icons.pause : Icons.play_arrow),
              ),
            ));
      }
    }

【问题讨论】:

    标签: flutter android-studio google-cloud-firestore firebase-authentication


    【解决方案1】:

    你不能直接播放YouTube视频,请看下面link

    虽然您可以使用Youtube Explode 包进行变通以从 Youtube 获取重定向并获取真实视频 url 的位置,但这是我在我的应用程序中使用的一个示例,尽管我是从这个论坛中搜索的:

    Future<String> extractVideoUrl(String url) async {
        final extractor = YoutubeExplode();
        final videoId = convertUrlToId(url);
        final streamManifest =
            await extractor.videos.streamsClient.getManifest(videoId);
        final streamInfo = streamManifest.muxed.withHighestBitrate();
        extractor.close();
        return streamInfo.url.toString();
      }
    
    
    String convertUrlToId(String url, {bool trimWhitespaces = true}) {
        assert(url?.isNotEmpty ?? false, 'Url cannot be empty');
        String _url;
        if (!url.contains('http') && (url.length == 11)) return url;
        if (trimWhitespaces) {
          _url = url.trim();
        } else {
          _url = url;
        }
    
        for (final exp in [
          RegExp(
              r'^https:\/\/(?:www\.|m\.)?youtube\.com\/watch\?v=([_\-a-zA-Z0-9]{11}).*$'),
          RegExp(
              r'^https:\/\/(?:www\.|m\.)?youtube(?:-nocookie)?\.com\/embed\/([_\-a-zA-Z0-9]{11}).*$'),
          RegExp(r'^https:\/\/youtu\.be\/([_\-a-zA-Z0-9]{11}).*$')
        ]) {
          final Match match = exp.firstMatch(_url);
          if (match != null && match.groupCount >= 1) return match.group(1);
        }
    
        return null;
      }
    

    获得网址后,您就可以流式传输视频了。

    【讨论】:

    • 您好,先生!我希望你做得很好,实际上我已经在堆栈上发布了 2 -3 个查询,但没有人回复。我需要你的帮助,你是唯一帮助我解决我的问题的人,我是新手,请帮帮我。我会等待你的回应。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 2016-09-28
    • 2013-02-13
    • 2012-10-05
    • 2011-05-21
    • 1970-01-01
    相关资源
    最近更新 更多