2

I realize this question has been asked before, but I have all the same code as other answers (so I think). The pictures and images show up fine and update while a new song becomes current song. I just can't seem to get the play/pause and skip buttons working. This is the code him using.

-(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;
        }
    }
}
Community
  • 1
  • 1
Andy
  • 750
  • 7
  • 23
  • The code you have posted works. As soon as you call `beginReceivingRemoteControlEvents` and you are the first responder, you will receive remote control events. – Sebastian Hojas Jul 14 '16 at 10:57

1 Answers1

2

Use this code:

    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 is for player seek time of current song when you long press next/previous button.

Make sure you set audio session category:

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

Also you need to set playback rate for MPNowPlayingInfoCenter when pause/resume your player, so play/pause button is shown properly.

        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]; 
Borzh
  • 5,069
  • 2
  • 48
  • 64