Flutter video_player点击重新播放

视频播放完成之后,暂停视频,点击重新播放

dart 复制代码
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class ListViewItemWidget extends StatefulWidget{

  @override
  State createState() {
    return _ListViewItemWidgetState();
  }
}

class _ListViewItemWidgetState extends State<ListViewItemWidget>{

  bool isPlay = false;
  bool isOver = false;
  late VideoPlayerController _controller;

  @override
  void initState() {
    _controller =  VideoPlayerController.network('https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4')
        ..initialize().then((_){
          _controller.play();
          setState(() {
            _controller.pause();
          });
        })
        ..addListener(videoListener);
  }

  @override
  void dispose() {
    _controller.dispose();
    _controller.removeListener(videoListener);
    super.dispose();
  }

  void videoListener(){
    var curPlayPosition = _controller.value.position;
    var totalPlayTime = _controller.value.duration;
    if (curPlayPosition == totalPlayTime && curPlayPosition.toString() != "0:00:00.000000"){
      isOver = true;
      isPlay = false;
      _controller.initialize()
        ..then((_) {
          setState(() {

          });
        });

      setState(() {

      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        //未关注的才会显示为你推荐
        Row(
          children: [
            Container(
              width: MediaQuery.of(context).size.width - 50,
              height: 50,
              alignment: Alignment.centerLeft,
              padding: const EdgeInsets.only(left: 12.0),
              child: Text("为你推荐"),
            ),
            SizedBox(
              width: 50,
              height: 50,
              child: InkWell(
                child: Icon(Icons.close),
                onTap: (){
                  /**
                   * 关闭
                   */
                },
              ),
            )
          ],
        ),

        Stack(
          children: [
            Container(
              height: 220,
              color: Colors.blueGrey,
              margin: EdgeInsets.only(top: 10),
              child: Stack(
                children: [
                  Positioned.fill(child: GestureDetector(
                      child: AspectRatio(
                        child: VideoPlayer(_controller),
                        aspectRatio: _controller.value.aspectRatio,
                      )),
                  ),
                  Container(
                    height: 40,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        /**
                         * 网络图片
                         */
                        // Image(image: Image.network(""))
                        Expanded(
                            flex: 1,
                            child:Icon(Icons.verified_user, color: Colors.white)
                        ),
                        Expanded(
                          flex: 6,
                          child:Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text("userName", style: TextStyle(fontSize: 18, color: Colors.white),),
                              Text("userName", style: TextStyle(fontSize: 12, color: Colors.white),)
                            ],
                          ),
                        ),
                        Expanded(
                            flex: 1,
                            child: Text("关注",  style: TextStyle(fontSize: 12, color: Colors.white))
                        ),
                        Expanded(
                            flex: 1,
                            child:   Icon(Icons.menu, color: Colors.white)
                        )
                      ],
                    ),
                  ),
                  buildControllerWidget()
                ],
              ),

            ),
          ],
        ),

        Container(
          margin: EdgeInsets.only(top: 10),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              /**
               * 网络图片
               */
              // Image(image: Image.network(""))

              Expanded(
                  flex: 1,
                  child:Icon(Icons.favorite_border)
              ),
              Expanded(
                  flex: 1,
                  child:Icon(Icons.messenger_outline)
              ),
              Expanded(
                  flex: 1,
                  child:Icon(Icons.share_outlined)
              ),
              Expanded(
                flex: 4,
                child:Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                  ],
                ),
              ),
              Expanded(
                  flex: 1,
                  child:   Icon(Icons.collections_outlined)
              )
            ],
          ),
        ),
        Container(
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(left: 18,top: 20),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text("1732赞"),
              Text("hahhahah"),
              Text("总共23条评论"),
              Text("6天前")
            ],
          ),
        )
      ],
    );
  }

  //播放界面
  Widget buildControllerWidget(){
    if(isPlay){
      return Container(
        child: GestureDetector(
          onTap: (){
            _controller.pause();
            isPlay = false;
            setState(() {
            });
          },
        ),
      );
    }
    return  Positioned.fill(child: AspectRatio(
        aspectRatio: _controller.value.aspectRatio,
        child: Container(
          child: GestureDetector(
            onTap: (){
              if(isOver){
                isOver = false;
                _controller.play();
                isPlay = true;
                setState(() {
                });
              } else {
                _controller.play();
                isPlay = true;
                setState(() {
                });
              }
            },
            child: Icon(Icons.play_circle_fill, size: 44,),
          ),
        )),
    );
  }
}

pubspec.yaml依赖

dart 复制代码
  video_player: ^2.3.0

要求Android compileSdkVersion 为33

相关推荐
烬奇小云3 小时前
认识一下Unicorn
android·python·安全·系统安全
顾北川_野15 小时前
Android 进入浏览器下载应用,下载的是bin文件无法安装,应为apk文件
android
CYRUS STUDIO15 小时前
Android 下内联汇编,Android Studio 汇编开发
android·汇编·arm开发·android studio·arm
右手吉他15 小时前
Android ANR分析总结
android
jhonjson17 小时前
Flutter开发之flutter_local_notifications
flutter·macos·cocoa
PenguinLetsGo17 小时前
关于 Android15 GKI2407R40 导致梆梆加固软件崩溃
android·linux
iFlyCai18 小时前
23种设计模式的Flutter实现第一篇创建型模式(一)
flutter·设计模式·dart
恋猫de小郭19 小时前
Flutter 小技巧之 OverlayPortal 实现自限性和可共享的页面图层
flutter
杨武博19 小时前
音频格式转换
android·音视频
音视频牛哥21 小时前
Android音视频直播低延迟探究之:WLAN低延迟模式
android·音视频·实时音视频·大牛直播sdk·rtsp播放器·rtmp播放器·android rtmp