【问题标题】:Control audio on lock screen using AVAudioPlayer使用 AVAudioPlayer 控制锁定屏幕上的音频
【发布时间】:2017-12-19 15:55:20
【问题描述】:

我为音频启用了后台模式,并且我使用此方法播放文件,但是当屏幕锁定时我无法控制它们。

func loadSound(resource: String, soundExtension: String) {
    guard let url = Bundle.main.url(forResource: resource, withExtension: soundExtension) else {
        return
    }

    UIApplication.shared.beginReceivingRemoteControlEvents()

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true)

        player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)
    } catch let error {
        print(error.localizedDescription)
    }
}

【问题讨论】:

    标签: ios swift avaudioplayer


    【解决方案1】:

    遇到同样问题的人,在问题方法中调用这个方法

    fileprivate let remoteCommandCenter = MPRemoteCommandCenter.shared()
    
    func activateControlCenterCommands() {
    
        remoteCommandCenter.playCommand.addTarget(self, action: #selector(handlePlayCommandEvent(_:)))
        remoteCommandCenter.pauseCommand.addTarget(self, action: #selector(handlePauseCommandEvent(_:)))
    
        remoteCommandCenter.playCommand.isEnabled = true
        remoteCommandCenter.pauseCommand.isEnabled = true
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-20
      • 2016-04-13
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多