Bridge-老大 本地音乐播放器---持续更新(伴奏/原唱切换
2017-07-12 本文已影响1986人
Renjiee
最近看了《中国有嘻哈》听了Bridge的那首《老大》有点中毒,把以前在学校时写的音乐播放器Demo重新修改了一下,花了10元钱在淘宝去做了这首歌的伴奏,不过还是很坑,听了勿喷。下面进入正题
先来两张效果图压压惊,感觉个人审美还是不错的
老大 老大大
代码我在这里只贴几个部分功能,下面👇有Demo下载链接,歌曲、伴奏、歌词里面都有,还望喜欢的朋友给个Stare✨
引用库引用库
AudioManager.h文件
// 伴奏
#define kBackGroundMusic @"老大伴奏"
// 原声
#define KBackGroundMusic1 @"Bridge - 老大"
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface AudioManager : NSObject
@property (nonatomic, strong) NSArray *musicArray;
@property (strong, nonatomic) AVAudioPlayer *musicPlayer;
+(instancetype)defaultManager;
- (BOOL)playMusic:(NSString *)musicFileName;
//-(NSArray *)musicArray;
-(void)pause;///<暂停后继续播放 回到最初
-(void)stop;
-(BOOL)play;
-(BOOL)isPlaying;
AudioManager.m文件
#import "AudioManager.h"
@interface AudioManager()
@end
@implementation AudioManager
+(instancetype)defaultManager
{
static AudioManager *manager = nil;
@synchronized(self) {
if (!manager) {
manager = [[AudioManager alloc] init];
}
}
return manager;
}
-(BOOL)playMusic:(NSString *)musicFileName
{
NSURL *musicURL = [[NSBundle mainBundle]URLForResource:musicFileName withExtension:@"mp3"];
if(!musicURL)
{
return NO;
}
NSError *error = nil;
self.musicPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:musicURL error:&error];
if(error)
{
NSLog(@"播放文件%@出错,错误为%@",musicFileName,error);
}
// self.musicPlayer.numberOfLoops = -1;///<音乐循环播放
[self.musicPlayer play];
return YES;
}
-(void)pause
{
[self.musicPlayer pause];
}
-(void)stop
{
[self.musicPlayer stop];
// self.musicPlayer.currentTime = 0;
}
-(BOOL)play
{
if([self.musicPlayer prepareToPlay])
{
[self.musicPlayer play];
return YES;
}
return NO;
}
-(BOOL)isPlaying
{
return self.musicPlayer.isPlaying;
}
暂停/播放、上一首、下一首按钮点击事件
#pragma mark - 点击左键事件
- (IBAction)leftBtn:(UIButton *)sender
{
[self playPrev];
}
#pragma mark - 点击右键事件
- (IBAction)rightBtn:(UIButton *)sender
{
[self playNext];
}
// 切换播放、暂停按钮
- (void) resumeOrPause{
if(self.isPlayer.selected == YES)
{
self.isPlayer.selected = NO;
[[AudioManager defaultManager]stop];
}
else if(self.isPlayer.selected == NO)
{
self.isPlayer.selected =YES;
[[AudioManager defaultManager]play];
}
}
歌词解析
/**
* 解析歌词
*/
-(void)parselyric
{
if([self.musicName.text isEqualToString:KBackGroundMusic1])
{
self.lyricPath = [[NSBundle mainBundle]pathForResource:@"老大歌词" ofType:@"lrc"];///<歌词文件路径
}
else if([self.musicName.text isEqualToString:kBackGroundMusic])
{
self.lyricPath = [[NSBundle mainBundle]pathForResource:@"老大歌词" ofType:@"lrc"];
}
if ([self.lyricPath length]) {
//get the lyric string
NSString *lyc = [NSString stringWithContentsOfFile:self.lyricPath encoding:NSUTF8StringEncoding error:nil];
//init
_musictime = [[NSMutableArray alloc]init];
_lyrics = [[NSMutableArray alloc]init];
self.musictime = [NSMutableArray arrayWithCapacity:0];
self.lrcDic = [NSMutableDictionary dictionaryWithCapacity:0];
NSArray *array = [lyc componentsSeparatedByString:@"\n"];
for (int i = 0; i < array.count; i++)
{
NSString *lineString = [array objectAtIndex:i];
NSArray *lineArray = [lineString componentsSeparatedByString:@"]"];
if ([lineArray[0] length] > 7) {
NSString *str1 = [lineString substringWithRange:NSMakeRange(3, 1)];
NSString *str2 = [lineString substringWithRange:NSMakeRange(6, 1)];
if ([str1 isEqualToString:@":"]&&[str2 isEqualToString:@"."])
{
//截取歌词和时间;
NSString *timeStr = [lineString substringWithRange:NSMakeRange(1, 5)];
NSString *lrcStr = [lineString substringFromIndex:10 ];///<歌词字符串
[self.musictime addObject:timeStr];
[self.lrcDic setObject:lrcStr forKey:timeStr];
}
}
}
}
}
授权锁屏后台播放
在AppDelegate中注册后台播放
AVAudioSession *session = [AVAudioSession sharedInstance];
if ([session setCategory:AVAudioSessionCategoryPlayback error:nil]) {
if ([session setActive:YES error:nil]) {
NSLog(@"后台播放准备完毕");
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
在每一首音乐切换的时候调用
- (void)configPlayingInfo{
if(NSClassFromString(@"MPNowPlayingInfoCenter")){
NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
[dict setObject:@"Bridge - 老大" forKey:MPMediaItemPropertyTitle];
[dict setObject:@"Bridge" forKey:MPMediaItemPropertyArtist];
[dict setObject:[[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"Bridge"]] forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
}
}
AVAudioPlayerDelegate
#pragma mark - AVAudioPlayerDelegate
/**
* 自动播放下一首
*/
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[self playNext];
}
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error
{
// 解码失败,自动播放下一首
[self playNext];
}
// 音乐播放器被打断 (如开始 打、接电话)
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
// 会自动暂停 do nothing ...
NSLog(@"audioPlayerBeginInterruption---被打断");
}
// 音乐播放器打断终止 (如结束 打、接电话)
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags
{
// 手动恢复播放
[player play];
NSLog(@"audioPlayerEndInterruption---打断终止");
}
耳机线控
- (void)remoteControlReceivedWithEvent:(UIEvent*)event{
if(event.type == UIEventTypeRemoteControl){
switch(event.subtype){
case UIEventSubtypeRemoteControlTogglePlayPause:
[self resumeOrPause];
// 切换播放、暂停按钮
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self playPrev];
// 播放上一曲按钮
break;
case UIEventSubtypeRemoteControlNextTrack:
[self playNext];
// 播放下一曲按钮
break;
default:
break;
}
}
}
Demo下载地址 喜欢的记得给个Stare✨ 欢迎大家提交建议
我想在跑车里 想要一辆法拉利
我是Renjiee 我要做最骚的程序猿👨💻