Swift MPRemoteCommandCenter nextTrackCommand 被调用 3 次

标签 swift xcode avplayer mpremotecommandcenter

我正在使用 MPRemoteCommandCenter 更改我的音乐广播应用程序中的歌曲。但是当我从锁定屏幕按下下一首轨道按钮时,它会跳 3 步而不是一步。

这是我的代码:

func setupRemoteTransportControls() {
        let commandCenter = MPRemoteCommandCenter.shared()

        commandCenter.nextTrackCommand.addTarget { [unowned self]     event in
        print("Next")
        self.stationIndex = (self.stationIndex+1)
        self.currentStation = self.stations[self.stationIndex]
            return .success
        }
}

输出是: 下一个 下一个 下一步

但我只按了一次按钮。我该怎么做才能只输出一次而不是 3 次?

最佳答案

问题很可能是您多次调用 setupRemoteTransportControls。每次执行此操作时,您都会调用 commandCenter.nextTrackCommand.addTarget 并设置一个新的 Action 目标对(无需删除现有的)。因此,当用户按下按钮时,所有这些对都会触发。

关于Swift MPRemoteCommandCenter nextTrackCommand 被调用 3 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57128152/

相关文章:

ios - Objective C 到 Swift 的互操作性问题

ios - Swift:使用保护语句的好处?

ios - 从具有特殊字符的 URL 加载图像不起作用

ios - AVPlayer - 检索所有格式的音轨

ios - 使用 localIdentifier,swift 访问视频

swift - 扫描真实世界对象并从中生成 3D 网格

ios - 是否可以在 rootViewController 上调用方法?

ios - Xcode 4.5 (iOS) - 链接项目(库和应用程序)

ios - 我错过了 ARC 的东西吗?

objective-c - 当铃声静音时,如何防止我的应用程序(使用 AVPlayer)被静音?