1. 程式人生 > >iOS解析lrc歌詞文件

iOS解析lrc歌詞文件

原歌詞文件:

[ar:Coldplay] 
[t_time:(04:04)] 
[00:00.00] Viva la Vida
[00:03.50] Coldplay
[00:13.29] I used to rule the world
[00:16.64] Seas would rise when I gave the word
[00:19.65] Now in the morning I sweep alone
[00:23.45] Sweep the streets I used to own
[00:41.05] I used to roll the dice
[00:44.33] Feel the fear in my enemys eyes
[00:47.72] Listen as the crowd would sing:
[00:51.03] Now the old king is dead! Long live the king!
[00:54.91] it is edited by rain
[00:55.10] One minute I held the key
[00:58.22] Next the walls were closed on me
[01:01.46] And I discovered that my castles stand
[01:04.96] Upon pillars of salt and pillars of sand
[01:09.06] I hear Jerusalem bells a ringing
[01:11.75] Roman Cavalry choirs are singing
[01:16.16] Be my mirror my sword and shield
[01:19.65] My missionaries in a foreign field
[01:22.90] For some reason I cant explain
[01:26.51] Once you go there was never, never an honest word
[01:32.67] That was when I ruled the world
[01:43.05] (Ohhh)
[01:50.25] It was the wicked and wild wind
[01:53.71] Blew down the doors to let me in
[01:56.76] Shattered windows and the sound of drums
[02:00.56] People couldnt believe what Id become
[02:04.46] Revolutionaries wait
[02:07.56] For my head on a silver plate
[02:10.96] Just a puppet on a lonely string
[02:14.74] Oh who would ever want to be king?
[02:18.07] I hear Jerusalem bells a ringing
[02:22.25] Roman Cavalry choirs are singing
[02:25.67] Be my mirror my sword and shield
[02:29.27] My missionaries in a foreign field
[02:32.61] For some reason I cant explain
[02:36.06] I know Saint Peter will call my name
[02:39.48] Never an honest word
[02:42.52] But that was when I ruled the world
[03:00.07] (Ohhhhh Ohhh Ohhh)
[03:15.22] I hear Jerusalem bells a ringing
[03:17.94] Roman Cavalry choirs are singing
[03:21.31] Be my mirror my sword and shield
[03:24.90] My missionaries in a foreign field
[03:28.22] For some reason I cant explain
[03:31.69] I know Saint Peter will call my name
[03:35.08] Never an honest word
[03:38.06] But that was when I ruled the world

解析後,歌詞如下
    " Viva la Vida",
    " Coldplay",
    " I used to rule the world",
    " Seas would rise when I gave the word",
    " Now in the morning I sweep alone",
    " Sweep the streets I used to own",
    " I used to roll the dice",
    " Feel the fear in my enemys eyes",
    " Listen as the crowd would sing:",
    " Now the old king is dead! Long live the king!",
    " it is edited by rain",
    " One minute I held the key",
    " Next the walls were closed on me",
    " And I discovered that my castles stand",
    " Upon pillars of salt and pillars of sand",
    " I hear Jerusalem bells a ringing",
    " Roman Cavalry choirs are singing",
    " Be my mirror my sword and shield",
    " My missionaries in a foreign field",
    " For some reason I cant explain",
    " Once you go there was never, never an honest word",
    " That was when I ruled the world",
    " (Ohhh)",
    " It was the wicked and wild wind",
    " Blew down the doors to let me in",
    " Shattered windows and the sound of drums",
    " People couldnt believe what Id become",
    " Revolutionaries wait",
    " For my head on a silver plate",
    " Just a puppet on a lonely string",
    " Oh who would ever want to be king?",
    " I hear Jerusalem bells a ringing",
    " Roman Cavalry choirs are singing",
    " Be my mirror my sword and shield",
    " My missionaries in a foreign field",
    " For some reason I cant explain",
    " I know Saint Peter will call my name",
    " Never an honest word",
    " But that was when I ruled the world",
    " (Ohhhhh Ohhh Ohhh)",
    " I hear Jerusalem bells a ringing",
    " Roman Cavalry choirs are singing",
    " Be my mirror my sword and shield",
    " My missionaries in a foreign field",
    " For some reason I cant explain",
    " I know Saint Peter will call my name",
    " Never an honest word",
    " But that was when I ruled the world"

