歌词加载学习笔记

来源:互联网 发布:人工智能 百度网盘 编辑:程序博客网 时间:2024/04/29 17:17
ios收获:ios中break语句的作用是跳过本层的for循环,而continue只是停止执行这一次循环,从而跳出这一次循环。就是继续执行之后的循环罢了。

 

1、首先从本地解析歌词文件。Lrc歌词文件时间来确定定时播放的数据。

 

 

通过

NSString *LRCPath = [[NSBundle mainBundle] pathForResource:[musicArray[musicArrayNumber] name] ofType:@"lrc"];
    NSString *contentStr = [NSString stringWithContentsOfFile:LRCPath encoding:NSUTF8StringEncoding error:nil];
    NSArray *array = [contentStr componentsSeparatedByString:@"\n"];

来解析数据,首先把所有的歌词文件加载到一个string中,然后通过通过相关的解码,来获得真实的数据,

array使用"/n"换行。从而得到一行数据,将所有的数据加载到一个数组中。

通过for循环遍历所有的数据,从而获得每一个数据来做相关的处理,根据歌词中的"]"字符串来分割相关的数据。

从而将时间和数据分开。然后再将时间根据":"和"."来分割开来,载入先关的数据。time和lrcstr。。。

 

 

- (NSString *)substringWithRange:(NSRange)aRange
可以从不规则的字符串中取出自己需要的字符,并且可以通过组装来实现例如时间的解集

 通过这个格式来截取不规则格式的音频的时间。

通过定时器来定时加载歌词,其中通过歌词的长度来判读需要执行的歌词跳转,

//动态显示歌词-(void)displayLrc:(NSUInteger )timer{    for (int i =0 ; i<[timeArray count]; i++) {        NSArray * array = [timeArray[i] componentsSeparatedByString:@":"];        NSUInteger currentTime = [array[0] intValue]*60 +[array[1] intValue];             if(i == ([timeArray count]-1))        {            NSArray *array1 = [timeArray[timeArray.count-1] componentsSeparatedByString:@":"];            NSUInteger currentTime1 = [array1[0] intValue]*60 +[array1[1] intValue];            if (timer>currentTime1) {            [self updataLRC:i];                            }            break;#warning  这里有一个问题                   }else        {            //求出第一句的时间点,在第一句显示前的时间内一直加载第一句            NSArray *array2 = [timeArray[0] componentsSeparatedByString:@":"];            NSUInteger currentTime2 = [array2[0] intValue] * 60 + [array2[1] intValue];            if (timer < currentTime2) {                [self updataLRC:0];                //                NSLog(@"马上到第一句");               break;            }        NSArray *array3 = [timeArray[i+1] componentsSeparatedByString:@":"];         NSUInteger currentTime3 = [array3[0] intValue]*60 +[array3[1] intValue];        if (timer >=currentTime && timer<=currentTime3) {            [self updataLRC:i];     break;        }    }    }}
//更新选中的代码。
-(void)updataLRC:(NSUInteger)lineNumber{ lrcLineNum = lineNumber; [centerTableView reloadData]; NSIndexPath *indexPath= [NSIndexPath indexPathForRow:lineNumber inSection:0 ]; NSLog(@"indexPath %@",indexPath); [centerTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; }

在tableview的datasorce方法中加载判断需要跳转的界面

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{        if (tags == 0) {                return [timeArray count];    }    else if (tags ==1)    {        return [timeArray count];    }        return [timeArray count];}

tags是绑定的全部变量,来判断点击事件触发的时候加载的是哪一个segment。

从而来返回相关的tableview cell的个数。

在tableview的datasorce方法 来实现动态加载歌词。

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 2 { 3     static NSString* cellIndentify = @"LRCCell"; 4     UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIndentify]; 5     if (cell ==nil) { 6         cell  = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentify]; 7     } 8     if (indexPath.row == lrcLineNum) { 9 //        cell.textLabel.text = lrcDict[timeArray[indexPath]];10         cell.textLabel.text = lrcDict[timeArray[indexPath.row]];11         cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];12         cell.textLabel.font = [UIFont systemFontOfSize:15];13     }else14     {15         cell.textLabel.text = lrcDict[timeArray[indexPath.row]];16         cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];17         cell.textLabel.font = [UIFont systemFontOfSize:13];18     }19     cell.backgroundColor = [UIColor clearColor];20     return cell;21 }

第一次写这么长的代码。好高兴。哈哈

0 0