【问题标题】:How to vanish player controls from lockscreen after the playback has finished in iOS10+?iOS10+播放结束后如何从锁屏中消失播放器控件?
【发布时间】:2018-09-09 10:08:46
【问题描述】:

在后台模式下播放音频时,锁定屏幕上会出现播放器控件。音频停止时如何删除它?如果尝试设置:

MPNowPlayingInfoCenter.default().nowPlayingInfo = nil

播放器仍在锁屏上,但艺术家/歌曲字段为空

UPD(我的音频会话代码):

在 AppDelegate 中:

func setupAudioSession() {

    let audioSession = AVAudioSession.sharedInstance()

    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayback)
        try audioSession.setActive(true)

    } catch {
        print("Setting category to AVAudioSessionCategoryPlayback failed.")
    }
}

在播放器类中:

private func clearRemotePlayerInfo() { // call after stop button pressed
    try? AVAudioSession.sharedInstance().setActive(false)
    MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
}

【问题讨论】:

  • 你的意思是“当音频停止时”音频已经播放完毕(直到音频文件结束)?

标签: ios swift avaudiosession background-audio


【解决方案1】:

TL;DR

Github 上的示例:https://github.com/JakubMazur/SO52243428


您不应将nil 分配给此nowPlayingInfo

你应该做的是:

  1. 停止播放(不是必需的,但最好清理您创建的内容)
  2. 将会话设置为非活动状态
  3. 清除nowPlayingInfo

所以代码看起来像:

self.player?.stop() // Where self.player is AVAudioPlayer
try? self.session?.setActive(false, with: .notifyOthersOnDeactivation) // Where self.session is AVAudioSession. You should do it with do-catch to good error catching.
MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]

它的行为是这样的:

编辑:

我已经写了尽可能简单的例子来试一试。它可以在 Github https://github.com/JakubMazur/SO52243428 上找到。随意检查一下,它与您的情况相符。

【讨论】:

  • 非常感谢您的回答。但我无法得到相同的结果。场地被清除,但玩家区域并没有消失。我现在会更新我的问题
  • 请查看更新后的答案,我确实提供了尽可能简单的示例,对于这种情况,它仅在 AppDelegate 中,起点将是 didFinishLaunchingWithOptions
  • Jakub,看看这个:streamable.com/0g7ih(在我的设备上启动你的项目)
  • 有趣!是 iOS 10 还是 11?
  • ios 11 (11.2.5)
【解决方案2】:

这对我有用。

func setupNowPlaying() {
    var nowPlayingInfo = [String : Any]()
    nowPlayingInfo[MPMediaItemPropertyTitle] = "my video"
    if let image = UIImage(named: "video image") {
        nowPlayingInfo[MPMediaItemPropertyArtwork] =
            MPMediaItemArtwork(boundsSize: image.size) { size in
                return image
        }
    }
    let playerItem = self.player?.currentItem
    
    nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = playerItem?.currentTime().seconds
    nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = playerItem?.asset.duration.seconds
    nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = self.player?.rate
 
    
    MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
    
    UIApplication.shared.beginReceivingRemoteControlEvents()
    
}


func closeVideo() {
    self.player?.pause()
    
    let sessionAV = AVAudioSession.sharedInstance()
    try? sessionAV.setActive(false, options: AVAudioSession.SetActiveOptions.notifyOthersOnDeactivation)
    
    
    MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
    
    UIApplication.shared.endReceivingRemoteControlEvents()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    相关资源
    最近更新 更多