【问题标题】:I want to get the video size from internet path for my flutter app我想从我的颤振应用程序的互联网路径获取视频大小
【发布时间】:2020-08-18 00:17:49
【问题描述】:

我正在尝试获取视频文件大小并将其用作 LinearPercentIndicator 中的百分比。 根据我的搜索,我找到了一个代码,但对于 android,我怎么能在颤振上做到这一点。

final URL uri=new URL("http://your_url.com/file.mp4");
URLConnection connection;
try
{
connection=uri.openConnection();
connection.connect();
final String contentLengthStr=ucon.getHeaderField("content-length");
// do whatever
}

catch(final IOException exception)
{
}

PS:我正在使用FFmpeg进行下载。

【问题讨论】:

    标签: android flutter dart video download


    【解决方案1】:

    例如,使用 dio 插件

    dio使用大尺寸文件下载器

    网址链接:https://pub.dev/packages/dio

    举例

    import 'dart:io';
    
    import 'package:flutter/material.dart';
    import 'package:dio/dio.dart';
    import 'package:path_provider/path_provider.dart';
    
    class LargeFileMain extends StatefulWidget {
      @override
      State<StatefulWidget> createState() => _LargeFileMain();
    }
    
    class _LargeFileMain extends State<LargeFileMain> {
      final imgUrl =
          'http://your_url.com/file.mp4';
      bool downloading = false;
      var progressString = "";
      var file;
    
      Future<void> downloadFile() async {
        Dio dio = Dio();
        try {
          var dir = await getApplicationDocumentsDirectory();
          await dio.download(imgUrl, '${dir.path}/myimage.jpg',
              onReceiveProgress: (rec, total) {
            print('Rec: $rec , Total: $total');
            file = '${dir.path}/myimage.jpg';
            setState(() {
              downloading = true;
              progressString = ((rec / total) * 100).toStringAsFixed(0) + '%';
            });
          });
        } catch (e) {
          print(e);
        }
        setState(() {
          downloading = false;
          progressString = 'Completed';
        });
        print('Download completed');
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Large File Example'),
          ),
          body: Center(
              child: downloading
                  ? Container(
                      height: 120.0,
                      width: 200.0,
                      child: Card(
                        color: Colors.black,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            CircularProgressIndicator(),
                            SizedBox(
                              height: 20.0,
                            ),
                            Text(
                              'Downloading File: $progressString',
                              style: TextStyle(
                                color: Colors.white,
                              ),
                            )
                          ],
                        ),
                      ),
                    )
                  : FutureBuilder(
                      builder: (context, snapshot) {
                        switch (snapshot.connectionState) {
                          case ConnectionState.none:
                            print('none');
                            return Text('No Data');
                          case ConnectionState.waiting:
                            print('waiting');
                            return CircularProgressIndicator();
                          case ConnectionState.active:
                            print('active');
                            return CircularProgressIndicator();
                          case ConnectionState.done:
                            print('done');
                            if (snapshot.hasData) {
                              return snapshot.data;
                            }
                        }
                        return Text('No Data');
                      },
                      future: downloadWidget(file),
                    )),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              downloadFile();
            },
            child: Icon(Icons.file_download),
          ),
        );
      }
    
      Future<Widget> downloadWidget(String filePath) async {
        File file = File(filePath);
        bool exist = await file.exists();
        if (exist) {
          return Center(
            // your video file using widget
          );
        } else {
          return Text('No Data');
        }
      }
    }
    

    【讨论】:

    猜你喜欢
    • 2020-11-28
    • 2020-11-04
    • 2020-03-04
    • 1970-01-01
    • 2021-05-29
    • 2020-08-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多