Skip to content

Commit

Permalink
feat: 可否增加打开视频自动全屏的功能 issues #37
Browse files Browse the repository at this point in the history
  • Loading branch information
guozhigq committed Aug 29, 2023
1 parent c156468 commit 184088f
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 99 deletions.
12 changes: 12 additions & 0 deletions lib/pages/setting/play_setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ class _PlaySettingState extends State<PlaySetting> {
setKey: SettingBoxKey.enableAutoBrightness,
defaultVal: false,
),
const SetSwitchItem(
title: '自动全屏',
subTitle: '视频开始播放时进入全屏',
setKey: SettingBoxKey.enableAutoEnter,
defaultVal: false,
),
const SetSwitchItem(
title: '自动退出',
subTitle: '视频结束播放时退出全屏',
setKey: SettingBoxKey.enableAutoExit,
defaultVal: false,
),
ListTile(
dense: false,
title: Text('默认画质', style: titleStyle),
Expand Down
4 changes: 3 additions & 1 deletion lib/pages/video/detail/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class VideoDetailController extends GetxController
// 默认记录历史记录
bool enableHeart = true;
var userInfo;
late bool isFirstTime = true;

@override
void onInit() {
Expand Down Expand Up @@ -193,6 +194,7 @@ class VideoDetailController extends GetxController
bvid: bvid,
cid: cid,
enableHeart: enableHeart,
isFirstTime: isFirstTime,
);
}

