ios 音频后台播放

首先配置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;  

        }  

    }  

}



你可能感兴趣的:(ios 音频后台播放)