AVAudioPlayer 플레이어 잠금 화면 인터페이스

2059 단어
Info에서plist에 Required background modes를 추가하고 펼친 후 App plays audio or streams audio/video using AirPlay 선택
// 
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error = nil;
[session setCategory:AVAudioSessionCategoryPlayback error:&error];
if (error) {
    NSLog(@"%s %@", __func__, error);
}
[session setActive:YES error:nil];
// ( )
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

#pragma mark  
-(void)remoteControlReceivedWithEvent:(UIEvent *)event{
    // 
    if (event.type == UIEventTypeRemoteControl) {
        // 
        [self myRemoteEventBlock:event];
     }
}

- (void)myRemoteEventBlock:(UIEvent*)event{
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlPlay:
            [self.avAudioPlayer play];
            break;
        case UIEventSubtypeRemoteControlPause:
            [self.avAudioPlayer pause];
            break;
        case UIEventSubtypeRemoteControlPreviousTrack:
            [self last];
            break;
        case UIEventSubtypeRemoteControlNextTrack:
            [self next];
            break;
        
        default:
            break;
    }
};

-(void)next{
    if (self.flag < self.array.count - 1) {
        self.flag++;
    }else{
        self.flag = 0;
    }
    [self setupUI:self.flag];
}

-(void)last{
    if (self.flag <= 0) {
        self.flag = self.array.count - 1;
    }else{
        self.flag --;
    }
    [self setupUI:self.flag];
}



// 
NSMutableDictionary *info = [NSMutableDictionary dictionary];
// 
info[MPMediaItemPropertyAlbumTitle] = @" ";
// 
info[MPMediaItemPropertyTitle] = self.array[index];
// 
info[MPMediaItemPropertyArtist] = self.array[index];
// ,  ,  
UIImage *image = [UIImage imageNamed:@"01"];
MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:image];
info[MPMediaItemPropertyArtwork] = artwork;
// 
info[MPMediaItemPropertyPlaybackDuration] = @(self.avAudioPlayer.duration);
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = info;

좋은 웹페이지 즐겨찾기