思路是先获取整个歌词内容,再按换行分段,对每一行中的内容,分为两部分,时间和内容,分别提取。
-(void)parselyric
{
NSString *path = [[NSBundle mainBundle]pathForResource:_lab_title.text ofType:@"lrc"];
//if lyric file exits
if ([path length]) {
//get the lyric string
NSString *lyc = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//init
_musictime = [[NSMutableArray alloc]init];
_lyrics = [[NSMutableArray alloc]init];
_t = [[NSMutableArray alloc]init];
NSArray *arr = [lyc componentsSeparatedByString:@"\n"];
for (NSString *item in arr) {
//if item is not empty
if ([item length]) {
NSRange startrange = [item rangeOfString:@"["];
NSLog(@"%d%d",startrange.length,startrange.location);
NSRange stoprange = [item rangeOfString:@"]"];
NSString *content = [item substringWithRange:NSMakeRange(startrange.location+1, stoprange.location-startrange.location-1)];
NSLog(@"%d",[item length]);
//the music time format is mm.ss.xx such as 00:03.84
if ([content length] == 8) {
NSString *minute = [content substringWithRange:NSMakeRange(0, 2)];
NSString *second = [content substringWithRange:NSMakeRange(3, 2)];
NSString *mm = [content substringWithRange:NSMakeRange(6, 2)];
NSString *time = [NSString stringWithFormat:@"%@:%@.%@",minute,second,mm];
NSNumber *total =[NSNumber numberWithInteger:[minute integerValue] * 60 + [second integerValue]];
[_t addObject:total];
NSString *lyric = [item substringFromIndex:10];
[_musictime addObject:time];
[_lyrics addObject:lyric];
}
}
}
}
else
_lyrics = nil;
http://download.csdn.net/detail/kgart/6337687 提供了代码的下载。。你可以去查看 看
http://www.cocoachina.com/bbs/read.php?tid=84468 给你提供了两个网站进行下载看看代码试试。。 |