600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 音乐播放器的滚动歌词的实现

音乐播放器的滚动歌词的实现

时间:2022-05-08 08:15:19

相关推荐

音乐播放器的滚动歌词的实现

先说下思路:歌词是tableView的实现,用个定时器每隔0.5秒就自动滚动一次,比较当前的时间和播放器的时间的大小来判断是否滚动到下一行。废话不多说,直接上代码。

一. 播放界面VC.h

#import<UIKit/UIKit.h>

@interfaceThirdViewController :UIViewController

@end

二:播放界面的VC.m

#import "ThirdViewController.h"

#import "LrcParser.h"

#import <AVFoundation/AVFoundation.h>

@interface ThirdViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) AVAudioPlayer *player;

@property (strong, nonatomic) IBOutlet UITableView *lyrTableView;

@property (nonatomic, strong) LrcParser *lrcContent; //model,存储的是歌词的时间和歌词

@property (nonatomic, assign) NSInteger currentRow;

@end

@implementation ThirdViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.navigationController.edgesForExtendedLayout = UIRectEdgeNone;

self.lrcContent = [[LrcParser alloc] init ];

[self.lrcContent parseLrc];

[self initPlayer];

// UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wall1.jpg"]];

// imageView = [self getBlurEffectImageView:imageView];

// self.lyrTableView.backgroundView = imageView;

UIImage *img = [UIImage imageNamed:@"wall1.jpg"];

UIImageView *bgView = [[UIImageView alloc] initWithImage:img];

self.lyrTableView.backgroundView = bgView;

[bgView setImage:[self getBlurredImage:img]];

[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];

}

- (void)initPlayer{

AVAudioSession *session = [AVAudioSession sharedInstance];

[session setActive:YES error:nil];

[session setCategory:AVAudioSessionCategoryPlayback error:nil];

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"冰雨" withExtension:@"mp3"] error:nil];

self.player.numberOfLoops = 10;

[self.player prepareToPlay];

[self.player play];

}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.lrcContent.wordArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [self.lyrTableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

cell.textLabel.text = self.lrcContent.wordArray[indexPath.row];

if (indexPath.row == _currentRow) {

cell.textLabel.textColor = [UIColor greenColor];

}else{

cell.textLabel.textColor = [UIColor lightGrayColor];

}

return cell;

}

#pragma mark - UITableViewDelegate

- (void)updateTime{

CGFloat currentTime = self.player.currentTime;

NSLog(@"%d:%d", (int)currentTime / 60, (int)currentTime % 60);

for (int i = 0; i < self.lrcContent.timerArray.count; i ++) {

NSArray *timeArray = [self.lrcContent.timerArray[i] componentsSeparatedByString:@":"];

float lrcTime = [timeArray[0] intValue] * 60 + [timeArray[1] intValue];

if (currentTime > lrcTime) {

_currentRow = i;

}else{

break;

}

[self.lyrTableView reloadData];

[self.lyrTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_currentRow inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

}

}

/**

* 实现图片blurEffect的模糊效果(模糊效果明显)

*/

//- (UIImageView *)getBlurEffectImageView:(UIImageView *)imageView{

// UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

// UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];

// effectView.frame = imageView.frame;

// [imageView addSubview:effectView];

// return imageView;

//}

/**

* 实现图片高斯模糊

*/

- (UIImage *)getBlurredImage:(UIImage *)image{

CIContext *content = [CIContext contextWithOptions:nil];

CIImage *ciImage = [CIImage imageWithCGImage:image.CGImage];

CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];

[filter setValue:ciImage forKey:kCIInputImageKey];

[filter setValue:@5.0f forKey:@"inputRadius"];

CIImage *result = [filter valueForKey:kCIOutputImageKey];

CGImageRef ref = [content createCGImage:result fromRect:[result extent]];

return [UIImage imageWithCGImage:ref];

}

三。LrcParser类是model,主要是存储的是歌词和时间

//

// LrcParser.h

// DFRDemo

//

#import <Foundation/Foundation.h>

@interface LrcParser : NSObject

/**

* 时间

*/

@property (nonatomic, strong) NSMutableArray *timerArray;

/**

* 歌词

*/

@property (nonatomic, strong) NSMutableArray *wordArray;

/**

* 解析歌词

*/

- (void)parseLrc;

/**

* 解析歌词

*

* @param lrc

*/

- (void)parseLrc:(NSString *)lrc;

@end

//

// LrcParser.m

// DFRDemo

//

#import "LrcParser.h"

@implementation LrcParser

-(instancetype)init{

if (self = [super init]) {

self.timerArray = [[NSMutableArray alloc] initWithCapacity:0];

self.wordArray = [[NSMutableArray alloc] initWithCapacity:0];

}

return self;

}

- (NSString *)getLrcFile:(NSString *)lrc{

NSString *filePath = [[NSBundle mainBundle] pathForResource:lrc ofType:@"lrc"];

return [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

}

/**

* 解析歌词

*/

- (void)parseLrc{

[self parseLrc:[self getLrcFile:@"冰雨"]];

}

/**

* 解析歌词

*

* @param lrc

*/

- (void)parseLrc:(NSString *)lrc{

// NSLog(@"%@", lrc);

if (![lrc isEqual:nil]) {

NSArray *tempArray = [lrc componentsSeparatedByString:@"["];

NSArray *lineArray = [[NSArray alloc] init];

for (int i = 0; i < tempArray.count; i ++) {

if ([tempArray[i] length] > 0) {

lineArray = [tempArray[i] componentsSeparatedByString:@"]"];

if (![lineArray[0] isEqualToString:@"\n"]) {

[self.timerArray addObject:lineArray[0]];

[self.wordArray addObject:lineArray.count > 1 ? lineArray[1] : @""];

//NSLog(@"self.timerArray----%@", lineArray[0]);

//NSLog(@"self.wordArray----%@",lineArray.count > 1 ? lineArray[1] : @"");

}

}

}

}

}

解析出来的数据是这个样子的:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。