首页 > 解决方案 > 使用 Flutter Web 和 Firebase 存储播放视频

问题描述

我正在尝试制作一个网站,用户可以在其中添加视频并查看这些视频。所有这些都存储在 Firebase 存储中。这就是我存储它的方式

fb.StorageReference storageRef = fb.storage().ref('videos/$chapter)');
fb.UploadTaskSnapshot uploadTaskSnapshot = await storageRef.put(videoFile, fb.UploadMetadata(contentType: 'video/mp4')).future;
await uploadTaskSnapshot.ref.getDownloadURL().then((value){
  videoUrl = value.toString();
});
snapshot.reference.collection("videos").add({
  "chapter":chapter,
   "class":grade,
  "description":description,
  "notes":notes,
  "subject":subject,
  "video":videoUrl,
  "timestamp":DateTime.now().millisecondsSinceEpoch.toString()
});

我使用普通的视频播放器插件播放它们。现在的问题是当我存储它时,我得到一个像
https://firebasestorage.googleapis.com/v0/b/studyme-me.appspot.com/o/videos%2Ff )?alt=media&token=ec4fea39-032b这样的网址-438f-8122-f8e042c1c9c7
但是flutter web中的视频播放器需要.mp4或.wav或类似的文件。我该怎么做才能让视频播放器播放这些(我认为不可能),或者我可以为此获取 .mp4 文件。也许我可以使用 firebase 存储来打开这个 url 并获取它,但我不知道
任何建议会如何得到赞赏

标签: flutterfirebase-storagevideo-player

解决方案


您可以使用VideoPlayerController插件来做到这一点

VideoPlayerController controller = VideoPlayerController.network('your-video-url',);

//Play/Pause Video:
FloatingActionButton(
  onPressed: () {
    // Wrap the play or pause in a call to `setState`. This ensures the
    // correct icon is shown
    setState(() {
      // If the video is playing, pause it.
      if (_controller.value.isPlaying) {
        _controller.pause();
      } else {
        // If the video is paused, play it.
        _controller.play();
      }
    });
  },
  // Display the correct icon depending on the state of the player.
  child: Icon(
    _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
  ),
)
'''
more examples in the link above

推荐阅读