如何从flutter中的firebase获取视频列表[已关闭]

ccgok5k5  于 2023-10-22  发布在  Flutter
关注(0)|答案(1)|浏览(104)

已关闭,此问题需要details or clarity。它目前不接受回答。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

昨天关门了。
Improve this question
我想从firebase使用url获取视频,但我无法做到这一点,在扑谁可以帮助我
如何使用flutter中的URL从firebase中获取视频我想在我们的flutter应用程序中使用firebase URL显示视频

elcex8rz

elcex8rz1#

以下是视频列表的示例。

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

 class VideoListScreen extends StatelessWidget {
 @override
  Widget build(BuildContext context) {
  return Scaffold(
  appBar: AppBar(
    title: Text('Video List'),
  ),
  body: StreamBuilder<QuerySnapshot>(
    stream: FirebaseFirestore.instance.collection('videos').snapshots(),
    builder: (context, snapshot) {
      if (!snapshot.hasData) {
        return CircularProgressIndicator();
      }
      final videoList = snapshot.data.docs;

      return ListView.builder(
        itemCount: videoList.length,
        itemBuilder: (context, index) {
          final videoData = videoList[index];
          final videoUrl = videoData['video_url'];

          return ListTile(
            title: Text(videoData['title']),
            onTap: () {
              // Play the list of videos
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) {
                    return VideoPlayerScreen(videoList, index);
                  },
                ),
              );
            },
          );
        },
      );
    },
  ),
  );
  }

}

class VideoPlayerScreen extends StatefulWidget {
 final List<DocumentSnapshot> videoList;
 final int startIndex;

 VideoPlayerScreen(this.videoList, this.startIndex);

 @override
_VideoPlayerScreenState createState() => _VideoPlayerScreenState();
}

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
VideoPlayerController _videoController;
int currentIndex;

@override
void initState() {
super.initState();
currentIndex = widget.startIndex;
_initializeVideoPlayer(currentIndex);
}

void _initializeVideoPlayer(int index) {
final videoUrl = widget.videoList[index]['video_url'];
_videoController = VideoPlayerController.network(videoUrl)
  ..initialize().then((_) {
    setState(() {});
    _videoController.play();
    _videoController.addListener(() {
      if (_videoController.value.position >= _videoController.value.duration) {
        // Play the next video in the list
        if (currentIndex < widget.videoList.length - 1) {
          currentIndex++;
          _videoController.dispose();
          _initializeVideoPlayer(currentIndex);
        }
      }
    });
  });
  }

@override
void dispose() {
_videoController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text('Video Playlist'),
  ),
  body: Center(
    child: _videoController.value.isInitialized
        ? AspectRatio(
            aspectRatio: _videoController.value.aspectRatio,
            child: VideoPlayer(_videoController),
          )
        : CircularProgressIndicator(),
  ),
);
}

}

相关问题