فهرست منبع

Minor optimizations to the video widget

Vishnu Mohandas 4 سال پیش
والد
کامیت
ced00f9267
1فایلهای تغییر یافته به همراه14 افزوده شده و 7 حذف شده
  1. 14 7
      lib/ui/video_widget.dart

+ 14 - 7
lib/ui/video_widget.dart

@@ -1,3 +1,4 @@
+import 'dart:io' as io;
 import 'package:chewie/chewie.dart';
 import 'package:chewie/chewie.dart';
 import 'package:flutter/cupertino.dart';
 import 'package:flutter/cupertino.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter/material.dart';
@@ -38,14 +39,14 @@ class _VideoWidgetState extends State<VideoWidget> {
     super.initState();
     super.initState();
     if (widget.file.localID == null) {
     if (widget.file.localID == null) {
       if (!widget.file.isEncrypted) {
       if (!widget.file.isEncrypted) {
-        _setVideoPlayerController(widget.file.getStreamUrl());
+        _setVideoPlayerController(url: widget.file.getStreamUrl());
         _videoPlayerController.addListener(() {
         _videoPlayerController.addListener(() {
           if (_videoPlayerController.value.hasError) {
           if (_videoPlayerController.value.hasError) {
             _logger.warning(_videoPlayerController.value.errorDescription);
             _logger.warning(_videoPlayerController.value.errorDescription);
             showToast(
             showToast(
                 "The video has not been processed yet. Downloading the original one...",
                 "The video has not been processed yet. Downloading the original one...",
                 toastLength: Toast.LENGTH_SHORT);
                 toastLength: Toast.LENGTH_SHORT);
-            _setVideoPlayerController(widget.file.getDownloadUrl());
+            _setVideoPlayerController(url: widget.file.getDownloadUrl());
           }
           }
         });
         });
       } else {
       } else {
@@ -61,13 +62,13 @@ class _VideoWidgetState extends State<VideoWidget> {
             });
             });
           },
           },
         ).then((file) {
         ).then((file) {
-          _setVideoPlayerController(file.path);
+          _setVideoPlayerController(file: file);
         });
         });
       }
       }
     } else {
     } else {
       widget.file.getAsset().then((asset) {
       widget.file.getAsset().then((asset) {
         asset.getMediaUrl().then((url) {
         asset.getMediaUrl().then((url) {
-          _setVideoPlayerController(url);
+          _setVideoPlayerController(url: url);
         });
         });
       });
       });
     }
     }
@@ -82,8 +83,14 @@ class _VideoWidgetState extends State<VideoWidget> {
     super.dispose();
     super.dispose();
   }
   }
 
 
-  VideoPlayerController _setVideoPlayerController(String url) {
-    return _videoPlayerController = VideoPlayerController.network(url)
+  VideoPlayerController _setVideoPlayerController({String url, io.File file}) {
+    var videoPlayerController;
+    if (url != null) {
+      videoPlayerController = VideoPlayerController.network(url);
+    } else {
+      videoPlayerController = VideoPlayerController.file(file);
+    }
+    return _videoPlayerController = videoPlayerController
       ..initialize().whenComplete(() {
       ..initialize().whenComplete(() {
         if (mounted) {
         if (mounted) {
           setState(() {});
           setState(() {});
@@ -101,7 +108,7 @@ class _VideoWidgetState extends State<VideoWidget> {
       key: Key(widget.file.tag()),
       key: Key(widget.file.tag()),
       onVisibilityChanged: (info) {
       onVisibilityChanged: (info) {
         if (info.visibleFraction < 1) {
         if (info.visibleFraction < 1) {
-          if (mounted) {
+          if (mounted && _chewieController != null) {
             _chewieController.pause();
             _chewieController.pause();
           }
           }
         }
         }