【问题标题】:MPRemoteCommandCenter calls handler multiple times in iOSMPRemoteCommandCenter 在 iOS 中多次调用处理程序
【发布时间】:2016-07-06 01:36:11
【问题描述】:

MPRemoteCommandCenter 多次调用处理程序块并导致对选择器方法的不必要调用。

这里是代码sn-p:

MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

[commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
    NSLog(@"NEXTTTTTT");
    return MPRemoteCommandHandlerStatusSuccess;
}];

[commandCenter.previousTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
    NSLog(@"PREVIOUSSS");
    return MPRemoteCommandHandlerStatusSuccess;
}];

当用户在屏幕锁定时点击音乐播放器底座上的下一个或上一个按钮时,会导致多次调用上述块。

【问题讨论】:

    标签: ios iphone ios9 audio-player mpmusicplayercontroller


    【解决方案1】:

    处理程序将被调用多次,即使它在同一个对象上注册了多次。也许您的代码 sn-p 被多次调用。

    【讨论】:

    • 谢谢!你节省了我的时间。
    【解决方案2】:

    看起来您有多个调用代码的对象实例,例如。如果您为每个轨道推送一个新的 UIViewController。旧的视图控制器可能仍然存在并再次调用处理程序。

    试着把你的代码放进去

    - (void)viewDidAppear:(BOOL)animated
    

    然后像这样禁用它

    - (void)viewWillDisappear:(BOOL)animated {
         MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
        [commandCenter.nextTrackCommand removeTarget:self];
        [commandCenter.previousTrackCommand removeTarget:self];
    }
    

    【讨论】:

    • 感谢您的回答,我认为这应该是解决方案。我设法通过检查自上次单击按钮以来的时间间隔来防止多次调用。
    • 应该注意,removeTarget:self 仅在您添加了带有addTarget(_:action:) 签名的目标时才有效。如果您使用闭包签名 (addTarget(handler:)),则必须保留返回的令牌 (Any) 并使用它来解决目标关系。 command.removeTarget(<previouslyStoredValue>).
    猜你喜欢
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 2013-05-17
    相关资源
    最近更新 更多