gpt4 book ai didi

swift - MPRemoteCommandCenter 播放/暂停 触摸时闪烁

转载 作者:搜寻专家 更新时间:2023-11-01 05:54:46 30 4
gpt4 key购买 nike

我正在开发用 Swift 编写的音乐播放器应用程序,使用 AVPlayer 播放音频流,一切都很好

但是当我尝试将 MPRemoteCommandCenter 添加到我的应用程序时,出现了很多错误,我什至不知道为什么会这样

link to video that describes my problem

AVPlayer 实现如下:

func setupPlayer() {
let item = AVPlayerItem(url: musicURL)
self.player = AVPlayer.init(playerItem: item)
self.player.play()
self.player.volume = 1
self.player.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: DispatchQueue.main, using: { (time) in
if self.player.currentItem?.status == .readyToPlay {
self.reloadNowPlayingInfo()
let currentTime = self.player.currentTime().seconds
self.playingTime.text = currentTime.getTimeString()
self.playerSlider.value = currentTime/duration
}
})
}

func reloadNowPlayingInfo() {
var info = [String : Any]()
info[MPMediaItemPropertyTitle] = self.titleText
info[MPMediaItemPropertyArtwork] = MPMediaItemArtwork.init("some image")
info[MPMediaItemPropertyPlaybackDuration] = seconds
info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentSecs
info[MPMediaItemPropertyArtist] = "Artist name"
MPNowPlayingInfoCenter.default().nowPlayingInfo = info
}

对于指挥中心,

MPRemoteCommandCenter 实现方式如下:

func setupCommandCenter() {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.pauseCommand.isEnabled = true
commandCenter.playCommand.addTarget(self, action: #selector(self.playCommand(_:)))
commandCenter.pauseCommand.addTarget(self, action: #selector(self.pauseCommand(_:)))
}


@objc func playCenter(_ action: MPRemoteCommandEvent) {
self.state = .play
self.playBtn.setBackgroundImage("some image"), for: .normal)
self.player.play()
self.fetchTracks()
}
@objc func pauseCenter(_ action: MPRemoteCommandEvent) {
self.state = .pause
self.playBtn.setBackgroundImage("some image"), for: .normal)
self.player.pause()
self.fetchTracks()
}

最佳答案

除了您提供的代码之外,您还可能在应用委托(delegate)的某处调用了以下代码:

UIApplication.shared.beginReceivingRemoteControlEvents()

除了使用 MPRemoteCommandCenter.shared() 之外执行此操作似乎会导致竞争条件。

根据 Apple's documentation :

In iOS 7.1 and later, use the shared MPRemoteCommandCenter object to register for remote control events. You do not need to call this method when using the shared command center object.

This method starts the delivery of remote control events using the responder chain.

从您的应用委托(delegate)中删除该方法,您应该没问题。

关于swift - MPRemoteCommandCenter 播放/暂停 触摸时闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53989356/

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