IOS 解析歌詞lrc

來源:互聯網
上載者:User

IOS 解析歌詞lrc
最近在搗鼓音樂播放器,過程中學到了一些東西,寫下來分享一下,首先是歌詞的解析 首先我們看看lrc(不貼維基了怕打不開 歌詞檔案一般是這樣的格式 1.[分鐘:秒.毫秒] 歌詞 2. [分鐘:秒] 歌詞 3. [分鐘:秒:毫秒] 歌詞 其中1是標準格式,下面我就一種為例。 思路是先擷取整個歌詞內容,再按換行分段,對每一行中的內容,分為兩部分,時間和內容,分別提取。      -(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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.