import 'dart:convert'; import 'dart:math'; import 'package:chewie/chewie.dart'; import "package:dart_vlc/dart_vlc.dart"; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:video_player/video_player.dart'; import 'api/api.dart'; import 'api/token.dart'; import 'log/log.dart'; import 'platform.dart'; class VideoScreen extends StatefulWidget { const VideoScreen({Key? key, required this.videoID}) : super(key: key); final int videoID; @override State createState() => _VideoScreenState(); } class _VideoScreenState extends State { late Player player; ChewieController? _chewieController; void loadData() async { final data = await API.query("video", "loadVideo", {'MovieId': widget.videoID}); final d = jsonDecode(data); final url = d["MovieUrl"]; final token = await Token.getInstance().getToken(); if (token == null) return; final baseurl = token.domain; // todo not static middle path final path = baseurl + "/videos/vids/" + url; if (isDesktop()) { player = Player(id: Random().nextInt(0x7fffffff)); final media2 = Media.network(path); player.open( media2, autoStart: true, // default ); } else { final VideoPlayerController _controller = VideoPlayerController.network(path); await _controller.initialize(); _chewieController = ChewieController( videoPlayerController: _controller, autoPlay: true, looping: true, ); setState(() {}); } } @override void initState() { super.initState(); RawKeyboard.instance.addListener((value) { if (value.logicalKey == LogicalKeyboardKey.arrowRight) { player.seek(player.position.position! + const Duration(seconds: 5)); } else if (value.logicalKey == LogicalKeyboardKey.arrowLeft) { player.seek(player.position.position! + const Duration(seconds: -5)); } }); loadData(); // todo hide appbar after some seonds } @override void dispose() { super.dispose(); if (isDesktop()) { player.pause(); player.stop(); player.dispose(); } else { _chewieController?.videoPlayerController.dispose(); _chewieController?.dispose(); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Second Route'), ), body: Center(child: isDesktop() ? videoDesktop() : videoNotDesktop()), ); } Widget videoDesktop() { return Video( player: player, scale: 1.0, // default showControls: true, playlistLength: 1, // default ); } Widget videoNotDesktop() { if (_chewieController == null) { return Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: const [CircularProgressIndicator(), Text("loading...")], ); } return Chewie( controller: _chewieController!, ); } }