iOS控制中心音乐控件停止在iOS 11更新中工作(remoteControlreceivedWithEvent并未称为



我对iOS控制中心音乐控件有一个问题

但是,在iOS 11中,它停止工作。经过一项研究,我发现在iOS 11中,从来没有称呼过remoteconecontevieldwithevent,但是,在iOS 9的较旧iOS版本中,它被称为正常

我在 AppDelegate.m

上设置了我的事件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Enable background audio listening
    // https://developer.apple.com/library/ios/qa/qa1668/_index.html
    NSError *error = nil;
    if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error]) {
        NSLog(@"%@", error);
    }
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
    if (receivedEvent.type == UIEventTypeRemoteControl) {
        switch (receivedEvent.subtype) {
            case UIEventSubtypeRemoteControlPlay:
                [[NSNotificationCenter defaultCenter] postNotificationName:kCYCAppDelegatePlayPauseNotificationName object:nil];
                break;
            case UIEventSubtypeRemoteControlPause:
                [[NSNotificationCenter defaultCenter] postNotificationName:kCYCAppDelegatePlayPauseNotificationName object:nil];
                break;
            case UIEventSubtypeRemoteControlTogglePlayPause:
                [[NSNotificationCenter defaultCenter] postNotificationName:kCYCAppDelegatePlayPauseNotificationName object:nil];
                break;
            default:
                break;
        }
    }
}

我也订阅了另一个类中的远程事件以控制播放/暂停按钮

- (void)subscribeToRemoteControlEvents {
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.1")) {
        // Disables the forward/backward buttons and only shows the play button.
        // You can't just enable the command, you must subscribe for this to activate, so
        // the subscription in this case doesn't do anything.
        [MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand.enabled = YES;
        [[MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand addTarget:self action:@selector(ignore_removeCommandCenterFired)];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self forName:kCYCAppDelegatePlayPauseNotificationName object:nil queue:nil usingBlock:^(NSNotification *note, CYCCastManager *observer) {
        if (observer.isCastPlaying) {
            [observer pause];
        }
        else {
            [observer play:NO];
        }
    }];
}
- (void)unsubscribeFromRemoteControlEvents {
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.1")) {
        [[MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand removeTarget:self action:@selector(ignore_removeCommandCenterFired)];
    }
}

我想知道为什么不再工作了,我确实在API中检查了文档,但我看不到更改

注意:我检查以下链接,没有运气

ios-未收到的uieventtyperemotecontrol事件

https://forums.developer.apple.com/thread/84204

Avaudio中的RemoteControlreceivedwithEvent并未称为

remotecontrolreceivedwithevent未在appdelegate

中调用

最后,我通过使用remotecommandcenter并播放和暂停按钮而不是toogleplaypausecommand

来解决问题
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0")){
    //NOTE: this is the only way that I find to make this work on IOS 11 its seems to be that togglePlayPauseCommand is not working anymore
    MPRemoteCommandCenter* commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
    [commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kCYCAppDelegatePlayNotificationName object:nil];
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    [commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kCYCAppDelegatePauseNotificationName object:nil];
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self forName:kCYCAppDelegatePlayNotificationName object:nil queue:nil usingBlock:^(NSNotification *note, CYCCastManager *observer) {
        if (!observer.isCastPlaying) {
            [observer play:NO];
        }
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self forName:kCYCAppDelegatePauseNotificationName object:nil queue:nil usingBlock:^(NSNotification *note, CYCCastManager *observer) {
        if (observer.isCastPlaying) {
            [observer pause];
        }
    }];

}

刚刚由胡安的答案修改。

if(@available(iOS 11, *)) {
    MPRemoteCommandCenter* commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
    [commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"RemotePlayCommandNotification" object:nil];
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    [commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"RemotePauseCommandNotification" object:nil];
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    [[NSNotificationCenter defaultCenter] addObserverForName:@"RemotePlayCommandNotification" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        NSLog(@"Clicked the play button.");
    }];
    [[NSNotificationCenter defaultCenter] addObserverForName:@"RemotePauseCommandNotification" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        NSLog(@"Clicked the pause button.");
    }];
}

相关内容

  • 没有找到相关文章

最新更新