├── .gitignore ├── LICENSE ├── LRCParser ├── DDAudioLRCParser.h └── DDAudioLRCParser.m ├── LRCParserDemo ├── LRCParserDemo.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── LRCParserDemo │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── DDAudioLRCParser.h │ ├── DDAudioLRCParser.m │ ├── DDDetailViewController.h │ ├── DDDetailViewController.m │ ├── DDRootViewController.h │ ├── DDRootViewController.m │ ├── Info.plist │ ├── TestLRC1 │ ├── TestLRC2 │ └── main.m └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata 19 | 20 | ## Other 21 | *.xccheckout 22 | *.moved-aside 23 | *.xcuserstate 24 | *.xcscmblueprint 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | 30 | # CocoaPods 31 | # 32 | # We recommend against adding the Pods directory to your .gitignore. However 33 | # you should judge for yourself, the pros and cons are mentioned at: 34 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 35 | # 36 | # Pods/ 37 | 38 | # Carthage 39 | # 40 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 41 | # Carthage/Checkouts 42 | 43 | Carthage/Build 44 | 45 | # fastlane 46 | # 47 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 48 | # screenshots whenever they are needed. 49 | # For more information about the recommended setup visit: 50 | # https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md 51 | 52 | fastlane/report.xml 53 | fastlane/screenshots 54 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 developforapple 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LRCParser/DDAudioLRCParser.h: -------------------------------------------------------------------------------- 1 | // 2 | // DDAudioLRCParser.h 3 | // JuYouQu 4 | // 5 | // Created by Normal on 16/2/19. 6 | // Copyright © 2016年 Bo Wang. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @class DDAudioLRC; 14 | @class DDAudioLRCUnit; 15 | 16 | @interface DDAudioLRCParser : NSObject 17 | + (nullable DDAudioLRC *)parserLRCText:(NSString *)lrc; 18 | @end 19 | 20 | FOUNDATION_EXTERN const NSString *kDDLRCMetadataKeyTI;//歌曲名 21 | FOUNDATION_EXTERN const NSString *kDDLRCMetadataKeyAR;//歌手名 22 | FOUNDATION_EXTERN const NSString *kDDLRCMetadataKeyAL;//专辑 23 | FOUNDATION_EXTERN const NSString *kDDLRCMetadataKeyBY;//编辑者 24 | FOUNDATION_EXTERN const NSString *kDDLRCMetadataKeyOFFSET;//补偿 25 | FOUNDATION_EXTERN const NSString *kDDLRCMetadataKeyTIME;//时长 26 | 27 | @interface DDAudioLRC : NSObject 28 | @property (nullable, strong, nonatomic) NSString *originLRCText; 29 | @property (nullable, strong, nonatomic) NSArray *units; 30 | 31 | // 歌词上附带的一些歌曲数据。根据 keys 取数据,数据是否存在由歌词决定。 32 | - (void)setMetadata:(nullable id)value forKey:(NSString *)key; 33 | - (nullable id)metadataForKey:(NSString *)key; 34 | 35 | // 根据时间返回相应歌词所在行数 36 | - (NSRange)linesAtTimeSecondFrom:(NSTimeInterval)from to:(NSTimeInterval)end; 37 | - (NSUInteger)lineAtTimeSecond:(NSTimeInterval)sec; 38 | 39 | // 根据时间返回相应的歌词 40 | - (nullable NSArray *)LRCUnitsAtTimeSecondFrom:(NSTimeInterval)from to:(NSTimeInterval)end; 41 | - (nullable DDAudioLRCUnit *)LRCUnitAtTimeSecond:(NSTimeInterval)sec; 42 | 43 | // 返回歌词行数对应的歌词内容 44 | - (nullable NSArray *)LRCUnitsAtLines:(NSRange)range; 45 | - (nullable DDAudioLRCUnit *)LRCUnitAtLine:(NSUInteger)line; 46 | @end 47 | 48 | @interface DDAudioLRCUnit : NSObject 49 | @property (nullable, strong, nonatomic) NSString *secString; 50 | @property (assign, readonly, nonatomic) NSTimeInterval sec; 51 | @property (nullable, strong, nonatomic) NSString *lrc; 52 | @end 53 | 54 | NS_ASSUME_NONNULL_END -------------------------------------------------------------------------------- /LRCParser/DDAudioLRCParser.m: -------------------------------------------------------------------------------- 1 | // 2 | // DDAudioLRCParser.m 3 | // JuYouQu 4 | // 5 | // Created by Normal on 16/2/19. 6 | // Copyright © 2016年 Bo Wang. All rights reserved. 7 | // 8 | 9 | #import "DDAudioLRCParser.h" 10 | 11 | @implementation DDAudioLRCParser 12 | 13 | + (DDAudioLRC *)parserLRCText:(NSString *)lrc 14 | { 15 | return [self parser:lrc]; 16 | } 17 | 18 | + (DDAudioLRC *)parser:(NSString *)lrc 19 | { 20 | if (!lrc || lrc.length == 0) { 21 | return nil; 22 | } 23 | 24 | DDAudioLRC *tmp = [DDAudioLRC new]; 25 | 26 | NSArray *tags = @[kDDLRCMetadataKeyTI, 27 | kDDLRCMetadataKeyAR, 28 | kDDLRCMetadataKeyAL, 29 | kDDLRCMetadataKeyBY, 30 | kDDLRCMetadataKeyOFFSET]; 31 | 32 | NSString *reg = @"\\[(\\d{0,2}:\\d{0,2}[.|:]\\d{0,2})\\].*?"; 33 | NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:reg options:NSRegularExpressionCaseInsensitive error:nil]; 34 | NSString *reg2 = @".*\\[\\d{0,2}:\\d{0,2}[.|:]\\d{0,2}\\](.*)"; 35 | NSRegularExpression *re2 = [NSRegularExpression regularExpressionWithPattern:reg2 options:NSRegularExpressionCaseInsensitive error:nil]; 36 | 37 | NSMutableArray *units = [NSMutableArray array]; 38 | 39 | NSArray *lines = [lrc componentsSeparatedByString:@"\n"]; 40 | for (NSString *aLine in lines) { 41 | if (aLine.length <= 1) { 42 | continue; 43 | } 44 | 45 | BOOL isATag = NO; 46 | 47 | //歌曲信息 48 | for (NSString *tag in tags) { 49 | if (![tmp metadataForKey:tag]) { 50 | NSString *prefix = [NSString stringWithFormat:@"[%@:",tag]; 51 | if ([aLine hasPrefix:prefix] && [aLine hasSuffix:@"]"]) { 52 | NSUInteger loc = prefix.length; 53 | NSUInteger len = aLine.length - loc - 1; 54 | NSString *info = [aLine substringWithRange:NSMakeRange(loc, len)]; 55 | [tmp setMetadata:info forKey:tag]; 56 | isATag = YES; 57 | break; 58 | } 59 | } 60 | } 61 | 62 | if (isATag) { 63 | continue; 64 | } 65 | 66 | //歌词信息 67 | NSArray *matches = [re matchesInString:aLine options:0 range:NSMakeRange(0, aLine.length)]; 68 | 69 | for (NSTextCheckingResult *result in matches) { 70 | NSUInteger count = result.numberOfRanges; 71 | if (count >= 2) { 72 | NSRange secRange = [result rangeAtIndex:1]; 73 | NSString *secString = [[aLine substringWithRange:secRange] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 74 | 75 | [re2 enumerateMatchesInString:aLine options:0 range:NSMakeRange(0, aLine.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { 76 | NSUInteger count = result.numberOfRanges; 77 | if (count >= 2) { 78 | NSRange lrcRange = [result rangeAtIndex:1]; 79 | NSString *lrc = [aLine substringWithRange:lrcRange]; 80 | 81 | DDAudioLRCUnit *unit = [DDAudioLRCUnit new]; 82 | unit.secString = secString; 83 | unit.lrc = lrc; 84 | [units addObject:unit]; 85 | } 86 | }]; 87 | } 88 | } 89 | } 90 | tmp.units = [self sortedLRCUnits:units]; 91 | tmp.originLRCText = lrc; 92 | return tmp; 93 | } 94 | 95 | + (NSArray *)sortedLRCUnits:(NSArray *)units 96 | { 97 | //按时间排序 98 | NSArray *sorted = [units sortedArrayUsingComparator:^NSComparisonResult(DDAudioLRCUnit *obj1, DDAudioLRCUnit *obj2) { 99 | if (obj1.sec < obj2.sec) { 100 | return NSOrderedAscending; 101 | }else{ 102 | return NSOrderedDescending; 103 | } 104 | }]; 105 | return sorted; 106 | } 107 | 108 | @end 109 | 110 | NSString *kDDLRCMetadataKeyTI = @"ti"; 111 | NSString *kDDLRCMetadataKeyAR = @"ar"; 112 | NSString *kDDLRCMetadataKeyAL = @"al"; 113 | NSString *kDDLRCMetadataKeyBY = @"by"; 114 | NSString *kDDLRCMetadataKeyOFFSET = @"offset"; 115 | NSString *kDDLRCMetadataKeyTIME = @"t_time"; 116 | 117 | @implementation DDAudioLRC 118 | { 119 | NSMutableDictionary *_medatata; 120 | NSNumber *_offset; 121 | } 122 | 123 | - (void)setMetadata:(id)value forKey:(NSString *)key 124 | { 125 | if (!_medatata) { 126 | _medatata = [NSMutableDictionary dictionary]; 127 | } 128 | if (value && key) { 129 | _medatata[key] = value; 130 | } 131 | } 132 | 133 | - (id)metadataForKey:(NSString *)key 134 | { 135 | return _medatata[key]; 136 | } 137 | 138 | - (float)lrcOffset 139 | { 140 | if (!_offset) { 141 | _offset = [self metadataForKey:kDDLRCMetadataKeyOFFSET]; 142 | } 143 | return _offset.floatValue; 144 | } 145 | 146 | - (NSRange)linesAtTimeSecondFrom:(NSTimeInterval)from to:(NSTimeInterval)to 147 | { 148 | if (from > to) { 149 | return NSMakeRange(NSNotFound, 0); 150 | } 151 | 152 | NSTimeInterval start = from + [self lrcOffset]; 153 | NSTimeInterval end = to + [self lrcOffset]; 154 | 155 | NSMutableIndexSet *indexSets = [NSMutableIndexSet indexSet]; 156 | [self.units enumerateObjectsUsingBlock:^(DDAudioLRCUnit *obj, NSUInteger idx, BOOL *stop) { 157 | if (obj.sec >= start && obj.sec <= end) { 158 | [indexSets addIndex:idx]; 159 | } 160 | if (obj.sec > end) { 161 | *stop = YES; 162 | } 163 | }]; 164 | 165 | NSUInteger firstIndex = [indexSets firstIndex]; 166 | NSUInteger lastIndex = [indexSets lastIndex]; 167 | if (firstIndex > 0) { 168 | firstIndex --; 169 | } 170 | 171 | NSUInteger numberOfLines = lastIndex - firstIndex + 1; 172 | NSRange range = NSMakeRange(firstIndex, numberOfLines); 173 | return range; 174 | } 175 | 176 | - (NSUInteger)lineAtTimeSecond:(NSTimeInterval)sec 177 | { 178 | NSRange range = [self linesAtTimeSecondFrom:sec to:sec+0.001]; 179 | return range.location; 180 | } 181 | 182 | - (NSArray *)LRCUnitsAtTimeSecondFrom:(NSTimeInterval)from to:(NSTimeInterval)end 183 | { 184 | NSRange range = [self linesAtTimeSecondFrom:from to:end]; 185 | return [self LRCUnitsAtLines:range]; 186 | } 187 | 188 | - (DDAudioLRCUnit *)LRCUnitAtTimeSecond:(NSTimeInterval)sec 189 | { 190 | return [[self LRCUnitsAtTimeSecondFrom:sec to:sec+0.001] firstObject]; 191 | } 192 | 193 | - (NSArray *)LRCUnitsAtLines:(NSRange)range 194 | { 195 | return [self safeSubarrayWithRange:range inArray:self.units]; 196 | } 197 | 198 | - (DDAudioLRCUnit *)LRCUnitAtLine:(NSUInteger)line 199 | { 200 | return [[self LRCUnitsAtLines:NSMakeRange(line, 1)] firstObject]; 201 | } 202 | 203 | /** 204 | * @brief 安全的取子数组。根据范围尽量取到合适的子数组,范围越界时也取。 205 | * 206 | * @param range 子数组范围 207 | * @param array 父数组 208 | * 209 | * @return 子数组 210 | */ 211 | - (NSArray *)safeSubarrayWithRange:(NSRange)range inArray:(NSArray *)array 212 | { 213 | if (range.location == NSNotFound || !array || array.count == 0) { 214 | return nil; 215 | } 216 | 217 | NSUInteger count = array.count; 218 | NSUInteger loc = range.location; 219 | NSUInteger len = range.length; 220 | 221 | if (loc >= count) { 222 | return nil; 223 | } 224 | if (count - loc < len) { //防止整形溢出 225 | len = count - loc; 226 | } 227 | NSRange safeRange = NSMakeRange(loc, len); 228 | return [array subarrayWithRange:safeRange]; 229 | } 230 | 231 | - (NSString *)description 232 | { 233 | return [NSString stringWithFormat:@"%@\nmetedata:%@\n%@",[super description],_medatata,self.units]; 234 | } 235 | 236 | @end 237 | 238 | @implementation DDAudioLRCUnit 239 | 240 | - (void)setSecString:(NSString *)secString 241 | { 242 | _secString = secString; 243 | 244 | NSInteger m = 0; //分 245 | NSInteger s = 0; //秒 246 | NSInteger ms = 0; //毫秒 247 | 248 | NSScanner *scanner = [NSScanner scannerWithString:secString]; 249 | NSCharacterSet *set = [NSCharacterSet decimalDigitCharacterSet]; 250 | 251 | [scanner scanUpToCharactersFromSet:set intoString:nil]; 252 | [scanner scanInteger:&m]; 253 | [scanner scanUpToCharactersFromSet:set intoString:nil]; 254 | [scanner scanInteger:&s]; 255 | [scanner scanUpToCharactersFromSet:set intoString:nil]; 256 | [scanner scanInteger:&ms]; 257 | 258 | NSTimeInterval time = m*60 + s + ms*0.01; 259 | self->_sec = time; 260 | } 261 | 262 | - (NSString *)description 263 | { 264 | return [NSString stringWithFormat:@"%@, timeSecond:%.0f , LRC:%@",[super description],self.sec,self.lrc]; 265 | } 266 | @end -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 985836911C7E9B66007E6CB5 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 985836901C7E9B66007E6CB5 /* main.m */; }; 11 | 985836941C7E9B66007E6CB5 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 985836931C7E9B66007E6CB5 /* AppDelegate.m */; }; 12 | 9858369A1C7E9B66007E6CB5 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 985836981C7E9B66007E6CB5 /* Main.storyboard */; }; 13 | 9858369C1C7E9B66007E6CB5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9858369B1C7E9B66007E6CB5 /* Assets.xcassets */; }; 14 | 9858369F1C7E9B66007E6CB5 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9858369D1C7E9B66007E6CB5 /* LaunchScreen.storyboard */; }; 15 | 985836A81C7E9B7E007E6CB5 /* DDAudioLRCParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 985836A71C7E9B7E007E6CB5 /* DDAudioLRCParser.m */; }; 16 | 985836AA1C7ECC73007E6CB5 /* TestLRC1 in Resources */ = {isa = PBXBuildFile; fileRef = 985836A91C7ECC73007E6CB5 /* TestLRC1 */; }; 17 | 985836AC1C7ECD04007E6CB5 /* TestLRC2 in Resources */ = {isa = PBXBuildFile; fileRef = 985836AB1C7ECD04007E6CB5 /* TestLRC2 */; }; 18 | 985836B01C7ECEA8007E6CB5 /* DDRootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 985836AF1C7ECEA8007E6CB5 /* DDRootViewController.m */; }; 19 | 985836B31C7ECEB8007E6CB5 /* DDDetailViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 985836B21C7ECEB8007E6CB5 /* DDDetailViewController.m */; }; 20 | /* End PBXBuildFile section */ 21 | 22 | /* Begin PBXFileReference section */ 23 | 9858368C1C7E9B66007E6CB5 /* LRCParserDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LRCParserDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 24 | 985836901C7E9B66007E6CB5 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 25 | 985836921C7E9B66007E6CB5 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 26 | 985836931C7E9B66007E6CB5 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 27 | 985836991C7E9B66007E6CB5 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 28 | 9858369B1C7E9B66007E6CB5 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 29 | 9858369E1C7E9B66007E6CB5 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 30 | 985836A01C7E9B66007E6CB5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 31 | 985836A61C7E9B7E007E6CB5 /* DDAudioLRCParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DDAudioLRCParser.h; sourceTree = ""; }; 32 | 985836A71C7E9B7E007E6CB5 /* DDAudioLRCParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DDAudioLRCParser.m; sourceTree = ""; }; 33 | 985836A91C7ECC73007E6CB5 /* TestLRC1 */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = TestLRC1; sourceTree = ""; }; 34 | 985836AB1C7ECD04007E6CB5 /* TestLRC2 */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = TestLRC2; sourceTree = ""; }; 35 | 985836AE1C7ECEA8007E6CB5 /* DDRootViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DDRootViewController.h; sourceTree = ""; }; 36 | 985836AF1C7ECEA8007E6CB5 /* DDRootViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DDRootViewController.m; sourceTree = ""; }; 37 | 985836B11C7ECEB8007E6CB5 /* DDDetailViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DDDetailViewController.h; sourceTree = ""; }; 38 | 985836B21C7ECEB8007E6CB5 /* DDDetailViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DDDetailViewController.m; sourceTree = ""; }; 39 | /* End PBXFileReference section */ 40 | 41 | /* Begin PBXFrameworksBuildPhase section */ 42 | 985836891C7E9B66007E6CB5 /* Frameworks */ = { 43 | isa = PBXFrameworksBuildPhase; 44 | buildActionMask = 2147483647; 45 | files = ( 46 | ); 47 | runOnlyForDeploymentPostprocessing = 0; 48 | }; 49 | /* End PBXFrameworksBuildPhase section */ 50 | 51 | /* Begin PBXGroup section */ 52 | 985836831C7E9B66007E6CB5 = { 53 | isa = PBXGroup; 54 | children = ( 55 | 9858368E1C7E9B66007E6CB5 /* LRCParserDemo */, 56 | 9858368D1C7E9B66007E6CB5 /* Products */, 57 | ); 58 | sourceTree = ""; 59 | }; 60 | 9858368D1C7E9B66007E6CB5 /* Products */ = { 61 | isa = PBXGroup; 62 | children = ( 63 | 9858368C1C7E9B66007E6CB5 /* LRCParserDemo.app */, 64 | ); 65 | name = Products; 66 | sourceTree = ""; 67 | }; 68 | 9858368E1C7E9B66007E6CB5 /* LRCParserDemo */ = { 69 | isa = PBXGroup; 70 | children = ( 71 | 985836A61C7E9B7E007E6CB5 /* DDAudioLRCParser.h */, 72 | 985836A71C7E9B7E007E6CB5 /* DDAudioLRCParser.m */, 73 | 985836921C7E9B66007E6CB5 /* AppDelegate.h */, 74 | 985836931C7E9B66007E6CB5 /* AppDelegate.m */, 75 | 985836AD1C7ECE83007E6CB5 /* UI */, 76 | 9858369B1C7E9B66007E6CB5 /* Assets.xcassets */, 77 | 9858369D1C7E9B66007E6CB5 /* LaunchScreen.storyboard */, 78 | 985836A01C7E9B66007E6CB5 /* Info.plist */, 79 | 9858368F1C7E9B66007E6CB5 /* Supporting Files */, 80 | 985836A91C7ECC73007E6CB5 /* TestLRC1 */, 81 | 985836AB1C7ECD04007E6CB5 /* TestLRC2 */, 82 | ); 83 | path = LRCParserDemo; 84 | sourceTree = ""; 85 | }; 86 | 9858368F1C7E9B66007E6CB5 /* Supporting Files */ = { 87 | isa = PBXGroup; 88 | children = ( 89 | 985836901C7E9B66007E6CB5 /* main.m */, 90 | ); 91 | name = "Supporting Files"; 92 | sourceTree = ""; 93 | }; 94 | 985836AD1C7ECE83007E6CB5 /* UI */ = { 95 | isa = PBXGroup; 96 | children = ( 97 | 985836981C7E9B66007E6CB5 /* Main.storyboard */, 98 | 985836AE1C7ECEA8007E6CB5 /* DDRootViewController.h */, 99 | 985836AF1C7ECEA8007E6CB5 /* DDRootViewController.m */, 100 | 985836B11C7ECEB8007E6CB5 /* DDDetailViewController.h */, 101 | 985836B21C7ECEB8007E6CB5 /* DDDetailViewController.m */, 102 | ); 103 | name = UI; 104 | sourceTree = ""; 105 | }; 106 | /* End PBXGroup section */ 107 | 108 | /* Begin PBXNativeTarget section */ 109 | 9858368B1C7E9B66007E6CB5 /* LRCParserDemo */ = { 110 | isa = PBXNativeTarget; 111 | buildConfigurationList = 985836A31C7E9B66007E6CB5 /* Build configuration list for PBXNativeTarget "LRCParserDemo" */; 112 | buildPhases = ( 113 | 985836881C7E9B66007E6CB5 /* Sources */, 114 | 985836891C7E9B66007E6CB5 /* Frameworks */, 115 | 9858368A1C7E9B66007E6CB5 /* Resources */, 116 | ); 117 | buildRules = ( 118 | ); 119 | dependencies = ( 120 | ); 121 | name = LRCParserDemo; 122 | productName = LRCParserDemo; 123 | productReference = 9858368C1C7E9B66007E6CB5 /* LRCParserDemo.app */; 124 | productType = "com.apple.product-type.application"; 125 | }; 126 | /* End PBXNativeTarget section */ 127 | 128 | /* Begin PBXProject section */ 129 | 985836841C7E9B66007E6CB5 /* Project object */ = { 130 | isa = PBXProject; 131 | attributes = { 132 | LastUpgradeCheck = 0720; 133 | ORGANIZATIONNAME = wwwbbat; 134 | TargetAttributes = { 135 | 9858368B1C7E9B66007E6CB5 = { 136 | CreatedOnToolsVersion = 7.2.1; 137 | }; 138 | }; 139 | }; 140 | buildConfigurationList = 985836871C7E9B66007E6CB5 /* Build configuration list for PBXProject "LRCParserDemo" */; 141 | compatibilityVersion = "Xcode 3.2"; 142 | developmentRegion = English; 143 | hasScannedForEncodings = 0; 144 | knownRegions = ( 145 | en, 146 | Base, 147 | ); 148 | mainGroup = 985836831C7E9B66007E6CB5; 149 | productRefGroup = 9858368D1C7E9B66007E6CB5 /* Products */; 150 | projectDirPath = ""; 151 | projectRoot = ""; 152 | targets = ( 153 | 9858368B1C7E9B66007E6CB5 /* LRCParserDemo */, 154 | ); 155 | }; 156 | /* End PBXProject section */ 157 | 158 | /* Begin PBXResourcesBuildPhase section */ 159 | 9858368A1C7E9B66007E6CB5 /* Resources */ = { 160 | isa = PBXResourcesBuildPhase; 161 | buildActionMask = 2147483647; 162 | files = ( 163 | 9858369F1C7E9B66007E6CB5 /* LaunchScreen.storyboard in Resources */, 164 | 9858369C1C7E9B66007E6CB5 /* Assets.xcassets in Resources */, 165 | 985836AA1C7ECC73007E6CB5 /* TestLRC1 in Resources */, 166 | 9858369A1C7E9B66007E6CB5 /* Main.storyboard in Resources */, 167 | 985836AC1C7ECD04007E6CB5 /* TestLRC2 in Resources */, 168 | ); 169 | runOnlyForDeploymentPostprocessing = 0; 170 | }; 171 | /* End PBXResourcesBuildPhase section */ 172 | 173 | /* Begin PBXSourcesBuildPhase section */ 174 | 985836881C7E9B66007E6CB5 /* Sources */ = { 175 | isa = PBXSourcesBuildPhase; 176 | buildActionMask = 2147483647; 177 | files = ( 178 | 985836B01C7ECEA8007E6CB5 /* DDRootViewController.m in Sources */, 179 | 985836A81C7E9B7E007E6CB5 /* DDAudioLRCParser.m in Sources */, 180 | 985836941C7E9B66007E6CB5 /* AppDelegate.m in Sources */, 181 | 985836911C7E9B66007E6CB5 /* main.m in Sources */, 182 | 985836B31C7ECEB8007E6CB5 /* DDDetailViewController.m in Sources */, 183 | ); 184 | runOnlyForDeploymentPostprocessing = 0; 185 | }; 186 | /* End PBXSourcesBuildPhase section */ 187 | 188 | /* Begin PBXVariantGroup section */ 189 | 985836981C7E9B66007E6CB5 /* Main.storyboard */ = { 190 | isa = PBXVariantGroup; 191 | children = ( 192 | 985836991C7E9B66007E6CB5 /* Base */, 193 | ); 194 | name = Main.storyboard; 195 | sourceTree = ""; 196 | }; 197 | 9858369D1C7E9B66007E6CB5 /* LaunchScreen.storyboard */ = { 198 | isa = PBXVariantGroup; 199 | children = ( 200 | 9858369E1C7E9B66007E6CB5 /* Base */, 201 | ); 202 | name = LaunchScreen.storyboard; 203 | sourceTree = ""; 204 | }; 205 | /* End PBXVariantGroup section */ 206 | 207 | /* Begin XCBuildConfiguration section */ 208 | 985836A11C7E9B66007E6CB5 /* Debug */ = { 209 | isa = XCBuildConfiguration; 210 | buildSettings = { 211 | ALWAYS_SEARCH_USER_PATHS = NO; 212 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 213 | CLANG_CXX_LIBRARY = "libc++"; 214 | CLANG_ENABLE_MODULES = YES; 215 | CLANG_ENABLE_OBJC_ARC = YES; 216 | CLANG_WARN_BOOL_CONVERSION = YES; 217 | CLANG_WARN_CONSTANT_CONVERSION = YES; 218 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 219 | CLANG_WARN_EMPTY_BODY = YES; 220 | CLANG_WARN_ENUM_CONVERSION = YES; 221 | CLANG_WARN_INT_CONVERSION = YES; 222 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 223 | CLANG_WARN_UNREACHABLE_CODE = YES; 224 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 225 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 226 | COPY_PHASE_STRIP = NO; 227 | DEBUG_INFORMATION_FORMAT = dwarf; 228 | ENABLE_STRICT_OBJC_MSGSEND = YES; 229 | ENABLE_TESTABILITY = YES; 230 | GCC_C_LANGUAGE_STANDARD = gnu99; 231 | GCC_DYNAMIC_NO_PIC = NO; 232 | GCC_NO_COMMON_BLOCKS = YES; 233 | GCC_OPTIMIZATION_LEVEL = 0; 234 | GCC_PREPROCESSOR_DEFINITIONS = ( 235 | "DEBUG=1", 236 | "$(inherited)", 237 | ); 238 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 239 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 240 | GCC_WARN_UNDECLARED_SELECTOR = YES; 241 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 242 | GCC_WARN_UNUSED_FUNCTION = YES; 243 | GCC_WARN_UNUSED_VARIABLE = YES; 244 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 245 | MTL_ENABLE_DEBUG_INFO = YES; 246 | ONLY_ACTIVE_ARCH = YES; 247 | SDKROOT = iphoneos; 248 | }; 249 | name = Debug; 250 | }; 251 | 985836A21C7E9B66007E6CB5 /* Release */ = { 252 | isa = XCBuildConfiguration; 253 | buildSettings = { 254 | ALWAYS_SEARCH_USER_PATHS = NO; 255 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 256 | CLANG_CXX_LIBRARY = "libc++"; 257 | CLANG_ENABLE_MODULES = YES; 258 | CLANG_ENABLE_OBJC_ARC = YES; 259 | CLANG_WARN_BOOL_CONVERSION = YES; 260 | CLANG_WARN_CONSTANT_CONVERSION = YES; 261 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 262 | CLANG_WARN_EMPTY_BODY = YES; 263 | CLANG_WARN_ENUM_CONVERSION = YES; 264 | CLANG_WARN_INT_CONVERSION = YES; 265 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 266 | CLANG_WARN_UNREACHABLE_CODE = YES; 267 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 268 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 269 | COPY_PHASE_STRIP = NO; 270 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 271 | ENABLE_NS_ASSERTIONS = NO; 272 | ENABLE_STRICT_OBJC_MSGSEND = YES; 273 | GCC_C_LANGUAGE_STANDARD = gnu99; 274 | GCC_NO_COMMON_BLOCKS = YES; 275 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 276 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 277 | GCC_WARN_UNDECLARED_SELECTOR = YES; 278 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 279 | GCC_WARN_UNUSED_FUNCTION = YES; 280 | GCC_WARN_UNUSED_VARIABLE = YES; 281 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 282 | MTL_ENABLE_DEBUG_INFO = NO; 283 | SDKROOT = iphoneos; 284 | VALIDATE_PRODUCT = YES; 285 | }; 286 | name = Release; 287 | }; 288 | 985836A41C7E9B66007E6CB5 /* Debug */ = { 289 | isa = XCBuildConfiguration; 290 | buildSettings = { 291 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 292 | INFOPLIST_FILE = LRCParserDemo/Info.plist; 293 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 294 | PRODUCT_BUNDLE_IDENTIFIER = wwwbbat.LRCParserDemo; 295 | PRODUCT_NAME = "$(TARGET_NAME)"; 296 | }; 297 | name = Debug; 298 | }; 299 | 985836A51C7E9B66007E6CB5 /* Release */ = { 300 | isa = XCBuildConfiguration; 301 | buildSettings = { 302 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 303 | INFOPLIST_FILE = LRCParserDemo/Info.plist; 304 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 305 | PRODUCT_BUNDLE_IDENTIFIER = wwwbbat.LRCParserDemo; 306 | PRODUCT_NAME = "$(TARGET_NAME)"; 307 | }; 308 | name = Release; 309 | }; 310 | /* End XCBuildConfiguration section */ 311 | 312 | /* Begin XCConfigurationList section */ 313 | 985836871C7E9B66007E6CB5 /* Build configuration list for PBXProject "LRCParserDemo" */ = { 314 | isa = XCConfigurationList; 315 | buildConfigurations = ( 316 | 985836A11C7E9B66007E6CB5 /* Debug */, 317 | 985836A21C7E9B66007E6CB5 /* Release */, 318 | ); 319 | defaultConfigurationIsVisible = 0; 320 | defaultConfigurationName = Release; 321 | }; 322 | 985836A31C7E9B66007E6CB5 /* Build configuration list for PBXNativeTarget "LRCParserDemo" */ = { 323 | isa = XCConfigurationList; 324 | buildConfigurations = ( 325 | 985836A41C7E9B66007E6CB5 /* Debug */, 326 | 985836A51C7E9B66007E6CB5 /* Release */, 327 | ); 328 | defaultConfigurationIsVisible = 0; 329 | }; 330 | /* End XCConfigurationList section */ 331 | }; 332 | rootObject = 985836841C7E9B66007E6CB5 /* Project object */; 333 | } 334 | -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // LRCParserDemo 4 | // 5 | // Created by Normal on 16/2/25. 6 | // Copyright © 2016年 wwwbbat. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // LRCParserDemo 4 | // 5 | // Created by Normal on 16/2/25. 6 | // Copyright © 2016年 wwwbbat. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 93 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/DDAudioLRCParser.h: -------------------------------------------------------------------------------- 1 | // 2 | // DDAudioLRCParser.h 3 | // JuYouQu 4 | // 5 | // Created by Normal on 16/2/19. 6 | // Copyright © 2016年 Bo Wang. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @class DDAudioLRC; 14 | @class DDAudioLRCUnit; 15 | 16 | @interface DDAudioLRCParser : NSObject 17 | + (nullable DDAudioLRC *)parserLRCText:(NSString *)lrc; 18 | @end 19 | 20 | FOUNDATION_EXTERN const NSString *kDDLRCMetadataKeyTI;//歌曲名 21 | FOUNDATION_EXTERN const NSString *kDDLRCMetadataKeyAR;//歌手名 22 | FOUNDATION_EXTERN const NSString *kDDLRCMetadataKeyAL;//专辑 23 | FOUNDATION_EXTERN const NSString *kDDLRCMetadataKeyBY;//编辑者 24 | FOUNDATION_EXTERN const NSString *kDDLRCMetadataKeyOFFSET;//补偿 25 | FOUNDATION_EXTERN const NSString *kDDLRCMetadataKeyTIME;//时长 26 | 27 | @interface DDAudioLRC : NSObject 28 | @property (nullable, strong, nonatomic) NSString *originLRCText; 29 | @property (nullable, strong, nonatomic) NSArray *units; 30 | 31 | // 歌词上附带的一些歌曲数据。根据 keys 取数据,数据是否存在由歌词决定。 32 | - (void)setMetadata:(nullable id)value forKey:(NSString *)key; 33 | - (nullable id)metadataForKey:(NSString *)key; 34 | 35 | // 根据时间返回相应歌词所在行数 36 | - (NSRange)linesAtTimeSecondFrom:(NSTimeInterval)from to:(NSTimeInterval)end; 37 | - (NSUInteger)lineAtTimeSecond:(NSTimeInterval)sec; 38 | 39 | // 根据时间返回相应的歌词 40 | - (nullable NSArray *)LRCUnitsAtTimeSecondFrom:(NSTimeInterval)from to:(NSTimeInterval)end; 41 | - (nullable DDAudioLRCUnit *)LRCUnitAtTimeSecond:(NSTimeInterval)sec; 42 | 43 | // 返回歌词行数对应的歌词内容 44 | - (nullable NSArray *)LRCUnitsAtLines:(NSRange)range; 45 | - (nullable DDAudioLRCUnit *)LRCUnitAtLine:(NSUInteger)line; 46 | @end 47 | 48 | @interface DDAudioLRCUnit : NSObject 49 | @property (nullable, strong, nonatomic) NSString *secString; 50 | @property (assign, readonly, nonatomic) NSTimeInterval sec; 51 | @property (nullable, strong, nonatomic) NSString *lrc; 52 | @end 53 | 54 | NS_ASSUME_NONNULL_END -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/DDAudioLRCParser.m: -------------------------------------------------------------------------------- 1 | // 2 | // DDAudioLRCParser.m 3 | // JuYouQu 4 | // 5 | // Created by Normal on 16/2/19. 6 | // Copyright © 2016年 Bo Wang. All rights reserved. 7 | // 8 | 9 | #import "DDAudioLRCParser.h" 10 | 11 | @implementation DDAudioLRCParser 12 | 13 | + (DDAudioLRC *)parserLRCText:(NSString *)lrc 14 | { 15 | return [self parser:lrc]; 16 | } 17 | 18 | + (DDAudioLRC *)parser:(NSString *)lrc 19 | { 20 | if (!lrc || lrc.length == 0) { 21 | return nil; 22 | } 23 | 24 | DDAudioLRC *tmp = [DDAudioLRC new]; 25 | 26 | NSArray *tags = @[kDDLRCMetadataKeyTI, 27 | kDDLRCMetadataKeyAR, 28 | kDDLRCMetadataKeyAL, 29 | kDDLRCMetadataKeyBY, 30 | kDDLRCMetadataKeyOFFSET]; 31 | 32 | NSString *reg = @"\\[(\\d{0,2}:\\d{0,2}[.|:]\\d{0,2})\\].*?"; 33 | NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:reg options:NSRegularExpressionCaseInsensitive error:nil]; 34 | NSString *reg2 = @".*\\[\\d{0,2}:\\d{0,2}[.|:]\\d{0,2}\\](.*)"; 35 | NSRegularExpression *re2 = [NSRegularExpression regularExpressionWithPattern:reg2 options:NSRegularExpressionCaseInsensitive error:nil]; 36 | 37 | NSMutableArray *units = [NSMutableArray array]; 38 | 39 | NSArray *lines = [lrc componentsSeparatedByString:@"\n"]; 40 | for (NSString *aLine in lines) { 41 | if (aLine.length <= 1) { 42 | continue; 43 | } 44 | 45 | BOOL isATag = NO; 46 | 47 | //歌曲信息 48 | for (NSString *tag in tags) { 49 | if (![tmp metadataForKey:tag]) { 50 | NSString *prefix = [NSString stringWithFormat:@"[%@:",tag]; 51 | if ([aLine hasPrefix:prefix] && [aLine hasSuffix:@"]"]) { 52 | NSUInteger loc = prefix.length; 53 | NSUInteger len = aLine.length - loc - 1; 54 | NSString *info = [aLine substringWithRange:NSMakeRange(loc, len)]; 55 | [tmp setMetadata:info forKey:tag]; 56 | isATag = YES; 57 | break; 58 | } 59 | } 60 | } 61 | 62 | if (isATag) { 63 | continue; 64 | } 65 | 66 | //歌词信息 67 | NSArray *matches = [re matchesInString:aLine options:0 range:NSMakeRange(0, aLine.length)]; 68 | 69 | for (NSTextCheckingResult *result in matches) { 70 | NSUInteger count = result.numberOfRanges; 71 | if (count >= 2) { 72 | NSRange secRange = [result rangeAtIndex:1]; 73 | NSString *secString = [[aLine substringWithRange:secRange] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 74 | 75 | [re2 enumerateMatchesInString:aLine options:0 range:NSMakeRange(0, aLine.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { 76 | NSUInteger count = result.numberOfRanges; 77 | if (count >= 2) { 78 | NSRange lrcRange = [result rangeAtIndex:1]; 79 | NSString *lrc = [aLine substringWithRange:lrcRange]; 80 | 81 | DDAudioLRCUnit *unit = [DDAudioLRCUnit new]; 82 | unit.secString = secString; 83 | unit.lrc = lrc; 84 | [units addObject:unit]; 85 | } 86 | }]; 87 | } 88 | } 89 | } 90 | tmp.units = [self sortedLRCUnits:units]; 91 | tmp.originLRCText = lrc; 92 | return tmp; 93 | } 94 | 95 | + (NSArray *)sortedLRCUnits:(NSArray *)units 96 | { 97 | //按时间排序 98 | NSArray *sorted = [units sortedArrayUsingComparator:^NSComparisonResult(DDAudioLRCUnit *obj1, DDAudioLRCUnit *obj2) { 99 | if (obj1.sec < obj2.sec) { 100 | return NSOrderedAscending; 101 | }else{ 102 | return NSOrderedDescending; 103 | } 104 | }]; 105 | return sorted; 106 | } 107 | 108 | @end 109 | 110 | NSString *kDDLRCMetadataKeyTI = @"ti"; 111 | NSString *kDDLRCMetadataKeyAR = @"ar"; 112 | NSString *kDDLRCMetadataKeyAL = @"al"; 113 | NSString *kDDLRCMetadataKeyBY = @"by"; 114 | NSString *kDDLRCMetadataKeyOFFSET = @"offset"; 115 | NSString *kDDLRCMetadataKeyTIME = @"t_time"; 116 | 117 | @implementation DDAudioLRC 118 | { 119 | NSMutableDictionary *_medatata; 120 | NSNumber *_offset; 121 | } 122 | 123 | - (void)setMetadata:(id)value forKey:(NSString *)key 124 | { 125 | if (!_medatata) { 126 | _medatata = [NSMutableDictionary dictionary]; 127 | } 128 | if (value && key) { 129 | _medatata[key] = value; 130 | } 131 | } 132 | 133 | - (id)metadataForKey:(NSString *)key 134 | { 135 | return _medatata[key]; 136 | } 137 | 138 | - (float)lrcOffset 139 | { 140 | if (!_offset) { 141 | _offset = [self metadataForKey:kDDLRCMetadataKeyOFFSET]; 142 | } 143 | return _offset.floatValue; 144 | } 145 | 146 | - (NSRange)linesAtTimeSecondFrom:(NSTimeInterval)from to:(NSTimeInterval)to 147 | { 148 | if (from > to) { 149 | return NSMakeRange(NSNotFound, 0); 150 | } 151 | 152 | NSTimeInterval start = from + [self lrcOffset]; 153 | NSTimeInterval end = to + [self lrcOffset]; 154 | 155 | NSMutableIndexSet *indexSets = [NSMutableIndexSet indexSet]; 156 | [self.units enumerateObjectsUsingBlock:^(DDAudioLRCUnit *obj, NSUInteger idx, BOOL *stop) { 157 | if (obj.sec >= start && obj.sec <= end) { 158 | [indexSets addIndex:idx]; 159 | } 160 | if (obj.sec > end) { 161 | *stop = YES; 162 | } 163 | }]; 164 | 165 | NSUInteger firstIndex = [indexSets firstIndex]; 166 | NSUInteger lastIndex = [indexSets lastIndex]; 167 | if (firstIndex > 0) { 168 | firstIndex --; 169 | } 170 | 171 | NSUInteger numberOfLines = lastIndex - firstIndex + 1; 172 | NSRange range = NSMakeRange(firstIndex, numberOfLines); 173 | return range; 174 | } 175 | 176 | - (NSUInteger)lineAtTimeSecond:(NSTimeInterval)sec 177 | { 178 | NSRange range = [self linesAtTimeSecondFrom:sec to:sec+0.001]; 179 | return range.location; 180 | } 181 | 182 | - (NSArray *)LRCUnitsAtTimeSecondFrom:(NSTimeInterval)from to:(NSTimeInterval)end 183 | { 184 | NSRange range = [self linesAtTimeSecondFrom:from to:end]; 185 | return [self LRCUnitsAtLines:range]; 186 | } 187 | 188 | - (DDAudioLRCUnit *)LRCUnitAtTimeSecond:(NSTimeInterval)sec 189 | { 190 | return [[self LRCUnitsAtTimeSecondFrom:sec to:sec+0.001] firstObject]; 191 | } 192 | 193 | - (NSArray *)LRCUnitsAtLines:(NSRange)range 194 | { 195 | return [self safeSubarrayWithRange:range inArray:self.units]; 196 | } 197 | 198 | - (DDAudioLRCUnit *)LRCUnitAtLine:(NSUInteger)line 199 | { 200 | return [[self LRCUnitsAtLines:NSMakeRange(line, 1)] firstObject]; 201 | } 202 | 203 | /** 204 | * @brief 安全的取子数组。根据范围尽量取到合适的子数组,范围越界时也取。 205 | * 206 | * @param range 子数组范围 207 | * @param array 父数组 208 | * 209 | * @return 子数组 210 | */ 211 | - (NSArray *)safeSubarrayWithRange:(NSRange)range inArray:(NSArray *)array 212 | { 213 | if (range.location == NSNotFound || !array || array.count == 0) { 214 | return nil; 215 | } 216 | 217 | NSUInteger count = array.count; 218 | NSUInteger loc = range.location; 219 | NSUInteger len = range.length; 220 | 221 | if (loc >= count) { 222 | return nil; 223 | } 224 | if (count - loc < len) { //防止整形溢出 225 | len = count - loc; 226 | } 227 | NSRange safeRange = NSMakeRange(loc, len); 228 | return [array subarrayWithRange:safeRange]; 229 | } 230 | 231 | - (NSString *)description 232 | { 233 | return [NSString stringWithFormat:@"%@\nmetedata:%@\n%@",[super description],_medatata,self.units]; 234 | } 235 | 236 | @end 237 | 238 | @implementation DDAudioLRCUnit 239 | 240 | - (void)setSecString:(NSString *)secString 241 | { 242 | _secString = secString; 243 | 244 | NSInteger m = 0; //分 245 | NSInteger s = 0; //秒 246 | NSInteger ms = 0; //毫秒 247 | 248 | NSScanner *scanner = [NSScanner scannerWithString:secString]; 249 | NSCharacterSet *set = [NSCharacterSet decimalDigitCharacterSet]; 250 | 251 | [scanner scanUpToCharactersFromSet:set intoString:nil]; 252 | [scanner scanInteger:&m]; 253 | [scanner scanUpToCharactersFromSet:set intoString:nil]; 254 | [scanner scanInteger:&s]; 255 | [scanner scanUpToCharactersFromSet:set intoString:nil]; 256 | [scanner scanInteger:&ms]; 257 | 258 | NSTimeInterval time = m*60 + s + ms*0.01; 259 | self->_sec = time; 260 | } 261 | 262 | - (NSString *)description 263 | { 264 | return [NSString stringWithFormat:@"%@, timeSecond:%.0f , LRC:%@",[super description],self.sec,self.lrc]; 265 | } 266 | @end -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/DDDetailViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // DDDetailViewController.h 3 | // LRCParserDemo 4 | // 5 | // Created by Normal on 16/2/25. 6 | // Copyright © 2016年 wwwbbat. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "DDAudioLRCParser.h" 11 | 12 | @interface DDDetailViewController : UITableViewController 13 | @property (strong, nonatomic) DDAudioLRC *lrc; 14 | @end 15 | -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/DDDetailViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // DDDetailViewController.m 3 | // LRCParserDemo 4 | // 5 | // Created by Normal on 16/2/25. 6 | // Copyright © 2016年 wwwbbat. All rights reserved. 7 | // 8 | 9 | #import "DDDetailViewController.h" 10 | 11 | @interface DDDetailViewController () 12 | 13 | @end 14 | 15 | @implementation DDDetailViewController 16 | 17 | - (void)viewDidLoad 18 | { 19 | [super viewDidLoad]; 20 | 21 | 22 | NSTimeInterval start = 20.f; 23 | NSTimeInterval end = 46.f; 24 | 25 | NSArray *lrcs = [self.lrc LRCUnitsAtTimeSecondFrom:start to:end]; 26 | NSLog(@"lrcs"); 27 | } 28 | 29 | #pragma mark - Table view data source 30 | 31 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 32 | { 33 | return self.lrc.units.count; 34 | } 35 | 36 | 37 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 38 | { 39 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath]; 40 | 41 | DDAudioLRCUnit *unit = [self.lrc LRCUnitAtLine:indexPath.row]; 42 | cell.textLabel.text = [NSString stringWithFormat:@"%.0f",unit.sec]; 43 | cell.detailTextLabel.text = unit.lrc; 44 | return cell; 45 | } 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/DDRootViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // DDRootViewController.h 3 | // LRCParserDemo 4 | // 5 | // Created by Normal on 16/2/25. 6 | // Copyright © 2016年 wwwbbat. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface DDRootViewController : UITableViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/DDRootViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // DDRootViewController.m 3 | // LRCParserDemo 4 | // 5 | // Created by Normal on 16/2/25. 6 | // Copyright © 2016年 wwwbbat. All rights reserved. 7 | // 8 | 9 | #import "DDRootViewController.h" 10 | #import "DDAudioLRCParser.h" 11 | #import "DDDetailViewController.h" 12 | 13 | @interface DDRootViewController () 14 | @end 15 | 16 | @implementation DDRootViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | } 21 | 22 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 23 | { 24 | if (indexPath.row == 0) { 25 | NSString *path = [[NSBundle mainBundle] pathForResource:@"TestLRC1" ofType:@""]; 26 | NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; 27 | 28 | DDAudioLRC *lrc = [DDAudioLRCParser parserLRCText:text]; 29 | [self performSegueWithIdentifier:@"SegueIdentifier" sender:lrc]; 30 | }else{ 31 | NSString *path = [[NSBundle mainBundle] pathForResource:@"TestLRC2" ofType:@""]; 32 | NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; 33 | 34 | DDAudioLRC *lrc = [DDAudioLRCParser parserLRCText:text]; 35 | [self performSegueWithIdentifier:@"SegueIdentifier" sender:lrc]; 36 | } 37 | } 38 | 39 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 40 | { 41 | if ([segue.identifier isEqualToString:@"SegueIdentifier"]) { 42 | DDDetailViewController *vc = segue.destinationViewController; 43 | vc.lrc = sender; 44 | } 45 | } 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/TestLRC1: -------------------------------------------------------------------------------- 1 | [ti:友情岁月(Live)] 2 | [ar:李克勤] 3 | [al:我是歌手第四季 第六期] 4 | [t_time:(04:03)] 5 | [00:00.00] 李克勤 - 友情岁月(Live) 6 | [00:03.00] (我是歌手第四季 第六期) 7 | [00:06.00] 作词:刘卓辉 8 | [00:09.00] 作曲:陈光荣 9 | [00:12.00] 编曲:Johnny Yim 10 | [00:15.00] 原唱:郑伊健 11 | [00:18.00] 钢琴:Johnny Yim 12 | [00:21.00] 歌词编辑:果果 13 | [00:24.00] QQ:765708831 14 | [00:27.00] 中文歌词库 www.cnLyric.com 15 | [00:44.07] 消失的光阴散在风里 16 | [00:47.94] 仿佛想不起再面对 17 | [00:51.83] 流浪日子 18 | [00:53.77] 你在伴随 19 | [00:55.58] 有缘再聚 20 | [00:59.50] 天真的声音已在减退 21 | [01:03.29] 彼此为着目标相距 22 | [01:06.98] 凝望夜空 23 | [01:08.90] 往日是谁 24 | [01:10.63] 领会心中疲累 25 | [01:16.40] 来忘掉错对 26 | [01:18.07] 来怀念过去 27 | [01:19.89] 曾共度患难日子总有乐趣 28 | [01:23.93] 不相信会绝望 29 | [01:25.67] 不感觉到踌躇 30 | [01:27.62] 在美梦里竞争 31 | [01:29.71] 每日拼命进取 32 | [01:31.58] 奔波的风雨里 33 | [01:33.31] 不羁的醒与醉 34 | [01:35.20] 所有故事像已发生 35 | [01:37.52] 飘泊岁月里 36 | [01:39.14] 风吹过已静下 37 | [01:40.99] 将心意再还谁 38 | [01:42.91] 让眼泪已带走夜憔悴 39 | [01:58.73] 天真的声音已在减退 40 | [02:02.35] 彼此为着目标相距 41 | [02:06.05] 凝望夜空 42 | [02:07.92] 往日是谁 43 | [02:09.58] 领会心中疲累 44 | [02:15.43] 来忘掉错对 45 | [02:16.81] 来怀念过去 46 | [02:19.09] 曾共度患难日子总有乐趣 47 | [02:22.84] 不相信会绝望 48 | [02:24.88] 不感觉到踌躇 49 | [02:26.87] 在美梦里竞争 50 | [02:28.66] 每日拼命进取 51 | [02:30.60] 奔波的风雨里 52 | [02:32.37] 不羁的醒与醉 53 | [02:34.29] 所有故事像已发生 54 | [02:36.66] 飘泊岁月里 55 | [02:38.11] 风吹过已静下 56 | [02:40.03] 将心意再还谁 57 | [02:41.92] 让眼泪已带走夜憔悴 58 | [03:05.23] 来忘掉错对 59 | [03:08.71] 来怀念过去 60 | [03:12.35] 曾共度患难日子总有乐趣 61 | [03:18.94] 不相信会绝望 62 | [03:22.88] 不相信会绝望 63 | [03:24.79] 不感觉到踌躇 64 | [03:26.75] 在美梦里竞争 65 | [03:28.50] 每日拼命进取 66 | [03:30.44] 奔波的风雨里 67 | [03:32.19] 不羁的醒与醉 68 | [03:34.18] 所有故事像已发生 69 | [03:36.42] 飘泊岁月里 70 | [03:37.95] 风吹过已静下 71 | [03:39.74] 将心意再还谁 72 | [03:41.76] 让眼泪已带走夜憔悴 -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/TestLRC2: -------------------------------------------------------------------------------- 1 | [ti:依赖] 2 | [ar:蔡健雅] 3 | [al:MY SPACE] 4 | [by:Chapter Chang] 5 | [offset:0] 6 | [00:00.50]蔡健雅 - 依赖 7 | [00:07.94]词、曲:蔡健雅、陶晶莹 8 | [00:11.60]关了灯把房间整理好 9 | [00:15.48]凌晨三点还是睡不著 10 | [00:19.64]你应该是不在 所以把电话挂掉 11 | [00:30.39]在黑暗手表跟着心跳 12 | [00:34.57]怎么慢它停也停不了 13 | [00:39.70]我应该只是心情不好 14 | [00:45.00]那又怎样 15 | [00:48.50]但本来是这样 16 | [01:21.36]朋友们对我的安慰 17 | [01:25.20]都是同样的一个话题 18 | [01:29.73]我一定要变得更坚强 19 | [01:34.68]说很简单 20 | [00:38.50]但是做却很难 21 | [00:53.00][01:43.88][02:11.23]虽然无所谓写在脸上 22 | [00:58.21][01:48.44][02:15.79]我还是舍不得让你离开 23 | [01:02.97][01:53.50][02:20.60]虽然闭着眼假装听不到 24 | [01:07.84][01:58.23][02:25.11][02:33.15]你对爱 已不再 想依赖 -------------------------------------------------------------------------------- /LRCParserDemo/LRCParserDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // LRCParserDemo 4 | // 5 | // Created by Normal on 16/2/25. 6 | // Copyright © 2016年 wwwbbat. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DDLRCParser 2 | Music LRC Text parser 3 | 4 | 可以识别标准的音乐LRC歌词格式 5 | 提供便利的方法,可以根据时间范围取歌词。 6 | 支持LRC中的标签:ti ar al by offset t_time 7 | 支持时间标签: mm:ss:ms 和 mm:ss.ms --------------------------------------------------------------------------------