歌詞時間如下:
"00:00.00",
    "00:03.50",
    "00:13.29",
    "00:16.64",
    "00:19.65",
    "00:23.45",
    "00:41.05",
    "00:44.33",
    "00:47.72",
    "00:51.03",
    "00:54.91",
    "00:55.10",
    "00:58.22",
    "01:01.46",
    "01:04.96",
    "01:09.06",
    "01:11.75",
    "01:16.16",
    "01:19.65",
    "01:22.90",
    "01:26.51",
    "01:32.67",
    "01:43.05",
    "01:50.25",
    "01:53.71",
    "01:56.76",
    "02:00.56",
    "02:04.46",
    "02:07.56",
    "02:10.96",
    "02:14.74",
    "02:18.07",
    "02:22.25",
    "02:25.67",
    "02:29.27",
    "02:32.61",
    "02:36.06",
    "02:39.48",
    "02:42.52",
    "03:00.07",
    "03:15.22",
    "03:17.94",
    "03:21.31",
    "03:24.90",
    "03:28.22",
    "03:31.69",
    "03:35.08",
    "03:38.06"

每句歌詞持續時間如下
    "0.000",
    "0.000",
    "0.000",
    "0.000",
    "3.050",
    "13.029",
    "16.064",
    "19.065",
    "23.045",
    "41.005",
    "44.033",
    "47.072",
    "51.003",
    "54.091",
    "55.010",
    "58.022",
    "61.046",
    "64.096",
    "69.006",
    "71.075",
    "76.016",
    "79.065",
    "82.090",
    "86.051",
    "92.067",
    "103.005",
    "110.025",
    "113.071",
    "116.076",
    "120.056",
    "124.046",
    "127.056",
    "130.096",
    "134.074",
    "138.007",
    "142.025",
    "145.067",
    "149.027",
    "152.061",
    "156.006",
    "159.048",
    "162.052",
    "180.007",
    "195.022",
    "197.094",
    "201.031",
    "204.090",
    "208.022",
    "211.069",
    "215.008",
    "218.006"

解析方式如下,主要為字串的擷取操作
    NSString *lynicPath = [[NSBundle mainBundle] pathForResource:@"vivalavida" ofType:@"lrc"];
    NSString *lyc = [NSString stringWithContentsOfFile:lynicPath encoding:NSUTF8StringEncoding error:nil];
    
    NSMutableArray *lyricArray = [NSMutableArray array];
    NSMutableArray *timeArray = [NSMutableArray array];
    NSMutableArray *lengthArray = [NSMutableArray array];
    
    NSArray *lycArray = [lyc componentsSeparatedByString:@"\n"];
    [lycArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSRange starRange = [obj rangeOfString:@"["];
        NSRange stopRange = [obj rangeOfString:@"]"];
        NSString *timeString = [obj substringWithRange:NSMakeRange(starRange.location + 1, stopRange.location - starRange.location - 1)];
        [timeArray addObject:timeString];   // 新增進時間陣列
        
        NSString *minString = [timeString substringWithRange:NSMakeRange(0, 2)];
        NSString *secString = [timeString substringWithRange:NSMakeRange(3, 2)];
        NSString *mseString = [timeString substringWithRange:NSMakeRange(6, 2)];
        
        float timeLength = [minString floatValue] * 60 + [secString floatValue] + [mseString floatValue] / 1000;
//        _middleTime = timeLength;
//        timeLength = timeLength - _frontTime;
//        _frontTime = _middleTime;
        [lengthArray addObject:[NSString stringWithFormat:@"%.3f",timeLength]];
        
        NSString *lyricString = [obj substringFromIndex:10];
        [lyricArray addObject:lyricString];
        
    }];