opt: 切换页面时销毁播放器组件提升性能

This commit is contained in:
orz12
2024-01-22 12:03:17 +08:00
parent e052c6eafe
commit 569184a507

View File

@ -61,6 +61,7 @@ class _VideoDetailPageState extends State<VideoDetailPage>
final Floating floating = Floating(); final Floating floating = Floating();
// 生命周期监听 // 生命周期监听
late final AppLifecycleListener _lifecycleListener; late final AppLifecycleListener _lifecycleListener;
bool isShowing = true;
@override @override
void initState() { void initState() {
@ -216,15 +217,15 @@ class _VideoDetailPageState extends State<VideoDetailPage>
videoIntroController.isPaused = true; videoIntroController.isPaused = true;
plPlayerController!.removeStatusLister(playerListener); plPlayerController!.removeStatusLister(playerListener);
plPlayerController!.pause(); plPlayerController!.pause();
plPlayerController!.danmakuController?.pause();
plPlayerController!.danmakuController?.clear();
} }
setState(() => isShowing = false);
super.didPushNext(); super.didPushNext();
} }
@override @override
// 返回当前页面时 // 返回当前页面时
void didPopNext() async { void didPopNext() async {
setState(() => isShowing = true);
videoDetailController.isFirstTime = false; videoDetailController.isFirstTime = false;
final bool autoplay = autoPlayEnable; final bool autoplay = autoPlayEnable;
videoDetailController.playerInit(autoplay: autoplay); videoDetailController.playerInit(autoplay: autoplay);
@ -354,44 +355,45 @@ class _VideoDetailPageState extends State<VideoDetailPage>
boxConstraints.maxHeight; boxConstraints.maxHeight;
return Stack( return Stack(
children: <Widget>[ children: <Widget>[
FutureBuilder( if (isShowing)
future: _futureBuilderFuture, FutureBuilder(
builder: (BuildContext context, future: _futureBuilderFuture,
AsyncSnapshot snapshot) { builder: (BuildContext context,
if (snapshot.hasData && AsyncSnapshot snapshot) {
snapshot.data['status']) { if (snapshot.hasData &&
return Obx( snapshot.data['status']) {
() => !videoDetailController return Obx(
.autoPlay.value () => !videoDetailController
? const SizedBox() .autoPlay.value
: PLVideoPlayer( ? nil
controller: : PLVideoPlayer(
plPlayerController!, controller:
headerControl: plPlayerController!,
videoDetailController headerControl:
.headerControl, videoDetailController
danmuWidget: Obx( .headerControl,
() => PlDanmaku( danmuWidget: Obx(
key: Key( () => PlDanmaku(
videoDetailController key: Key(
.danmakuCid videoDetailController
.value .danmakuCid
.toString()), .value
cid: .toString()),
videoDetailController cid:
.danmakuCid videoDetailController
.value, .danmakuCid
playerController: .value,
plPlayerController!, playerController:
plPlayerController!,
),
), ),
), ),
), );
); } else {
} else { return const SizedBox();
return const SizedBox(); }
} },
}, ),
),
/// 关闭自动播放时 手动播放 /// 关闭自动播放时 手动播放
if (!videoDetailController if (!videoDetailController