67 lines
1.5 KiB
Mathematica
67 lines
1.5 KiB
Mathematica
|
|
//
|
|||
|
|
// SubtitleSync.m
|
|||
|
|
// keyBoard
|
|||
|
|
//
|
|||
|
|
// Created by Mac on 2026/1/15.
|
|||
|
|
//
|
|||
|
|
|
|||
|
|
#import "SubtitleSync.h"
|
|||
|
|
|
|||
|
|
@implementation SubtitleSync
|
|||
|
|
|
|||
|
|
- (NSString *)visibleTextForFullText:(NSString *)fullText
|
|||
|
|
currentTime:(NSTimeInterval)currentTime
|
|||
|
|
duration:(NSTimeInterval)duration {
|
|||
|
|
|
|||
|
|
if (!fullText || fullText.length == 0) {
|
|||
|
|
return @"";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
NSUInteger visibleCount = [self visibleCountForFullText:fullText
|
|||
|
|
currentTime:currentTime
|
|||
|
|
duration:duration];
|
|||
|
|
|
|||
|
|
if (visibleCount >= fullText.length) {
|
|||
|
|
return fullText;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return [fullText substringToIndex:visibleCount];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
- (NSUInteger)visibleCountForFullText:(NSString *)fullText
|
|||
|
|
currentTime:(NSTimeInterval)currentTime
|
|||
|
|
duration:(NSTimeInterval)duration {
|
|||
|
|
|
|||
|
|
if (!fullText || fullText.length == 0) {
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 边界情况处理
|
|||
|
|
if (duration <= 0) {
|
|||
|
|
// 如果没有时长信息,直接返回全部
|
|||
|
|
return fullText.length;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (currentTime <= 0) {
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (currentTime >= duration) {
|
|||
|
|
return fullText.length;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 计算进度比例
|
|||
|
|
double progress = currentTime / duration;
|
|||
|
|
|
|||
|
|
// 计算可见字符数
|
|||
|
|
// 使用略微超前的策略,确保文字不会落后于语音
|
|||
|
|
double adjustedProgress = MIN(progress * 1.05, 1.0);
|
|||
|
|
|
|||
|
|
NSUInteger visibleCount =
|
|||
|
|
(NSUInteger)round(fullText.length * adjustedProgress);
|
|||
|
|
|
|||
|
|
return MIN(visibleCount, fullText.length);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@end
|