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

一. 播放界面VC.h

#import <UIKit/UIKit.h>

@interface ThirdViewController : 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

//  DFR2016Demo

//

#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

//  DFR2016Demo

//

#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] : @"");

}

}

}

}

}

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

音乐播放器的滚动歌词的实现相关推荐

  1. 全网首个tk网络音乐播放器!支持歌词滚动!你看我吊不?

    这是一个用 Python 的 tkinter 库做的一个网络音乐播放器.我不说它的 UI 设计的有多好看,但是它的功能绝对是全站首个!坚持看到底,你不点赞算我输! 成果展示 程序截图 前期准备 程序结 ...

  2. android版音乐播放器----卡拉OK歌词实现(二)

    相信大家都看过酷狗的音乐播放器,特别是酷狗的滚动歌词实现起来,效果特别好.我自己也尝试看了一下酷狗的歌词文件,发现是加密的,根本就看不了,所以只能是从目前开源的歌词文件出发了.我自己选择的是ksc歌词 ...

  3. Qt音乐播放器实现(带歌词功能)

    最近期末项目要写一个音乐播放器,作为新人头疼了好久,参考了各方文档,总算憋出来了一个,歌词功能用的是一个大佬实现的,然后我就调用了,就是我那个lyricwidget.cpp和头文件就是直接弄过来的,但 ...

  4. Android音乐播放器源码(歌词.均衡器.收藏.qq5.0菜单.通知)

    一款Android音乐播放器源码,基本功能都实现了 qq5.0菜单(歌词.均衡器.收藏.qq5.0菜单.通知) 只有向右滑动出现,菜单键和指定按钮都还没有添加. 源码下载:http://code.66 ...

  5. 音乐播放器(1)--在线歌词获取

    电脑上存放了特别的歌曲,大概有2400首左右,每次通过播放器选择相应歌曲实在麻烦,于是打算自己使用Java写一个终端下的音乐播放器,我使用的ubuntu,因为java是跨平台语言,所以什么系统已经无所 ...

  6. 仿网易云网页版音乐播放器,实现歌词随歌曲进行滚动高亮

    引言 前几天在使用网易云网页版听歌时,看着那个页面的歌词随歌曲进行高亮,突然也想自己手动地去实现一下,于是呢,就仿照了网易云音乐的网页自己也写了个页面.效果图如下: 当然了,此处不做css的样式介绍, ...

  7. android版音乐播放器----卡拉OK歌词实现(一)

    我们都看过酷狗的滚动歌词,当然,我们要实现他的话,第一步,就是弄出类似的歌词效果. 大家可以参考一下,下面我提供的链接的视频,里面提到了,歌词实现的大概原理. http://www.duobei.co ...

  8. Android开发本地及网络Mp3音乐播放器(十六)歌词显示及滚动事件实现、ViewPager使用

    转载请注明出处: http://blog.csdn.net/iwanghang/article/details/51386317 觉得博文有用,请点赞,请留言,请关注,谢谢!~ 实现功能: 歌词显示及 ...

  9. JS制作音乐播放器并高亮歌词滚动以及按钮暂停播放切歌

    简介:高亮歌词这里需要说一个文件,也就是.lrc文件,这个功能制作是将文件中时间与音乐文件中播放的进度作比较,实现文件何时滚动换行以及高亮的,好了看代码(我的歌曲文件是周总的<等你下课>) ...

最新文章

  1. 跟我学Springboot开发后端管理系统4:数据库连接池Druid和HikariCP
  2. steps/align_si.sh
  3. IIS上.Net 扩展中进行恢复
  4. access集团和abm_ACCESS集团宣布启动“ABM单创66会员狂欢节”
  5. IOS-C语言第8天,Struct (结构体)
  6. mongodump 备份还原
  7. [Day30] DBUtils和连接池
  8. 音视频开发(19)---Android视频开发基础(一)
  9. 混日子不是你的错,根源在这里
  10. 【DP + 卖股票】LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee
  11. 面向对象(Python):学习笔记之单例模式
  12. Hadoop安装教程_单机/伪分布式配置
  13. Win10系统电脑开机后显示无法登录到你的账户解决办法(亲测)
  14. Web 3.0让网络巨头们恐慌?Dapp爆发潮的到来会更让人颤抖!
  15. CodeForces #379(734A|734B|734C|734D|734E|734F)|二分查找|模拟|树的半径|位运算
  16. CAD命令栏窗口跑到屏幕外面怎么找回来
  17. 使用C#快速创建KML文件或读取其功能
  18. Unity学习 — 23种设计模式
  19. 如何完全卸载HbuilderX
  20. Java SE 027 String类源代码深析

热门文章

  1. Android-Accessibility(辅助功能/无障碍,自动安装APP)
  2. vxlan专题---第四章配置分布式网关部署方式的华为VXLAN示例-外部路由type5
  3. Vue 点击文字改变文字颜色
  4. CSDN网站中的版权投诉的标准
  5. python京东预约抢购_京东抢购脚本js教程
  6. 本科计算机专业sci好写吗,保研经验:本科发表4篇SCI,横扫北大、清华、中科大、中科院、浙大17个OFFER!...
  7. shell命令:打印除第一列外所有列
  8. 使用GoogleCode管理你的代码
  9. GoogleCode新手教程
  10. 面试题-redis数据类型