2. 在工程的AppDelegate.m文件的didFinishLaunchingWithOptions方法中加入如下代码:
//这种方式后台,可以连续播放非网络请求歌曲。遇到网络请求歌曲就废,需要后台申请task
* AudioSessionInitialize用于处理中断处理,
* AVAudioSession主要调用setCategory和setActive方法来进行设置,
* AVAudioSessionCategoryPlayback一般用于支持后台播放
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[session setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
NSError *activationError = nil;
[session setActive:YES error:&activationError];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"qhc" ofType:@"caf"];
BOOL fileexit = [[NSFileManager defaultManager] fileExistsAtPath:filepath];
if (_player && [_player isPlaying]) {
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filepath] error:nil];
经过上面简单的处理后,已经可以在后台播放歌曲了。但是使用过qq,酷狗的播放器后,就会发现一个很炫的功能,就是在锁屏的状态下,可以调用播放器,并切换歌曲,而且还可以显示歌曲对应的图片。
之前没搞过播放器,一直不理解实现原理,百度了很多次,使用的关键字为ios播放器,百度总是找给我一批垃圾资料。
后来同事帮忙找了份资料,然后我换歌关键字,终于找到我需要的资料了,现在也自己整理下。
1. 检查你的AppDelegate是不是继承于UIResponder,有的是自动生成,有的人习惯手写,如果是继承于NSObject请改为 AppDelegate : UIResponder。
-(void)configNowPlayingInfoCenter{
if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:@"name" forKey:MPMediaItemPropertyTitle];
[dict setObject:@"singer" forKey:MPMediaItemPropertyArtist];
[dict setObject:@"album" forKey:MPMediaItemPropertyAlbumTitle];
UIImage *image = [UIImage imageNamed:@"test.jpg"];
MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:image];
[dict setObject:artwork forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
上面的if (NSClassFromString(@"MPNowPlayingInfoCenter"))语句,说是为了避免了版本兼容问题。
这个API貌似只出现在5里面。现在你可以发现,上面的代码可能不起作用,而且待机屏的播放暂停也不会起作用。
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self configNowPlayingInfoCenter];
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
本人是在单一的viewcontroller里面的调试的,所以如上面的语句添加。也可以自行调整位置.
完成以上的方法后,是不是觉得程序也没做什么调整处理嘛,运行上面的代码,会发现只有信息出来,但是播放控制还是不起作用。
-(BOOL)canBecomeFirstResponder{
-(void)remoteControlReceivedWithEvent:(UIEvent *)event{
//if it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl) {
case UIEventSubtypeRemoteControlTogglePlayPause:
NSLog(@"UIEventSubtypeRemoteControlTogglePlayPause...");
case UIEventSubtypeRemoteControlPlay:
NSLog(@"UIEventSubtypeRemoteControlPlay...");
case UIEventSubtypeRemoteControlPause:
NSLog(@"UIEventSubtypeRemoteControlPause...");
case UIEventSubtypeRemoteControlStop:
NSLog(@"UIEventSubtypeRemoteControlStop...");
case UIEventSubtypeRemoteControlNextTrack:
NSLog(@"UIEventSubtypeRemoteControlNextTrack...");
case UIEventSubtypeRemoteControlPreviousTrack: