Flutter播放器 chewie-0.9.8 自定义UI,滑动手势

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter播放器 chewie-0.9.8 自定义UI,滑动手势相关的知识,希望对你有一定的参考价值。

参考技术A 修改后大致效果如下图

改动处添加了注释 搜索///即可

Flutter Chewie 视频播放器 - 检查播放何时完成

【中文标题】Flutter Chewie 视频播放器 - 检查播放何时完成【英文标题】:Flutter Chewie Video Player - check when playback is complete 【发布时间】:2020-06-04 09:19:36 【问题描述】:

我在 Flutter 应用中使用 Chewie 视频播放器。如何查看播放完成的时间,以便我可以关闭屏幕并进行处理?

【问题讨论】:

【参考方案1】:

您可以在下面复制粘贴运行完整代码 您可以查看positionduration 完整代码是官方示例设置循环到false并添加以下代码sn -p

代码sn-p

_videoPlayerController1.addListener(() 
      if (_videoPlayerController1.value.position ==
          _videoPlayerController1.value.duration) 
        print('video Ended');
      
    );

输出

I/flutter ( 4881): video Ended

完整代码

import 'package:chewie/chewie.dart';
import 'package:chewie/src/chewie_player.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

void main() 
  runApp(
    ChewieDemo(),
  );


class ChewieDemo extends StatefulWidget 
  ChewieDemo(this.title = 'Chewie Demo');

  final String title;

  @override
  State<StatefulWidget> createState() 
    return _ChewieDemoState();
  


class _ChewieDemoState extends State<ChewieDemo> 
  TargetPlatform _platform;
  VideoPlayerController _videoPlayerController1;
  VideoPlayerController _videoPlayerController2;
  ChewieController _chewieController;

  @override
  void initState() 
    super.initState();
    _videoPlayerController1 = VideoPlayerController.network(
        'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4');
    _videoPlayerController2 = VideoPlayerController.network(
        'https://www.sample-videos.com/video123/mp4/480/asdasdas.mp4');
    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController1,
      aspectRatio: 3 / 2,
      autoPlay: true,
      looping: false,
      // Try playing around with some of these other options:

      // showControls: false,
      // materialProgressColors: ChewieProgressColors(
      //   playedColor: Colors.red,
      //   handleColor: Colors.blue,
      //   backgroundColor: Colors.grey,
      //   bufferedColor: Colors.lightGreen,
      // ),
      // placeholder: Container(
      //   color: Colors.grey,
      // ),
      // autoInitialize: true,
    );

    _videoPlayerController1.addListener(() 
      if (_videoPlayerController1.value.position ==
          _videoPlayerController1.value.duration) 
        print('video Ended');
      
    );
  

  @override
  void dispose() 
    _videoPlayerController1.dispose();
    _videoPlayerController2.dispose();
    _chewieController.dispose();
    super.dispose();
  

  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      title: widget.title,
      theme: ThemeData.light().copyWith(
        platform: _platform ?? Theme.of(context).platform,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Column(
          children: <Widget>[
            Expanded(
              child: Center(
                child: Chewie(
                  controller: _chewieController,
                ),
              ),
            ),
            FlatButton(
              onPressed: () 
                _chewieController.enterFullScreen();
              ,
              child: Text('Fullscreen'),
            ),
            Row(
              children: <Widget>[
                Expanded(
                  child: FlatButton(
                    onPressed: () 
                      setState(() 
                        _chewieController.dispose();
                        _videoPlayerController2.pause();
                        _videoPlayerController2.seekTo(Duration(seconds: 0));
                        _chewieController = ChewieController(
                          videoPlayerController: _videoPlayerController1,
                          aspectRatio: 3 / 2,
                          autoPlay: true,
                          looping: true,
                        );
                      );
                    ,
                    child: Padding(
                      child: Text("Video 1"),
                      padding: EdgeInsets.symmetric(vertical: 16.0),
                    ),
                  ),
                ),
                Expanded(
                  child: FlatButton(
                    onPressed: () 
                      setState(() 
                        _chewieController.dispose();
                        _videoPlayerController1.pause();
                        _videoPlayerController1.seekTo(Duration(seconds: 0));
                        _chewieController = ChewieController(
                          videoPlayerController: _videoPlayerController2,
                          aspectRatio: 3 / 2,
                          autoPlay: true,
                          looping: true,
                        );
                      );
                    ,
                    child: Padding(
                      padding: EdgeInsets.symmetric(vertical: 16.0),
                      child: Text("Error Video"),
                    ),
                  ),
                )
              ],
            ),
            Row(
              children: <Widget>[
                Expanded(
                  child: FlatButton(
                    onPressed: () 
                      setState(() 
                        _platform = TargetPlatform.android;
                      );
                    ,
                    child: Padding(
                      child: Text("Android controls"),
                      padding: EdgeInsets.symmetric(vertical: 16.0),
                    ),
                  ),
                ),
                Expanded(
                  child: FlatButton(
                    onPressed: () 
                      setState(() 
                        _platform = TargetPlatform.iOS;
                      );
                    ,
                    child: Padding(
                      padding: EdgeInsets.symmetric(vertical: 16.0),
                      child: Text("iOS controls"),
                    ),
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  

【讨论】:

以上是关于Flutter播放器 chewie-0.9.8 自定义UI,滑动手势的主要内容,如果未能解决你的问题,请参考以下文章

Flutter - 播放来自平台特定文件夹的声音文件(例如 Android 的 android/app/src/main/res/raw)

Flutter开发--视频播放器

Flutter视频播放器,简洁!

Flutter 直播

Flutter3引用原生播放器-IOS(Swift)篇

Flutter 视频播放器,如 Youtube、Amazon Prime