ios 音频后台播放
2015-11-26 00:00
603 查看
首先配置info中
在需要后台播放的控制器中添加如下代码
//后台播放
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
- (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)configNowPlayingInfoCenter {
isSame = YES;
entity = [musicArray objectAtIndex:musicArrayNumber];
if ( entity == nil) {
return;
}
@autoreleasepool {
NSDictionary *info = self.nowPlayingMusicInfo;
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
//歌曲名称
[dict setObject:entity.title forKey:MPMediaItemPropertyTitle];
//演唱者
[dict setObject:[info ObjectNullForKey:@"author"] forKey:MPMediaItemPropertyArtist];
//专辑名
[dict setObject:[info ObjectNullForKey:@"album"] forKey:MPMediaItemPropertyAlbumTitle];
//专辑缩略图
NSString *localImagePath = [CommonService addLocalRootPath:entity.album1 ParkId:parkEntity.ID];
NSData * thumbData = [NSData dataWithContentsOfFile:localImagePath];
if (thumbData != nil) {
UIImage *image = [UIImage imageWithData:thumbData];
MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:image];
[dict setObject:artwork forKey:MPMediaItemPropertyArtwork];
}else
{
// FIXME: 无图的时候,读取图
}
//音乐剩余时长
[dict setObject:[NSNumber numberWithDouble:finalTime] forKey:MPMediaItemPropertyPlaybackDuration];
// 音乐当前播放时间 在计时器中修改
[dict setObject:[NSNumber numberWithDouble:0.0] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
//设置锁屏状态下屏幕显示播放音乐信息
//DLog(@"显示播放音乐信息:%@\n%@",dict,[info ObjectNullForKey:@"title"]);
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
}
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
-(void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
[self playMusic];
break;
case UIEventSubtypeRemoteControlPause:
[self pauseMusic];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self aboveMusic:_aboveBtn];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self nextMusic:_nextBtn];
break;
default:
break;
}
}
}
在需要后台播放的控制器中添加如下代码
//后台播放
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
- (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)configNowPlayingInfoCenter {
isSame = YES;
entity = [musicArray objectAtIndex:musicArrayNumber];
if ( entity == nil) {
return;
}
@autoreleasepool {
NSDictionary *info = self.nowPlayingMusicInfo;
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
//歌曲名称
[dict setObject:entity.title forKey:MPMediaItemPropertyTitle];
//演唱者
[dict setObject:[info ObjectNullForKey:@"author"] forKey:MPMediaItemPropertyArtist];
//专辑名
[dict setObject:[info ObjectNullForKey:@"album"] forKey:MPMediaItemPropertyAlbumTitle];
//专辑缩略图
NSString *localImagePath = [CommonService addLocalRootPath:entity.album1 ParkId:parkEntity.ID];
NSData * thumbData = [NSData dataWithContentsOfFile:localImagePath];
if (thumbData != nil) {
UIImage *image = [UIImage imageWithData:thumbData];
MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:image];
[dict setObject:artwork forKey:MPMediaItemPropertyArtwork];
}else
{
// FIXME: 无图的时候,读取图
}
//音乐剩余时长
[dict setObject:[NSNumber numberWithDouble:finalTime] forKey:MPMediaItemPropertyPlaybackDuration];
// 音乐当前播放时间 在计时器中修改
[dict setObject:[NSNumber numberWithDouble:0.0] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
//设置锁屏状态下屏幕显示播放音乐信息
//DLog(@"显示播放音乐信息:%@\n%@",dict,[info ObjectNullForKey:@"title"]);
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
}
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
-(void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
[self playMusic];
break;
case UIEventSubtypeRemoteControlPause:
[self pauseMusic];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self aboveMusic:_aboveBtn];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self nextMusic:_nextBtn];
break;
default:
break;
}
}
}
相关文章推荐
- Xcode自带的instrument中的Automation实现自动化测试简单使用
- iOS 图片查看关闭,以及放大缩小旋转移动
- iOS 唯一标示用户的设备
- iOS开发CoreAnimation解读之二——对CALayer的分析
- IOS 真机调试 由于安全原因未能启动的解决方法
- iOS开发多线程-NSOperation简单介绍
- iOS 细节 问题
- iOS工程如何引用第三方类库
- Core Motion
- iOS应用生命周期
- ios证书
- 蓝懿 iOS 技术交流和心得分享 11.25
- ReactNativeiOS(二)读书记录 2代码结构
- iOS 中图片循环动画
- ios nil、NULL和NSNull 的使用
- iOS新闻客户端开发教程7-新闻列表
- iOS中的MVVM
- iOS 开发值翻牌子游戏
- iOS 开发之封装
- iOS之Button以及警告视图(简单)