首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS控制中心音乐控件在iOS 11更新中停止工作(remoteControlReceivedWithEvent不称为iOS 11)

iOS控制中心音乐控件在iOS 11更新中停止工作(remoteControlReceivedWithEvent不称为iOS 11)
EN

Stack Overflow用户
提问于 2017-09-26 16:38:19
回答 2查看 794关注 0票数 2

在iOS 11更新之前,我遇到了iOS控制中心音乐控制方面的问题,“播放暂停”按钮已经启用并正常工作,就像预期的那样。

然而,在iOS 11中,它停止了工作。经过一项研究,我发现在IOS 11中,remoteControlReceivedWithEvent从未被调用过,但在较早的iOS版本(如iOS 9)中,它通常被调用。

我在AppDelegate.m上设置了事件

代码语言:javascript
复制
- (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;
        }
    }
}

此外,我还订阅另一个类中的远程事件,以控制播放/暂停按钮。

代码语言:javascript
复制
- (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 events not received

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

Unable to receive remoteControlReceivedWithEvent - objective c - ios

remoteControlReceivedWithEvent in AVAudio is not being called

remoteControlReceivedWithEvent not Called in appDelegate

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-03 19:49:32

最后,我使用remoteCommandCenter和play和pause按钮来解决这个问题,而不是tooglePlayPauseCommand。

代码语言:javascript
复制
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];
        }
    }];


}
票数 3
EN

Stack Overflow用户

发布于 2017-12-20 01:44:58

只是被胡安的回答修改了。

代码语言:javascript
复制
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.");
    }];

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46431723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档