ios - iPhone 上不显示音频播放锁屏控件

标签 ios swift audio media-player

我正在我的实际 iPhone 6s 设备上使用 iOS 10.2 对此进行测试。

我正在播放流式音频,并且能够播放/暂停音频、跳过轨道等。我还启用了后台模式,音频在后台播放并正确地继续播放列表。我遇到的唯一问题是让锁屏控件显示出来。什么都不显示...

在我的 MainViewController 的 viewDidLoad() 中,就在我的应用程序启动时,我称之为...

 func setupAudioSession(){

   UIApplication.shared.beginReceivingRemoteControlEvents()


   do {
       try    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.mixWithOthers)

       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)
   }
}

然后在我的 AudioPlayer 类中开始播放音频后,我调用 ...

func setupLockScreen(){
       let commandCenter = MPRemoteCommandCenter.shared()
       commandCenter.nextTrackCommand.isEnabled = true
       commandCenter.nextTrackCommand.addTarget(self, action:#selector(skipTrack))
       MPNowPlayingInfoCenter.default().nowPlayingInfo = [MPMediaItemPropertyTitle: "TESTING"]
}

当我锁定我的 iPhone,然后再次点击电源按钮进入锁定屏幕时,音频控件根本不显示。就好像没有播放音频,我只看到我的正常背景照片。控制面板中也没有显示控件(在主屏幕上向上滑动,然后向左滑动到音乐控件应位于的位置)。

问题是因为我没有使用 AVAudioPlayer 或 AVPlayer 吗?但是,例如,Spotify 如何使用他们自己的自定义音频播放器来显示锁屏控件?感谢您的任何建议/帮助

最佳答案

问题原来是这一行...

try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.duckOthers)

一旦我把它改成了

try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])

一切正常。因此,似乎为 AVAudioSessionCategoryPlaybackOptions 传递任何参数都会导致锁屏控件不显示。我还尝试传入 .mixWithOthers ,这也导致锁屏控件无法显示

关于ios - iPhone 上不显示音频播放锁屏控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42656940/

相关文章:

ios - Xcode 4 键盘快捷键切换构建目标目的地

ios - 如何更改 UILabel swift 中文本的行距

ios - iTunes Connect 自动生成应用程序 ID

c - Audio API 适用于windows多声道播放

ios - MKNetworkKit 和 AFNetworking 有什么区别?

ios - 如何在不自定义的情况下在一个单元格中有多行?

swift - Safari 扩展无法执行 URLSession 任务,错误为 : failed path:/var/run/mDNSResponder Operation not permitted

ios - 无法从 IPv6 连接到 IPv4 错误域=NSURLErrorDomain 代码=-1003“找不到具有指定主机名的服务器

jquery - jQuery声音通知有关新文件的内容

java - 使用混音器混合两个音频文件