UIInterfaceOrientationLandscapeLeft
和UIInterfaceOrientationLandscapeRight
都为false的情况下,无法做到全屏播放,因为FLutter的SystemChrome.setPreferredOrientations
方法不适配iOS端,无法控制屏幕进行旋转,下面我们看效果chewie
封装的播放器代码如下:
import 'dart:io';
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
// ignore: depend_on_referenced_packages
import 'package:video_player/video_player.dart';
class VideoPlayerWidget extends StatefulWidget {
final String videoUrl;
final bool fullScreenByDefault;
const VideoPlayerWidget({
super.key,
required this.videoUrl,
this.fullScreenByDefault = false,
});
@override
State createState() => _VideoPlayerWidgetState();
}
class _VideoPlayerWidgetState extends State {
late VideoPlayerController _videoPlayerController;
ChewieController? _chewieController;
int? bufferDelay;
@override
void initState() {
super.initState();
initializePlayer();
}
@override
void dispose() {
_videoPlayerController.dispose();
_chewieController?.dispose();
super.dispose();
}
List srcs = [
"https://assets.mixkit.co/videos/preview/mixkit-spinning-around-the-earth-29351-large.mp4",
"https://assets.mixkit.co/videos/preview/mixkit-daytime-city-traffic-aerial-view-56-large.mp4",
"https://assets.mixkit.co/videos/preview/mixkit-a-girl-blowing-a-bubble-gum-at-an-amusement-park-1226-large.mp4"
];
Future initializePlayer() async {
//判断是本地视频还是网络请求下来的视频
if (widget.videoUrl.contains('http')) {
_videoPlayerController =
VideoPlayerController.networkUrl(Uri.parse(widget.videoUrl));
} else {
_videoPlayerController =
VideoPlayerController.file(File(widget.videoUrl));
}
await Future.wait([
_videoPlayerController.initialize(),
]);
_createChewieController();
setState(() {});
}
void _createChewieController() {
_chewieController = ChewieController(
videoPlayerController: _videoPlayerController,
autoPlay: true,
hideControlsTimer: const Duration(seconds: 10),
allowPlaybackSpeedChanging: false,
fullScreenByDefault: widget.fullScreenByDefault,
);
}
@override
Widget build(BuildContext context) {
return Container(
color: const Color.fromRGBO(28, 34, 47, 1),
width: MediaQuery.of(context).size.width - 20,
height: 300,
child: Center(
child: _chewieController != null &&
_chewieController!.videoPlayerController.value.isInitialized
? Chewie(
controller: _chewieController!,
)
: const Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
SizedBox(height: 20),
Text(
'Loading',
style: TextStyle(
color: Colors.white,
),
),
],
),
),
);
}
}
如果碰到Video Player "UnimplementedError: init() has not been implemented
的错误,关闭App,重新运行(Flutter run)就行了
视频播放链接https://assets.mixkit.co/videos/preview/mixkit-spinning-around-the-earth-29351-large.mp4
body: Center(
child: ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return Dialog(
insetPadding: const EdgeInsets.only(left: 0),
child: Platform.isAndroid
? const VideoPlayerWidget(
videoUrl: videoUrl,
) //Android视频播放器
: SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.width * 9 / 16,
child: const UiKitView(
viewType:
'plugins.flutter.io/custom_platform_view_plugin',
creationParams: {
'videoUrl': videoUrl,
},
creationParamsCodec: StandardMessageCodec(),
),
), //iOS视频播放器
);
},
);
},
child: const Text('点击播放视频'),
),
),
因为写视频播放器的时候用到了Masonry
三方,所有用户需要再Podfile
里面加入pod 'Masonry'
,然后cd到Runner目录下,运行pod install
安装Masonry
AppDelegate.m
里面注册一下[FlutterViewRegistran registerWithRegistry:self];
喜欢的同学欢迎帮忙点个star 简书传送门