IOS parsing lyrics lrc
Recently, I learned some things in the Process of playing a music player and wrote it down to share it. First of all, I want to analyze the lyrics. First, let's look at lrc. (If you don't stick a wiki, you may not be able to open the lyrics. Generally, this is the format. [minute: second. millisecond] lyrics 2. [minute: Second] lyrics 3. [minute: Second: millisecond] 1 of the lyrics is in the standard format. I will use the following example. The idea is to first obtain the entire lyric content, and then segment it by line feed. The content in each line is divided into two parts: time and content, extracted separately. -(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]) {nsange startrange = [item rangeOfString: @ "["]; NSLog (@ "% d", startrange. length, startrange. location); nsange 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;