ChilledVibes
ChilledVibes

Reputation: 41

How do I fetch firebase storage video url to video player?

I'm trying to make an app that have an upload video file (like youtube form when want to upload a video).And I want to add a video player for preview or thumbnail but when I tried with firebase storage url from upload a video from camera or file it doesn't work but when I put the video url value manually from firebase storage it works.(the pick & upload to firebase works)

Video Player code:

VideoPlayerController? _controller;
Future<void>? _initializeVideoPlayerFuture;
String? videoUrl;

@override
void dispose() {
// TODO: implement dispose
_controller!.dispose();
super.dispose();
}

@override
void initState() {
// TODO: implement initState
super.initState();
getUserData();
_controller = VideoPlayerController.network("$videoUrl");
_initializeVideoPlayerFuture = _controller!.initialize();
_controller!.setLooping(true);
_controller!.setVolume(1.0);
}

Pick Video & Upload to Firebase:

Future _pickVideo(ImageSource source) async {
final pickedFile =
    await _picker.pickVideo(source: source,);
if(pickedFile == null){
  return;
}

await _uploadVideoFile(pickedFile.path);
}

Future _uploadVideoFile(String path) async {
final ref = FirebaseStorage.instance
    .ref()
    .child('videos')
    .child('${DateTime.now().toIso8601String() + p.basename(path)}');

final result = await ref.putFile(File(path));
final fileUrl = await result.ref.getDownloadURL();

setState(() {
  videoUrl = fileUrl;
});
}

Video Preview:

FutureBuilder(
                  future: _initializeVideoPlayerFuture,
                  builder: (context, snapshot){
                    if(snapshot.connectionState == ConnectionState.done){
                      return AspectRatio(
                        aspectRatio: _controller!.value.aspectRatio,
                      child: VideoPlayer(_controller!),);
                    } else {
                      return Center(
                        child: CircularProgressIndicator(),
                      );
                    }
                  },
                ),

enter image description here

Upvotes: 0

Views: 1570

Answers (1)

wesleyhodaj
wesleyhodaj

Reputation: 72

Can you check the value that you get manually from firebase storage with the value that you set here final fileUrl = await result.ref.getDownloadURL();

Upvotes: 0

Related Questions