iOS ProcessiOS 实用iOS学习开发

学习之—后台、锁屏模式三部曲

2015-09-19  本文已影响721人  sydie
 // 获取音频会话
    AVAudioSession *session = [AVAudioSession sharedInstance];
    // 设置后台模式
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    // 激活后台模式
    [session setActive:YES error:nil];
#pragma mark - 设置锁屏信息
- (void)setUpLockScreenInfoWithPlayingMusic:(CXLMusic *)playingMusic
{
    //    1.获取锁屏信息
    MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
    
    // 设置锁屏界面信息
        // 获取歌名、歌手名、歌手照片等信息
    NSMutableDictionary *playingInfo = [NSMutableDictionary dictionary];
    playingInfo[MPMediaItemPropertyAlbumTitle] = playingMusic.name;
    playingInfo[MPMediaItemPropertyAlbumArtist] = playingMusic.singer;
    playingInfo[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:playingMusic.singerIcon]];
    playingInfo[MPMediaItemPropertyPlaybackDuration] = @(self.currentPlayer.duration);
  // 设置信息 
    infoCenter.nowPlayingInfo = playingInfo;
    
    // 接收远程事件
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlPlay:
        case UIEventSubtypeRemoteControlPause:
            [self playOrPuase];
            break;
        case UIEventSubtypeRemoteControlNextTrack:
            [self nextMusicClick];
            break;
            case UIEventSubtypeRemoteControlPreviousTrack:
            [self preMusicClick];
            break;
            
        default:
            break;
    }
}
Snip20150919_6.png
上一篇 下一篇

猜你喜欢

热点阅读