Android配置

/android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>

Ios配置

Ios无需配置,注意要使用https协议

代码

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class VideoPage extends StatefulWidget {
  const VideoPage({super.key});
  @override
  State<VideoPage> createState() => _VideoPageState();
}

class _VideoPageState extends State<VideoPage> {
  late VideoPlayerController _controller;
  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(
        'https://dashanbook.oss-cn-shenzhen.aliyuncs.com/av/2023/12/04/ce2ad15e833e4cd4ad2cfbb7cf32ba7c.mp4')
      ..initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even
// before the play button has been pressed.
        print("initialized");
      });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("video VideoPlayer"),
      ),
      body: Center(
        child: _controller.value.isInitialized
            ? AspectRatio(
                aspectRatio: _controller.value.aspectRatio,
                child: VideoPlayer(_controller),
              )
            : AspectRatio(
                aspectRatio: _controller.value.aspectRatio,
                child: Container(color: Colors.yellow),  //没有播放的时候的颜色;可以换成背景
              ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _controller.value.isPlaying
                ? _controller.pause()
                : _controller.play();
          });
        },
        child: Icon(
          _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }
}

 

 

posted on 2024-01-16 20:57  鲤斌  阅读(384)  评论(0)    收藏  举报