我觉得要想解析lrc 首先大家应该了解一下lrc文件的结构,大家可以去看一下**百科 我这里粗略的写一下;
■ 时间标签(Time-tag)
形式为"[mm:ss]"(分钟数:秒数)
或"[mm:ss.ff]"。数字须为非负整数
■ 标识标签(ID-tags)
其格式为"[标识名:值]"。大小写等价。以下是预定义的标签。
[ar:艺人名]
[ti:曲名]
[al:专辑名]
[by:编者(指编辑LRC歌词的人)]
[offset:时间补偿值] 其单位是毫秒,正值表示整体提前,负值相反。这是用于总体调整显示快慢的。
[t_time:(总时长)]
每一句歌词可能有多个播放时间,如:
[00:12:34][00:34:15][00:25:54]测试
所以解析的时候都要考虑到;
下面附源码,源码中有关键点的注释;源码下面还会有解释
注:代码中很多部分采用了三元式,希望大家可以看懂,三元式可以和if else 语句替换,大家自己可以网上搜一下,后面知识点会稍微说一下,不会很详细
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // 2 // LyricsAnalysis.h 3 // 08-10-MusicPlayer 4 // 5 // Created by Ibokan on 15/8/21. 6 // Copyright (c) 2015年 Crazy凡. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface LyricsAnalysis : NSObject 12 @property (nonatomic,strong)NSString *ar;//演唱 13 @property (nonatomic,strong)NSString *ti;//歌曲名 14 @property (nonatomic,strong)NSString *al;//专辑名 15 @property (nonatomic,strong)NSString *by;//歌词作者 16 @property (nonatomic,strong)NSString *t_time;//歌曲总时长 17 @property (nonatomic,strong)NSMutableArray *lrcArrayTime;//时间数组 18 @property (nonatomic,strong)NSMutableArray *lrcArrayStr;//歌词数组 19 20 - (instancetype)init; 21 - (instancetype)initWithFileName:(NSString *)name ofType:(NSString *)type; 22 - (instancetype)initWithFilePath:(NSString *)filePath; 23 - (void)lyricsAnalysisWithFileName:(NSString *)name ofType:(NSString *)type; 24 - (void)lyricsAnalysisWithFilePath:(NSString *)filePath; 25 @end
以上是.h头文件源码
以下是.m源文件源码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // 2 // LyricsAnalysis.m 3 // 08-10-MusicPlayer 4 // 5 // Created by Ibokan on 15/8/21. 6 // Copyright (c) 2015年 Crazy凡. All rights reserved. 7 // 8 9 #import "LyricsAnalysis.h" 10 11 12 @interface LyricsAnalysis () 13 @property double offset;//歌词时间调整变量 14 @end 15 @implementation LyricsAnalysis 16 17 //初始化 18 - (instancetype)init 19 { 20 self = [super init]; 21 if (self) { 22 self.ar = [[NSString alloc]init]; 23 self.ti = [[NSString alloc]init]; 24 self.by = [[NSString alloc]init]; 25 self.al = [[NSString alloc]init]; 26 self.offset = 0; 27 self.t_time = [[NSString alloc]init];//歌曲总时长单位(s) 28 self.lrcArrayStr = [[NSMutableArray alloc]init];//歌词数组初始化 29 self.lrcArrayTime = [[NSMutableArray alloc]init];//时间数组初始化 30 } 31 return self; 32 } 33 //带文件名的初始化 34 - (instancetype)initWithFileName:(NSString *)name ofType:(NSString *)type 35 { 36 self = [self init]; 37 [self lyricsAnalysisWithFileName:name ofType:type]; 38 return self; 39 } 40 //带文件路径的初始化 41 - (instancetype)initWithFilePath:(NSString *)filePath 42 { 43 self = [self init]; 44 [self lyricsAnalysisWithFilePath:filePath]; 45 return self; 46 } 47 //处理文件名歌词 48 - (void)lyricsAnalysisWithFileName:(NSString *)name ofType:(NSString *)type 49 { 50 [self lyricsAnalysisWithFilePath:[[NSBundle mainBundle] pathForResource:name ofType:type]];//构建filepath 51 } 52 //处理文件路径 53 - (void)lyricsAnalysisWithFilePath:(NSString *)filePath 54 { 55 NSString *strlrc = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 56 NSMutableArray * arraylrc = [[NSMutableArray alloc] initWithArray:[strlrc componentsSeparatedByString:@" "]]; 57 NSArray *StrToInt = [NSArray arrayWithObjects:@"ar",@"ti",@"al",@"by",@"of",@"t_",nil];//NSString switch 配置 58 BOOL flag = YES; 59 while(flag) 60 { 61 NSString *temp = arraylrc[0]; 62 switch ((int)[StrToInt indexOfObject:[temp substringWithRange:NSMakeRange(1, 2)]]) 63 { 64 case 0:self.ar = [[temp substringFromIndex:4]stringByReplacingOccurrencesOfString:@"]" withString:@"