【问题标题】:MPRemoteCommandCenter Not displaying in lock screenMPRemoteCommandCenter 未在锁定屏幕中显示
【发布时间】:2020-07-03 10:47:58
【问题描述】:

可能这是一个重复的问题,但没有一个解决方案适合我。我几乎尝试了所有方法。

正在播放的信息没有在锁定屏幕中更新。

Swift 版本:5 & iOS版本:13

这是我的代码

func setupRemoteCommandCenter() {
        UIApplication.shared.beginReceivingRemoteControlEvents()

        let commandCenter = MPRemoteCommandCenter.shared()
        

        commandCenter.playCommand.addTarget { event in
            return .success
        }
        

        commandCenter.pauseCommand.addTarget { event in
            return .success
        }
        

        commandCenter.nextTrackCommand.addTarget { event in
            return .success
        }
        

        commandCenter.previousTrackCommand.addTarget { event in
            return .success
        }
    }
    
    func updateLockScreen() {
        var nowPlayingInfo = [String : Any]()
        nowPlayingInfo[MPMediaItemPropertyArtist] = "Artist"
        nowPlayingInfo[MPMediaItemPropertyTitle] = "title"
        MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
    }

从 viewDidLoad 调用一个方法,即

override func viewDidLoad() {
        super.viewDidLoad()
        AudioManager.shared.audioManageDelegate = self
        // Do any additional setup after loading the view.
        setupRemoteCommandCenter()
    }

另一个是从 playbuttonAction 方法调用的,即

@IBAction func togglePlayPauseButton(sender: UIButton) {
        //play pause button
        sender.isSelected = !sender.isSelected
        //updateLockScreen() //I checked it from calling here also
        if sender.isSelected {
            AudioManager.shared.playMusic()
        } else {
            AudioManager.shared.pauseMusic()
        }
        updateLockScreen()
    }

我的 Appdelegate 在这里

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        setUPforbackground()
        return true
    }

    func setUPforbackground() {
        do {
            try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
            print("Playback OK")
            try AVAudioSession.sharedInstance().setActive(true)
            print("Session is Active")
        } catch {
            print(error)
        }
    }

在调用playMusic() 之前调用updateLockScreen() 也没有结果。我错过了什么吗?

【问题讨论】:

    标签: ios swift avplayer mpremotecommandcenter


    【解决方案1】:

    仔细分析代码后发现

    try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])

    抛出错误所以我把它改成了下面

    try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
    

    希望它会帮助某人。谢谢。

    【讨论】:

      猜你喜欢
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多