gpt4 book ai didi

ios - 使用 MPRemoteCommandCenter 时未调用播放/暂停回调

转载 作者:搜寻专家 更新时间:2023-10-31 08:06:51 34 4
gpt4 key购买 nike

我正在构建一个音乐播放器应用,一切正常。到目前为止,我一直在使用系统音乐播放器,但现在想改用应用程序音乐播放器,这就是我遇到问题的地方——我这辈子都不知道如何获得我的音乐播放器从 iOS 控制中心调用播放/暂停回调。这是我在主视图 Controller 中使用的代码:

override func viewDidLoad() {
super.viewDidLoad()

self.musicPlayer = MPMusicPlayerController.applicationMusicPlayer()

self.registerForMediaPlayerNotifications()
UIApplication.sharedApplication().beginReceivingRemoteControlEvents()

let commandCenter = MPRemoteCommandCenter.sharedCommandCenter()

commandCenter.previousTrackCommand.enabled = false
commandCenter.previousTrackCommand.addTarget(self, action: "previousTrack")
commandCenter.nextTrackCommand.enabled = false
commandCenter.nextTrackCommand.addTarget(self, action: "nextTrack")
commandCenter.togglePlayPauseCommand.enabled = true
commandCenter.togglePlayPauseCommand.addTarget(self, action: "playOrPauseMusic")
commandCenter.pauseCommand.addTarget(self, action: "playOrPauseMusic")
commandCenter.pauseCommand.enabled = true
commandCenter.playCommand.addTarget(self, action: "playOrPauseMusic")
commandCenter.playCommand.enabled = true

[...]
}


func previousTrack() {
}

func nextTrack() {
}

func playOrPauseMusic() {
print("playOrPause")
}

最佳答案

我遇到了同样的问题并注意到 togglePlayPauseCommand 的回调没有被调用,但是 previousTrackCommand 被调用了。因此,经过一些实验后,我通过删除 togglePlayPauseCommand 并单独实现 playCommandpauseCommand 的内联回调来实现此功能 - 请注意使用自定义选择器不起作用,它们必须是内联回调。

let commandCenter = MPRemoteCommandCenter.shared()

commandCenter.playCommand.addTarget { (commandEvent) -> MPRemoteCommandHandlerStatus in
moviePlayer.prepareToPlay()
moviePlayer.play()
return MPRemoteCommandHandlerStatus.success
}

commandCenter.pauseCommand.addTarget { (commandEvent) -> MPRemoteCommandHandlerStatus in
moviePlayer.pause()
return MPRemoteCommandHandlerStatus.success
}

关于ios - 使用 MPRemoteCommandCenter 时未调用播放/暂停回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34925641/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com