Skip to content

Commit

Permalink
mod: findClosestNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
guozhigq committed Apr 4, 2024
1 parent 5500a58 commit ec77626
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/pages/video/detail/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class VideoDetailController extends GetxController
late bool enableCDN;
late int? cacheVideoQa;
late String cacheDecode;
late int cacheAudioQa;
late int defaultAudioQa;

PersistentBottomSheetController? replyReplyBottomSheetCtr;
RxList<SubTitileContentModel> subtitleContents =
Expand Down Expand Up @@ -146,7 +146,7 @@ class VideoDetailController extends GetxController
// 预设的解码格式
cacheDecode = setting.get(SettingBoxKey.defaultDecode,
defaultValue: VideoDecodeFormats.values.last.code);
cacheAudioQa = setting.get(SettingBoxKey.defaultAudioQa,
defaultAudioQa = setting.get(SettingBoxKey.defaultAudioQa,
defaultValue: AudioQuality.hiRes.code);
oid.value = IdUtils.bv2av(Get.parameters['bvid']!);
getSubtitle();
Expand Down Expand Up @@ -353,9 +353,9 @@ class VideoDetailController extends GetxController

if (audiosList.isNotEmpty) {
final List<int> numbers = audiosList.map((map) => map.id!).toList();
int closestNumber = Utils.findClosestNumber(cacheAudioQa, numbers);
if (!numbers.contains(cacheAudioQa) &&
numbers.any((e) => e > cacheAudioQa)) {
int closestNumber = Utils.findClosestNumber(defaultAudioQa, numbers);
if (!numbers.contains(defaultAudioQa) &&
numbers.any((e) => e > defaultAudioQa)) {
closestNumber = 30280;
}
firstAudio = audiosList.firstWhere((e) => e.id == closestNumber);
Expand Down
16 changes: 16 additions & 0 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ class Utils {
static int findClosestNumber(int target, List<int> numbers) {
int minDiff = 127;
int closestNumber = 0; // 初始化为0,表示没有找到比目标值小的整数

// 向下查找
try {
for (int number in numbers) {
if (number < target) {
Expand All @@ -221,6 +223,20 @@ class Utils {
}
}
} catch (_) {}

// 向上查找
if (closestNumber == 0) {
try {
for (int number in numbers) {
int diff = (number - target).abs();

if (diff < minDiff) {
minDiff = diff;
closestNumber = number;
}
}
} catch (_) {}
}
return closestNumber;
}

Expand Down

0 comments on commit ec77626

Please sign in to comment.