79 lines
2.1 KiB
Dart
79 lines
2.1 KiB
Dart
import 'package:fijkplayer/fijkplayer.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:video_player/video_player.dart';
|
|
|
|
class CustomVideoPlayer extends StatefulWidget {
|
|
final String url;
|
|
|
|
const CustomVideoPlayer({super.key, required this.url});
|
|
|
|
@override
|
|
State<StatefulWidget> createState() => _CustomVideoPlayerState();
|
|
}
|
|
|
|
class _CustomVideoPlayerState extends State<CustomVideoPlayer> {
|
|
FijkPlayer? player;
|
|
VideoPlayerController? _controller;
|
|
|
|
_CustomVideoPlayerState();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
SystemChrome.setPreferredOrientations(
|
|
[DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
|
|
|
|
// initVideoPlayer();
|
|
initFijkPlayer();
|
|
}
|
|
|
|
void initVideoPlayer() {
|
|
_controller = VideoPlayerController.networkUrl(Uri.parse(widget.url))
|
|
..initialize().then((_) {
|
|
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
|
|
setState(() {
|
|
_controller!.play();
|
|
});
|
|
});
|
|
}
|
|
|
|
void initFijkPlayer() {
|
|
player = FijkPlayer();
|
|
player!.setDataSource(widget.url, autoPlay: true);
|
|
player!.enterFullScreen();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
toolbarHeight: 35,
|
|
),
|
|
body: player == null
|
|
? Center(
|
|
child: _controller!.value.isInitialized
|
|
? AspectRatio(
|
|
aspectRatio: _controller!.value.aspectRatio,
|
|
child: VideoPlayer(_controller!),
|
|
)
|
|
: Container(),
|
|
)
|
|
: Container(
|
|
alignment: Alignment.center,
|
|
child: FijkView(
|
|
player: player!,
|
|
),
|
|
));
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
SystemChrome.setPreferredOrientations([]);
|
|
super.dispose();
|
|
player?.stop().then((value) => player?.dispose());
|
|
_controller?.dispose();
|
|
}
|
|
}
|