iOS音乐播放器(歌词自动滚动)

简单实现基于IOS的音乐播放器,并且带有歌词,随播放自动滚动,实现效果如下:

首先,需要建立一个解析歌词的类ZMPlrc,解析歌词主要就是把时间和对应的歌词分离出来,然后存储到数组中。

ZMPlrc.h

#import <Foundation/Foundation.h>@interface ZMPlrc : NSObject/**时间*/
@property (nonatomic,strong)NSMutableArray *timeArray;
/**歌词*/
@property (nonatomic,strong)NSMutableArray *wordArray;/**解析歌词*/
- (void)parselrc;@end

ZMPlrc.m

#import "ZMPlrc.h"@implementation ZMPlrc- (instancetype)init
{self = [super init];if (self) {_timeArray = [NSMutableArray array];_wordArray = [NSMutableArray array];}return self;
}/**歌词路径*/
- (NSString *)getLrcPath{return [[NSBundle mainBundle] pathForResource:@"梁静茹-偶阵雨" ofType:@"lrc"];
}/**解析歌词*/
- (void)parselrc{NSString *content = [NSString stringWithContentsOfFile:[self getLrcPath] encoding:NSUTF8StringEncoding error:nil];NSArray *sepArray = [content componentsSeparatedByString:@"["];for (int i = 5; i < sepArray.count; i ++) {//有两个元素,一个是时间,一个是歌词NSArray *arr = [sepArray[i] componentsSeparatedByString:@"]"];//NSLog(@"%@",sepArray[i]);[_timeArray addObject:arr[0]];[_wordArray addObject:arr[1]];}//NSLog(@"%@",content);
}@end

接着,在storyboard中添加必要的控件。

最后,就可以在控制器中实现了ZMPViewController,在实现过程中,主要就是监听音频播放player的播放时间,跟之前解析好的每句歌词对应的时间进行处理。

ZMPViewController.h

#import <UIKit/UIKit.h>@interface ZMPViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITableView *lrcTableView;
@property (weak, nonatomic) IBOutlet UISlider *timeSlider;
@property (weak, nonatomic) IBOutlet UILabel *currentTimeLabel;@property (weak, nonatomic) IBOutlet UILabel *totalTimeLabel;@property (weak, nonatomic) IBOutlet UIButton *lastMusicBtnClick;
- (IBAction)playBtnClick:(UIButton *)sender;
- (IBAction)preMusicBtnClick:(id)sender;
- (IBAction)valueChange:(UISlider *)sender;- (IBAction)nextMusicBtnClick:(id)sender;
@end

ZMPViewController.m

#import "ZMPViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "ZMPlrc.h"@interface ZMPViewController ()<UITableViewDataSource,UITableViewDelegate>{//音乐播放器AVAudioPlayer *player;ZMPlrc *lrc;NSInteger currentRow;
}@end@implementation ZMPViewController- (void)viewDidLoad
{[super viewDidLoad];// Do any additional setup after loading the view, typically <span id="2_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=e21eb48420ee3a55&k=from&k0=from&kdi0=0&luki=4&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=553aee2084b41ee2&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D826622%2Ehtml&urlid=0" target="_blank" mpid="2" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">from</span></a></span> a nib.[self initPlayer];//侦听当前时间[NSTimer scheduledTimerWithTimeInterval:0.5 <span id="3_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=e21eb48420ee3a55&k=target&k0=target&kdi0=0&luki=8&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=553aee2084b41ee2&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D826622%2Ehtml&urlid=0" target="_blank" mpid="3" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">target</span></a></span>:self selector:@selector(updateTime) userInfo:nil repeats:YES];lrc = [[ZMPlrc alloc] init];[lrc parselrc];[self.lrcTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];[self.lrcTableView reloadData];}- (void)updateTime{CGFloat currentTime = player.currentTime;//self.currentTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d",(int)currentTime / 60, (int)currentTime % 60];self.timeSlider.value = currentTime / player.duration;for (int i = 0; i < lrc.timeArray.count; i ++) {NSArray *arr = [lrc.timeArray[i] componentsSeparatedByString:@":"];CGFloat compTime = [arr[0] integerValue]*60 + [arr[1] floatValue];if (player.currentTime > compTime){currentRow = i;}else{break;}}[self.lrcTableView reloadData];[self.lrcTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:currentRow inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}/**初始化音乐播放器*/
- (void)initPlayer{//后台播放音频设置AVAudioSession *session = [AVAudioSession sharedInstance];[session setActive:YES error:nil];[session setCategory:AVAudioSessionCategoryPlayback error:nil];//让<span id="4_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=e21eb48420ee3a55&k=app&k0=app&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=553aee2084b41ee2&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D826622%2Ehtml&urlid=0" target="_blank" mpid="4" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">app</span></a></span>支持接受远程控制事件[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];NSString *path = [[NSBundle mainBundle] pathForResource:@"梁静茹-偶阵雨" ofType:@"<span id="5_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="5_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=e21eb48420ee3a55&k=mp3&k0=mp3&kdi0=0&luki=3&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=553aee2084b41ee2&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D826622%2Ehtml&urlid=0" target="_blank" mpid="5" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">mp3</span></a></span>"];NSURL *url = [NSURL fileURLWithPath:path];player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];//声道 -1------左声道,1-----右声道player.pan = 0;//音量:0~1player.volume = 1;//单曲循环(负数表示单曲循环)player.numberOfLoops = -1;//速率(默认为1)//player.enableRate = YES;//player.rate = 1.0;//总时间CGFloat totalSeconds = player.duration;self.totalTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d",(int)totalSeconds / 60, (int)totalSeconds % 60];//当前时间player.currentTime;#if 0//播放[player play];//停止[player stop];//暂停[player pause];#endif}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}- (IBAction)playBtnClick:(UIButton *)sender {sender.selected = !sender.selected;if (sender.selected) {[player prepareToPlay];[player play];}else{[player pause];}
}- (IBAction)preMusicBtnClick:(id)sender {[self initPlayer];
}- (IBAction)valueChange:(UISlider *)sender {player.currentTime = player.duration * sender.value;
}- (IBAction)nextMusicBtnClick:(id)sender {[self initPlayer];
}#pragma <span id="6_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=e21eb48420ee3a55&k=mark&k0=mark&kdi0=0&luki=5&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=553aee2084b41ee2&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D826622%2Ehtml&urlid=0" target="_blank" mpid="6" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">mark</span></a></span> - TableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return lrc.wordArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];if (indexPath.row == currentRow){cell.textLabel.textColor = [UIColor <span id="7_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="7_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=e21eb48420ee3a55&k=red&k0=red&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=553aee2084b41ee2&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D826622%2Ehtml&urlid=0" target="_blank" mpid="7" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">red</span></a></span>Color];}else{cell.textLabel.textColor = [UIColor blackColor];}cell.textLabel.textAlignment = NSTextAlignmentCenter;cell.textLabel.font = [UIFont systemFontOfSize:15];cell.textLabel.text = lrc.wordArray[indexPath.row];return cell;
}@end

