iOS,MPNowPlayingInfoCenter暂停/播放按钮不起作用?

时间:2015-02-08 00:32:27

标签: ios objective-c xcode

我之前已经意识到这个问题,但我和其他答案都有相同的代码(所以我认为)。当新歌成为当前歌曲时,图片和图像显示正常并更新。我似乎无法让播放/暂停和跳过按钮工作。这是他使用的代码。

-(void)viewWillAppear:(bool)animated
{
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];  
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];

}
-(BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl) {

        switch (event.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:[self playPauseButtonPressed:nil];
                NSLog(@"play pause button remote pressed");

                break;

            case UIEventSubtypeRemoteControlBeginSeekingForward:[self skipButtonPressed:nil];
                NSLog(@"Skip remote pressed");
                break;

            default: break;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

使用此代码:

    switch (receivedEvent.subtype) {
        case UIEventSubtypeRemoteControlPlay:
            [player play];
            break;

        case UIEventSubtypeRemoteControlPause:
            [player pause];
            break;

        case UIEventSubtypeRemoteControlPreviousTrack:
            [player previous];
            break;

        case UIEventSubtypeRemoteControlNextTrack:
            [player next];
            break;

        default:
            break;
    }

UIEventSubtypeRemoteControlBeginSeekingForward用于当您长按下一个/上一个按钮时当前歌曲的玩家寻找时间。

确保设置音频会话类别:

    NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
    [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

在暂停/恢复播放器时,您还需要设置MPNowPlayingInfoCenter的播放速率,以便正确显示播放/暂停按钮。

        MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];

        NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];

        MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"image"]];

        [songInfo setObject:@"your song" forKey:MPMediaItemPropertyTitle];
        [songInfo setObject:@"your artist" forKey:MPMediaItemPropertyArtist];
        [songInfo setObject:@"your album" forKey:MPMediaItemPropertyAlbumTitle];
        [songInfo setObject:[NSNumber numberWithDouble:songProgress] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
        [songInfo setObject:[NSNumber numberWithDouble:songDuration] forKey:MPMediaItemPropertyPlaybackDuration];
        [songInfo setObject:[NSNumber numberWithDouble:(isPaused ? 0.0f : 1.0f)] forKey:MPNowPlayingInfoPropertyPlaybackRate];
        [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];

        [playingInfoCenter setNowPlayingInfo:songInfo];