feat: 视频、直播pip Android端

This commit is contained in:
guozhigq
2023-09-07 18:58:58 +08:00
parent 7f961e998c
commit 0dfcd4ed40
7 changed files with 196 additions and 20 deletions

View File

@ -1,3 +1,6 @@
import 'dart:io';
import 'package:floating/floating.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pilipala/common/widgets/network_img_layer.dart';
@ -19,6 +22,7 @@ class _LiveRoomPageState extends State<LiveRoomPage> {
bool isShowCover = true;
bool isPlay = true;
Floating? floating;
@override
void initState() {
@ -32,19 +36,24 @@ class _LiveRoomPageState extends State<LiveRoomPage> {
}
},
);
if (Platform.isAndroid) {
floating = Floating();
}
}
@override
void dispose() {
plPlayerController!.dispose();
if (floating != null) {
floating!.dispose();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
final videoHeight = MediaQuery.of(context).size.width * 9 / 16;
return Scaffold(
Widget childWhenDisabled = Scaffold(
primary: true,
appBar: AppBar(
centerTitle: false,
@ -98,6 +107,7 @@ class _LiveRoomPageState extends State<LiveRoomPage> {
bottomControl: BottomControl(
controller: plPlayerController,
liveRoomCtr: _liveRoomController,
floating: floating,
),
)
: const SizedBox(),
@ -123,5 +133,25 @@ class _LiveRoomPageState extends State<LiveRoomPage> {
],
),
);
Widget childWhenEnabled = AspectRatio(
aspectRatio: 16 / 9,
child: plPlayerController!.videoPlayerController != null
? PLVideoPlayer(
controller: plPlayerController!,
bottomControl: BottomControl(
controller: plPlayerController,
liveRoomCtr: _liveRoomController,
),
)
: const SizedBox(),
);
if (Platform.isAndroid) {
return PiPSwitcher(
childWhenDisabled: childWhenDisabled,
childWhenEnabled: childWhenEnabled,
);
} else {
return childWhenDisabled;
}
}
}

View File

@ -1,4 +1,8 @@
import 'dart:io';
import 'package:floating/floating.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:pilipala/models/video/play/url.dart';
@ -9,9 +13,11 @@ import 'package:pilipala/utils/storage.dart';
class BottomControl extends StatefulWidget implements PreferredSizeWidget {
final PlPlayerController? controller;
final LiveRoomController? liveRoomCtr;
final Floating? floating;
const BottomControl({
this.controller,
this.liveRoomCtr,
this.floating,
Key? key,
}) : super(key: key);
@ -85,6 +91,35 @@ class _BottomControlState extends State<BottomControl> {
),
),
const SizedBox(width: 4),
if (Platform.isAndroid) ...[
SizedBox(
width: 34,
height: 34,
child: IconButton(
style: ButtonStyle(
padding: MaterialStateProperty.all(EdgeInsets.zero),
),
onPressed: () async {
bool canUsePiP = false;
widget.controller!.hiddenControls(false);
try {
canUsePiP = await widget.floating!.isPipAvailable;
} on PlatformException catch (_) {
canUsePiP = false;
}
if (canUsePiP) {
await widget.floating!.enable();
} else {}
},
icon: const Icon(
Icons.picture_in_picture_outlined,
size: 18,
color: Colors.white,
),
),
),
const SizedBox(width: 4),
],
ComBtn(
icon: const Icon(
Icons.fullscreen,