Audio playback lock screen control not displaying

时间:2018-03-23 00:20:51

标签: swift avplayer avaudiosession

I am trying display audio controls on the lock screen, but the problem is the audio control does not display anything in the lock screen. I already enabled background modes and the audio plays in the background.

In app delegate class, right when my app launches, I set up my audio session

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    setupAudioSession()
    UIApplication.shared.beginReceivingRemoteControlEvents()
    return true
}

func setupAudioSession(){
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])
        self.becomeFirstResponder()

        do {
            try AVAudioSession.sharedInstance().setActive(true)
            print("AVAudioSession is Active")
        } catch let error as NSError {
            print(error.localizedDescription)

        }
    } catch let error as NSError {
        print(error.localizedDescription)
    }
}

In my main controller, I call the setupLockScreen function after I play the audio.

func setupLockScreen(){
    let commandCenter = MPRemoteCommandCenter.shared()
    commandCenter.playCommand.isEnabled = true
    commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
        if self.player?.rate == 0.0 {
            self.player?.play()
            return .success
        }
        return .commandFailed
    }
    var nowPlayingInfo = [String : Any]()
    nowPlayingInfo[MPMediaItemPropertyTitle] = "My Song"
    nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = audioplayerItem.duration.seconds
    nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = audioplayerItem.asset.duration.seconds
    nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player?.rate

    MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}

I read many articles and I looked all the qustions in Stack Overflow but no luck.

2 个答案:

答案 0 :(得分:1)

nowPlayingInfo的问题是否未显示在锁定屏幕上,或者您添加的控件是否没有响应?

要让您的nowPlayingInfo出现在锁定屏幕上,您需要做一些事情listed here in an answer on Handling External Player Events Notifications。一种是具有不可混合的音频会话(您已经使用AVAudioSessionCategoryPlayback),另一种是播放音频或者最近才停止播放音频。你的应用实际上是在播放音频吗

背景音频,你有一种派生的要求,因为lockscreen => background =>播放音频所需的背景音频,所以我应该把它添加到另一个答案。

如果问题是锁屏控件未启用/响应,请尝试添加pauseCommandplayCommand / pauseCommand对似乎是接收锁屏/外部播放器命令的最低要求。

P.S。你的MPNowPlayingInfoPropertyElapsedPlaybackTime看起来不对劲。不应该

nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(audioplayerItem.currentTime)

答案 1 :(得分:0)

Do it like this (this is swift 3):

override func viewDidLoad() {
    super.viewDidLoad()

    UIApplication.shared.beginReceivingRemoteControlEvents()
    let commandCenter = MPRemoteCommandCenter.shared()

    commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
        //Update your button here for the pause command
        return .success
    }

    commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
        //Update your button here for the play command
        return .success
    }

}

This answer found HERE