需要音乐在程序进入后台后同样能够播放,那么在plist文件中,增加Required background modes设置item 0的value为App plays audio or streams audio/video using AirPlay

并在控制器中,添加后台播放音频的设置

    //后台播放音频设置AVAudioSession *session = [AVAudioSession sharedInstance];[session setActive:YES error:nil];[session setCategory:AVAudioSessionCategoryPlayback error:nil];//让app支持接受远程控制事件[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

iOS音乐播放器(歌词自动滚动)相关推荐

  1. 原生js html音乐播放器(歌词滚动)

    本周学习了js的,用老师教的敲了一个音乐播放器 准备工作 首先随便找首歌 然后用一个小工具扒它的歌词 前期准备完成 代码部分 现在就开始写 html 和 css 了 很简单 就不解释了 直接上代码 h ...

  2. 基于IOS音乐播放器在线歌词同步小程序系统(音乐小程序)

    目 录 目 录 1 摘 要 3 Abstract 4 1 导论 6 1.1 背景问题 6 1.2 选题意义 6 1.3 本文内容 7 2 核心功能 9 2.1 功能调研 9 2.2 可行性分析 12 ...

  3. python+tkinter+threading制作多线程简易音乐播放器(自动播放,上一曲,下一曲,播放,暂停,实时显示歌曲名并能自动切换歌曲的功能)

    https://blog.csdn.net/qq_41962782/article/details/80106158 小弟是小白因大作业,在此博文的基础上,我增加了歌曲目录的选择,自动播放,实时显示歌 ...

  4. ios音乐播放器-仿QQ音乐

    这篇文章主要写一个iOS系统下的音乐播放器 , 包括简单的仿QQ音乐播放器界面.音乐播放.歌词解析.后台控制等  ,如果你正好需要 , 希望你看完后能够对你的提升有所帮助 , 当然,阅读中如果发现什么 ...

  5. 卡拉OK效果的实现-iOS音乐播放器

    自己编写的音乐播放器偶然用到这个模块,发现没有思路,而且上网搜了搜,关于这方面的文章不是很多,没找到满意的结果,然后自己也是想了想,最终实现了这种效果,想通了发现其实很简单. 直接上原理: 第一种: ...

  6. javafx音乐播放器----歌词同步实时显示(包含获取酷我歌词方式,歌词同步方法)

    首先我是爬虫获取的酷我的音源,因此歌词也是通过爬虫获取的,下面这个方法可以获取到歌曲对应的歌词信息.简单说下,在搜索歌曲之后会返回一个歌曲列表,查看源代码是包含在li标签里面的,这个li标签里面就有请 ...

  7. 音乐播放器歌词的逐字渲染效果

    受到Android 自定义控件玩转字体变色 打造炫酷ViewPager指示器的启发,决定用这个来仿制大多数音乐播放器的逐字染色效果,效果图如下: 关键点剖析一:逐字染色效果 关键代码如下: publi ...

  8. html音乐播放器代码自动,html5 css3音乐播放器代码

    特效描述:html5 css3 音乐播放器代码.html5 css3音乐播放器代码 代码结构 1. 引入CSS 2. 引入JS 3. HTML代码 标题歌手 00:00/00:00 var music ...

  9. android 音乐播放器-------歌词同步 lrc

    lrc格式 : [al:这首歌所在的唱片集 ] [ar:歌词作者 ] [by:本LRC文件的创建者 ] [offset:+/- 以毫秒为单位整体时间戳调整,+增加,-减小 ] [re:创建此LRC文件 ...

  10. android 本地lrc 歌词同步,android 音乐播放器-------歌词同步 lrc

    lrc格式 : [al:这首歌所在的唱片集 ] [ar:歌词作者 ] [by:本LRC文件的创建者 ] [offset:+/- 以毫秒为单位整体时间戳调整,+增加,-减小 ] [re:创建此LRC文件 ...

最新文章

  1. 王豪:HW265到“HW266”
  2. build muduo网络库undefined reference to问题
  3. 程序员黑话,看懂的都是老司机!
  4. vue 报错 Error: timeout of 5000ms exceeded
  5. 华为服务器麒麟系统,麒麟云服务器
  6. 菲律宾国防部长洛伦扎纳参观中国海军539编队芜湖舰
  7. Linux内核块设备层介绍之bio层
  8. 蚂蚁金服 CEO 突然辞职!去向很意外。。。
  9. 小米笔记本打开matlab,小米笔记本入手指南
  10. 倍加福编码器ENI58IL-H12BA5-1024UD1-RC5
  11. windows下ruby安装环境配置
  12. VTN:视频Transformer网络
  13. 利用百度api接口制作在线语音合成软件
  14. 普陀寺里的穿白T恤的奥特曼 2012年9月8日
  15. BDC模式与OKCODE、 CALL TRANSACTION用法
  16. shapely使用笔记
  17. Python 多张 Excel 合并为一张 Excel
  18. c语言扫雷游戏计时功能_C语言实现扫雷游戏(可以自动展开)
  19. 基于组态王的过程控制——以巧克力牛奶的生产为例
  20. docker 批量删除images

热门文章

  1. DeskSpace 显示Dash board
  2. google工具栏中的自定义搜索按钮
  3. NoteFirst的Word插件(WordAddIn)安装过程中遇到的问题和解决方案
  4. AI 编辑视频!这特效太逆天了!代码开源 SIGGRAPH Asia 2021
  5. IAP:HTTP 的替代者,更快、更丰富
  6. 数据库原理知识点总结一
  7. IT行业的工作内容是什么?
  8. winform小程序-随机抽奖软件
  9. 深入探访支付宝双11十年路,技术凿穿焦虑与想象极限
  10. 学而思网校怎么查看回放 学而思网校查看回放教程