3

Я использую этот код, чтобы проигрывать аудио, когда устройство заблокировано.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

Слайдер громкости работает нормально, но кнопка плей/пауза и кнопки перемотки не работают. Почему?

1 ответ 1

1

Скорей всего потому что не обрабатывается метод - (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent

Пример

AppDelegate.m

- (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent {

    if (theEvent.type == UIEventTypeRemoteControl)  {
        switch(theEvent.subtype)        {
            case UIEventSubtypeRemoteControlPlay:
                [[NSNotificationCenter defaultCenter] postNotificationName:@"PlayNotification" object:theEvent];
                break;
            case UIEventSubtypeRemoteControlPause:
                [[NSNotificationCenter defaultCenter] postNotificationName:@"PauseNotification" object:theEvent];
                break;
            case UIEventSubtypeRemoteControlTogglePlayPause:
                [[NSNotificationCenter defaultCenter] postNotificationName:@"TogglePlayPauseNotification" object:theEvent];
                break;
            default:
                return;
        }
    }
}

RadioViewController.m

@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, getter=isPlaying) BOOL playing;

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    ...
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(play) name:@"PlayNotification" object:nil];
    [notificationCenter addObserver:self selector:@selector(pause) name:@"PauseNotification" object:nil];
    [notificationCenter addObserver:self selector:@selector(togglePlayPause) name:@"TogglePlayPauseNotification" object:nil];
}

- (void)play
{
   [self.player play];
   self.playing = YES;
}

- (void)pause
{
   [self.player pause];
   self.playing = NO;
}

- (void)togglePlayPause
{
    (self.isPlaying) ? [self pause] : [self play];
}
0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.