程序后台音频继续:
示例代码
#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 1.获取音频会话
AVAudioSession *session = [AVAudioSession sharedInstance];
// 2.设置音频会话的类别为后台播放
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
// 3.激活会话
[session setActive:YES error:nil];
return YES;
}
锁屏状态显示歌词
1、跟据三句歌词开启图片上下文生成图片
-(UIImage*)createImageWithPreviousLrc:(NSString*)previousLrc NextLrc:(NSString*)nextLrc CurrentLrc:(NSString*)currentLrc{
UIImage *image = [UIImage imageNamed:[MusicTool playingMusic].icon];//获取当前播放歌曲的图片
UIGraphicsBeginImageContext(image.size);//开启图片上下文
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];//绘制图片到上下文
//前一句
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.alignment = NSTextAlignmentCenter;
NSDictionary *attributes1 = @{NSFontAttributeName : [UIFont systemFontOfSize:14.0],
NSForegroundColorAttributeName : [UIColor lightGrayColor],
NSParagraphStyleAttributeName : style};
CGRect previousRect = CGRectMake(0, image.size.height - 75, image.size.width, 25);
[previousLrc drawInRect:previousRect withAttributes:attributes1];//绘制文字到上下文
//下一句
CGRect nextRect = CGRectMake(0, image.size.height - 25, image.size.width, 25);
[nextLrc drawInRect:nextRect withAttributes:attributes1];//绘制文字到上下文
//当前句
NSDictionary *attributes2 = @{NSFontAttributeName : [UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName : [UIColor whiteColor],
NSParagraphStyleAttributeName : style};
CGRect currentRect = CGRectMake(0, image.size.height - 50, image.size.width, 25);
[currentLrc drawInRect:currentRect withAttributes:attributes2];//绘制文字到上下文
return UIGraphicsGetImageFromCurrentImageContext();//返回上下文的图片
}
2、将歌曲所有信息包装成字典
-(NSDictionary*)playingMusicDict:(UIImage*)lockImage{
NSMutableDictionary *nowPayingInfo = [NSMutableDictionary dictionary];
[nowPayingInfo setObject:[MusicTool playingMusic].name forKey:MPMediaItemPropertyAlbumTitle];
[nowPayingInfo setObject:[MusicTool playingMusic].singer forKey:MPMediaItemPropertyArtist];
MPMediaItemArtwork *artWork = [[MPMediaItemArtwork alloc] initWithImage:lockImage];
[nowPayingInfo setObject:artWork forKey:MPMediaItemPropertyArtwork];
[nowPayingInfo setObject:@(self.duration) forKey:MPMediaItemPropertyPlaybackDuration];
[nowPayingInfo setObject:@(self.currentTime) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
return nowPayingInfo;
}
3、将字典传递到锁屏界面
//获取锁屏中心
MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];
//设置播放的信息
playingInfoCenter.nowPlayingInfo = [self playingMusicDict:lockImage];
4、 锁屏状态按钮响应
1、didFinishLaunchingWithOptions方法中添加锁屏响应允许
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
2、实现锁屏按钮事件(必须是在控制器)
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
switch (event.subtype) {
case UIEventSubtypeRemoteControlPause:
[self playOrPuase:nil];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self previous:nil];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self next:nil];
break;
}
}
5、拔掉耳机后自动暂停
1、注册音频输出改变通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(routeChange:) name:AVAudioSessionRouteChangeNotification object:nil];
2、判断输出类型
-(void)routeChange:(NSNotification *)notification{
NSDictionary *dic=notification.userInfo;
int changeReason= [dic[AVAudioSessionRouteChangeReasonKey] intValue];
if (changeReason==AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
// 判断输出改变的原因是因为旧输出已经不可用
AVAudioSessionRouteDescription *routeDescription=dic[AVAudioSessionRouteChangePreviousRouteKey];
AVAudioSessionPortDescription *portDescription= [routeDescription.outputs firstObject];
// 判断原输出为耳机
if ([portDescription.portType isEqualToString:@"Headphones"]) [self 暂停];
}
}
3、控制器销毁时注销通知
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVAudioSessionRouteChangeNotification object:nil];
}