Merge branch 'fix' into alpha

This commit is contained in:
guozhigq
2023-09-17 22:30:46 +08:00
3 changed files with 58 additions and 41 deletions

View File

@ -17,17 +17,11 @@ class DanmakaHttp {
'oid': cid, 'oid': cid,
'segment_index': segmentIndex, 'segment_index': segmentIndex,
}; };
var response = await Request().get(
// 计算函数 Api.webDanmaku,
Future<DmSegMobileReply> computeTask(Map<String, int> params) async { data: params,
var response = await Request().get( extra: {'resType': ResponseType.bytes},
Api.webDanmaku, );
data: params, return DmSegMobileReply.fromBuffer(response.data);
extra: {'resType': ResponseType.bytes},
);
return DmSegMobileReply.fromBuffer(response.data);
}
return await compute(computeTask, params);
} }
} }

View File

@ -10,22 +10,32 @@ class PlDanmakuController {
// 按 6min 分段 // 按 6min 分段
int segCount = 0; int segCount = 0;
List<DmSegMobileReply> dmSegList = []; List<DmSegMobileReply> dmSegList = [];
int currentSegIndex = 0; int currentSegIndex = 1;
int currentDmIndex = 0; int currentDmIndex = 0;
void calcSegment() { void calcSegment() {
dmSegList.clear();
// 视频分段数
segCount = (videoDuration.inSeconds / (60 * 6)).ceil(); segCount = (videoDuration.inSeconds / (60 * 6)).ceil();
dmSegList = List<DmSegMobileReply>.generate(
segCount < 1 ? 1 : segCount, (index) => DmSegMobileReply());
// 当前分段
try {
currentSegIndex =
(playerController.position.value.inSeconds / (60 * 6)).ceil();
currentSegIndex = currentSegIndex < 1 ? 1 : currentSegIndex;
} catch (_) {}
} }
Future<List<DmSegMobileReply>> queryDanmaku() async { Future<List<DmSegMobileReply>> queryDanmaku() async {
dmSegList.clear(); // dmSegList.clear();
for (int segIndex = 1; segIndex <= segCount; segIndex++) { DmSegMobileReply result =
DmSegMobileReply result = await DanmakaHttp.queryDanmaku(cid: cid, segmentIndex: currentSegIndex);
await DanmakaHttp.queryDanmaku(cid: cid, segmentIndex: segIndex); if (result.elems.isNotEmpty) {
if (result.elems.isNotEmpty) { result.elems.sort((a, b) => (a.progress).compareTo(b.progress));
result.elems.sort((a, b) => (a.progress).compareTo(b.progress)); // dmSegList.add(result);
dmSegList.add(result); currentSegIndex = currentSegIndex < 1 ? 1 : currentSegIndex;
} dmSegList[currentSegIndex - 1] = result;
} }
if (dmSegList.isNotEmpty) { if (dmSegList.isNotEmpty) {
findClosestPositionIndex(playerController.position.value.inMilliseconds); findClosestPositionIndex(playerController.position.value.inMilliseconds);

View File

@ -1,3 +1,4 @@
import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
@ -88,7 +89,15 @@ class _PlDanmakuState extends State<PlDanmaku> {
PlDanmakuController ctr = _plDanmakuController; PlDanmakuController ctr = _plDanmakuController;
int currentPosition = position.inMilliseconds; int currentPosition = position.inMilliseconds;
blockTypes = playerController.blockTypes; blockTypes = playerController.blockTypes;
// 根据position判断是否有已缓存弹幕。没有则请求对应段
int segIndex = (currentPosition / (6 * 60 * 1000)).ceil();
segIndex = segIndex < 1 ? 1 : segIndex;
if (ctr.dmSegList[segIndex - 1].elems.isEmpty) {
ctr.currentSegIndex = segIndex;
EasyThrottle.throttle('follow', const Duration(seconds: 1), () {
ctr.queryDanmaku();
});
}
if (!playerController.isOpenDanmu.value) { if (!playerController.isOpenDanmu.value) {
return; return;
} }
@ -140,26 +149,30 @@ class _PlDanmakuState extends State<PlDanmaku> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Obx( return LayoutBuilder(builder: (context, box) {
() => AnimatedOpacity( double initDuration = box.maxWidth / 12;
opacity: playerController.isOpenDanmu.value ? 1 : 0, return Obx(
duration: const Duration(milliseconds: 100), () => AnimatedOpacity(
child: DanmakuView( opacity: playerController.isOpenDanmu.value ? 1 : 0,
createdController: (DanmakuController e) async { duration: const Duration(milliseconds: 100),
widget.playerController.danmakuController = _controller = e; child: DanmakuView(
}, createdController: (DanmakuController e) async {
option: DanmakuOption( widget.playerController.danmakuController = _controller = e;
fontSize: 15 * fontSizeVal, },
area: showArea, option: DanmakuOption(
opacity: opacityVal, fontSize: 15 * fontSizeVal,
hideTop: blockTypes.contains(5), area: showArea,
hideScroll: blockTypes.contains(2), opacity: opacityVal,
hideBottom: blockTypes.contains(4), hideTop: blockTypes.contains(5),
duration: danmakuSpeedVal * widget.playerController.playbackSpeed, hideScroll: blockTypes.contains(2),
hideBottom: blockTypes.contains(4),
duration: initDuration /
(danmakuSpeedVal * widget.playerController.playbackSpeed),
),
statusChanged: (isPlaying) {},
), ),
statusChanged: (isPlaying) {},
), ),
), );
); });
} }
} }