Expand Down Expand Up @@ -233,7 +235,6 @@ class VideoDetailController extends GetxController
currentDecodeFormats = VideoDecodeFormatsCode.fromString(setting.get(
SettingBoxKey.defaultDecode,
defaultValue: VideoDecodeFormats.values.last.code))!;
print(currentDecodeFormats.description);
try {
// 当前视频没有对应格式返回第一个
bool flag = false;
Expand Down Expand Up @@ -287,6 +288,7 @@ class VideoDetailController extends GetxController
defaultST = Duration(milliseconds: data.lastPlayTime!);
if (autoPlay.value) {
await playerInit();
isShowCover.value = false;
}
} else {
if (result['code'] == -404) {
Expand Down
36 changes: 27 additions & 9 deletions lib/pages/video/detail/introduction/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ class VideoIntroPanel extends StatefulWidget {

class _VideoIntroPanelState extends State<VideoIntroPanel>
with AutomaticKeepAliveClientMixin {
final VideoIntroController videoIntroController =
Get.put(VideoIntroController(), tag: Get.arguments['heroTag']);
late String heroTag;
late VideoIntroController videoIntroController;
VideoDetailData? videoDetail;
late Future? _futureBuilderFuture;

// 添加页面缓存
@override
Expand All @@ -42,6 +43,11 @@ class _VideoIntroPanelState extends State<VideoIntroPanel>
@override
void initState() {
super.initState();

/// fix 全屏时参数丢失
heroTag = Get.arguments['heroTag'];
videoIntroController = Get.put(VideoIntroController(), tag: heroTag);
_futureBuilderFuture = videoIntroController.queryVideoIntro();
videoIntroController.videoDetail.listen((value) {
videoDetail = value;
});
Expand All @@ -57,15 +63,20 @@ class _VideoIntroPanelState extends State<VideoIntroPanel>
Widget build(BuildContext context) {
super.build(context);
return FutureBuilder(
future: videoIntroController.queryVideoIntro(),
future: _futureBuilderFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.data == null) {
return const SliverToBoxAdapter(child: SizedBox());
}
if (snapshot.data['status']) {
// 请求成功
return Obx(
() => VideoInfo(
loadingStatus: false,
videoDetail: videoIntroController.videoDetail.value),
loadingStatus: false,
videoDetail: videoIntroController.videoDetail.value,
heroTag: heroTag,
),
);
} else {
// 请求错误
Expand All @@ -79,7 +90,11 @@ class _VideoIntroPanelState extends State<VideoIntroPanel>
);
}
} else {
return VideoInfo(loadingStatus: true, videoDetail: videoDetail);
return VideoInfo(
loadingStatus: true,
videoDetail: videoDetail,
heroTag: heroTag,
);
}
},
);
Expand All @@ -89,16 +104,19 @@ class _VideoIntroPanelState extends State<VideoIntroPanel>
class VideoInfo extends StatefulWidget {
final bool loadingStatus;
final VideoDetailData? videoDetail;
final String? heroTag;

const VideoInfo({Key? key, this.loadingStatus = false, this.videoDetail})
const VideoInfo(
{Key? key, this.loadingStatus = false, this.videoDetail, this.heroTag})
: super(key: key);

@override
State<VideoInfo> createState() => _VideoInfoState();
}

class _VideoInfoState extends State<VideoInfo> with TickerProviderStateMixin {
final String heroTag = Get.arguments['heroTag'];
// final String heroTag = Get.arguments['heroTag'];
late String heroTag;
late final VideoIntroController videoIntroController;
late final VideoDetailController videoDetailCtr;
late final Map<dynamic, dynamic> videoItem;
Expand All @@ -117,7 +135,7 @@ class _VideoInfoState extends State<VideoInfo> with TickerProviderStateMixin {
@override
void initState() {
super.initState();

heroTag = widget.heroTag!;
videoIntroController = Get.put(VideoIntroController(), tag: heroTag);
videoDetailCtr = Get.find<VideoDetailController>(tag: heroTag);
videoItem = videoIntroController.videoItem!;
Expand Down
38 changes: 18 additions & 20 deletions lib/pages/video/detail/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,22 @@ class _VideoDetailPageState extends State<VideoDetailPage>
Get.put(VideoIntroController(), tag: Get.arguments['heroTag']);

PlayerStatus playerStatus = PlayerStatus.playing;
// bool isShowCover = true;
double doubleOffset = 0;

Box localCache = GStrorage.localCache;
Box setting = GStrorage.setting;
late double statusBarHeight;
final videoHeight = Get.size.width * 9 / 16;
late Future _futureBuilderFuture;
// 自动退出全屏
late bool autoExitFullcreen;

@override
void initState() {
super.initState();
statusBarHeight = localCache.get('statusBarHeight');
autoExitFullcreen =
setting.get(SettingBoxKey.enableAutoExit, defaultValue: false);
videoSourceInit();
appbarStreamListen();
}
Expand All @@ -63,7 +66,7 @@ class _VideoDetailPageState extends State<VideoDetailPage>
_futureBuilderFuture = videoDetailController.queryVideoUrl();
if (videoDetailController.autoPlay.value) {
plPlayerController = videoDetailController.plPlayerController;
playerListener();
plPlayerController!.addStatusLister(playerListener);
}
}

Expand All @@ -79,23 +82,15 @@ class _VideoDetailPageState extends State<VideoDetailPage>
}

// 播放器状态监听
void playerListener() {
plPlayerController!.onPlayerStatusChanged.listen(
(PlayerStatus status) async {
playerStatus = status;
if (status == PlayerStatus.playing) {
videoDetailController.isShowCover.value = false;
} else {
// 播放完成停止 or 切换下一个
if (status == PlayerStatus.completed) {
// 当只有1p或多p未打开自动播放时,播放完成还原进度条,展示控制栏
plPlayerController!.seekTo(Duration.zero);
plPlayerController!.onLockControl(false);
plPlayerController!.videoPlayerController!.pause();
}
}
},
);
void playerListener(PlayerStatus? status) {
if (status == PlayerStatus.completed) {
// 结束播放退出全屏
if (autoExitFullcreen) {
plPlayerController!.triggerFullScreen(status: false);
}
// 播放完展示控制栏
plPlayerController!.onLockControl(false);
}
}

// 继续播放或重新播放
Expand All @@ -110,11 +105,11 @@ class _VideoDetailPageState extends State<VideoDetailPage>
plPlayerController = videoDetailController.plPlayerController;
videoDetailController.autoPlay.value = true;
videoDetailController.isShowCover.value = false;
playerListener();
}

@override
void dispose() {
plPlayerController!.removeStatusLister(playerListener);
plPlayerController!.dispose();
super.dispose();
}
Expand All @@ -128,19 +123,22 @@ class _VideoDetailPageState extends State<VideoDetailPage>
}
videoDetailController.defaultST = plPlayerController!.position.value;
videoIntroController.isPaused = true;
plPlayerController!.removeStatusLister(playerListener);
plPlayerController!.pause();
super.didPushNext();
}

@override
// 返回当前页面时
void didPopNext() async {
videoDetailController.isFirstTime = false;
videoDetailController.playerInit();
videoIntroController.isPaused = false;
if (_extendNestCtr.position.pixels == 0) {
await Future.delayed(const Duration(milliseconds: 300));
plPlayerController!.play();
}
plPlayerController!.addStatusLister(playerListener);
super.didPopNext();
}

Expand Down
Loading

0 comments on commit 184088f

Please sign in to comment.