native_video_player_plugin 1.0.0 copy "native_video_player_plugin: ^1.0.0" to clipboard
native_video_player_plugin: ^1.0.0 copied to clipboard

Plugin for implementing video player.

example/lib/main.dart

import 'dart:developer';

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

void main() => runApp(MaterialApp(home: VideoPage()));

class VideoPage extends StatefulWidget {
  const VideoPage({super.key});

  @override
  State<VideoPage> createState() => _VideoPageState();
}

class _VideoPageState extends State<VideoPage> {
  final controller = NativeVideoPlayerController();
  // String status = "Status: idle";
  bool isLooping = false;
  String url =
      "https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4";
  double width = 320;
  double height = 180;
  double aspectRatio = 16 / 9;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Native Player")),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Padding(
                padding: EdgeInsets.all(8.0),
                child: PlayerStatusText(controller: controller),
              ),
              Container(
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.red, width: 2),
                  color: Colors.black26,
                ),
                width: width,
                height: height,
                child: Stack(
                  children: [
                    // if (showVideo)
                    Center(
                      child: NativeVideoPlayer(
                        controller: controller,
                        // aspectRatio: aspectRatio,
                      ),
                    ),
                    Align(
                      alignment: Alignment.center,
                      child: PlayerStatusWidget(controller: controller),
                    ),
                  ],
                ),
              ),

              SizedBox(height: 16),
              // StatusWidget(controller: controller),
              SizedBox(height: 8),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      ElevatedButton(
                        onPressed: () => controller.load(url),
                        child: Text("Load"),
                      ),
                      SizedBox(width: 8),
                      ElevatedButton(
                        onPressed: controller.play,
                        child: Text("Play"),
                      ),
                    ],
                  ),
                  Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      SizedBox(width: 8),
                      ElevatedButton(
                        onPressed: controller.pause,
                        child: Text("Pause"),
                      ),
                    ],
                  ),
                  Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      ElevatedButton(
                        onPressed: () async {
                          final position = await controller.getPosition();
                          print(position);
                        },
                        child: Text("Get Position"),
                      ),
                      SizedBox(width: 8),
                      ElevatedButton(
                        onPressed: () async {
                          final duration = await controller.getDuration();
                          print(duration);
                        },
                        child: Text("Get Duration"),
                      ),

                      SizedBox(width: 8),
                      ElevatedButton(
                        onPressed: () async {
                          isLooping = !isLooping;
                          log("looping: $isLooping");
                          controller.setLooping(isLooping);
                        },
                        child: Text("Set Looping"),
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class PlayerStatusWidget extends StatefulWidget {
  final NativeVideoPlayerController controller;
  const PlayerStatusWidget({super.key, required this.controller});

  @override
  State<PlayerStatusWidget> createState() => _PlayerStatusWidgetState();
}

class _PlayerStatusWidgetState extends State<PlayerStatusWidget> {
  PlayerStatus status = PlayerStatus.unknown;
  @override
  initState() {
    super.initState();
    widget.controller.addStatusListener(updateStatus);
  }

  void updateStatus(status) {
    if (status == this.status) return;
    setState(() {
      this.status = status;
    });
  }

  @override
  void dispose() {
    widget.controller.removeStatusListener(updateStatus);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    bool showProgressIndicator =
        (status == PlayerStatus.buffering ||
        status == PlayerStatus.idle ||
        status == PlayerStatus.unknown);
    return Container(
      child: (showProgressIndicator)
          ? CircularProgressIndicator()
          : SizedBox.shrink(),
    );
  }
}

class PlayerStatusText extends StatefulWidget {
  final NativeVideoPlayerController controller;
  const PlayerStatusText({super.key, required this.controller});

  @override
  State<PlayerStatusText> createState() => _PlayerStatusTextState();
}

class _PlayerStatusTextState extends State<PlayerStatusText> {
  PlayerStatus status = PlayerStatus.unknown;
  @override
  initState() {
    super.initState();
    widget.controller.addStatusListener(updateStatus);
  }

  void updateStatus(status) {
    if (status == this.status) return;
    setState(() {
      this.status = status;
    });
  }

  @override
  void dispose() {
    widget.controller.removeStatusListener(updateStatus);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(8.0),
      decoration: BoxDecoration(
        border: Border.all(color: Colors.blue, width: 1),
        borderRadius: BorderRadius.circular(8),
        color: Colors.transparent,
      ),
      child: Text(
        "Status: $status",
        style: TextStyle(fontSize: 16, color: Colors.black54),
      ),
    );
  }
}