flutter 视频播放

745 阅读1分钟

比较简单有问题可提 用的是 video_player: ^0.10.1+2

完整代码 复制过去就能运行了 url XXX.mp4 (注意:http 要配置允许http)

import 'package:flutter_search_site/app/sq_color.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter/material.dart';

class VideoPlayScene extends StatefulWidget {
  final String url;

  VideoPlayScene(this.url);

  static push(BuildContext context, String url) {
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return VideoPlayScene(url);
    }));
  }

  @override
  _VideoPlaySceneState createState() => _VideoPlaySceneState();
}

class _VideoPlaySceneState extends State<VideoPlayScene> {
  VideoPlayerController _controller;
  double progressValue = 0;
  int totalSeconds = 0;
  int currentSecond = 0;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(this.widget.url)
      ..addListener(() {
        setState(() {
          currentSecond = _controller.value.position.inSeconds;
        });
      })
      ..initialize().then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {
          totalSeconds = _controller.value.duration.inSeconds;
          print(totalSeconds);
        });
      });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Video Demo',
      home: Scaffold(
        backgroundColor: SQColor.darkGray,
        body: Column(
          children: <Widget>[
            videoView(),
            Expanded(child: SizedBox()),
            GestureDetector(
              onTap: () {
                setState(() {
                  if (_controller.value.isPlaying) {
                    _controller.pause();
                  } else {
                    if (currentSecond == totalSeconds)
                      _controller.seekTo(Duration(seconds: 0));
                    _controller.play();
                }
                });
              },
              child: Icon(
                _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
                color: SQColor.paper,
              ),
            ),
            sliderView(),
          ],
        ),
      ),
    );
  }

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

  sliderView() {
    return Row(
      children: <Widget>[
        Padding(
          padding: EdgeInsets.only(left: 16),
          child: timeView(currentSecond),
        ),
        Expanded(
          child: Slider(
            value: currentSecond + 0.0,
            max: totalSeconds + 0.0,
            onChanged: (double value) {
              setState(() {
                currentSecond = value.floor();
              });
            },
            onChangeEnd: (double value) {
              _controller.seekTo(Duration(seconds: currentSecond)).then((_) {
                _controller.play();
              });
            },
            activeColor: SQColor.paper,
            inactiveColor: SQColor.gray,
          ),
        ),
        Padding(
          padding: EdgeInsets.only(right: 16),
          child: timeView(totalSeconds),
        ),
      ],
    );
  }

  Text timeView(int time) {
    String data = time < 10 ? "00:0$time" : "00:$time";
    return Text(data, style: TextStyle(color: SQColor.gray));
  }

  videoView() {
    return Center(
      child: _controller.value.initialized
          ? AspectRatio(
              aspectRatio: _controller.value.aspectRatio,
              child: VideoPlayer(_controller),
            )
          : Container(),
    );
  }
}