![](/img/trans.png)
[英]Use player controls on iOS lock screen for Remote Control application
[英]Can't show player controls on iOS lock screen
我在鎖定屏幕(iOS 11.2)上顯示播放器控件時遇到問題。 我已經完成的工作如下:
在AppDelegate.swift中,我初始化了AVAudioSession:
import MediaPlayer
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.beginReceivingRemoteControlEvents()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
}
return true
}
}
然后,我的應用程序由一個具有多個視圖的TabHostController組成。 其中之一是音頻選項卡。 在那里,我嘗試處理遠程按鈕:
import UIKit
import Foundation
import MediaPlayer
class AudioTabViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
self.audioPlayClicked()
return .success
}
commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
self.audioPauseClicked()
return .success
}
commandCenter.stopCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
self.audioStopClicked()
return .success
}
}
}
最后,我在Info.plist中添加了背景模式:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
怎么了? 我可以毫無問題地構建項目,然后在iPhone 8上運行它。我的音樂可以播放。
但是鎖定屏幕上沒有音頻控件(屏幕為空)。
而且,如果我查看iOS控制中心,則會找到一些音頻控件,但是通過單擊其中的“播放”按鈕,只會播放我以前使用過的Apple音樂應用程序的音軌。
因此,我認為我的應用未在系統范圍內“注冊”為當前的音頻播放器。
我不知道您能解決問題,但也許有人遇到問題,這篇文章可以幫助他們。
我有同樣的問題,
我終於找到了問題,
當我設置AVAudioSession時,我正在使用此行-> try
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.duckOthers)
一旦我將該行更改為以下內容,鎖定屏幕控件就會顯示並正常工作->
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])
所以我想問題出在傳入AVAudioSessionCategoryPlaybackOptions的參數。 我嘗試傳遞.duckOthers和.mixWithOthers,在兩種情況下,它都導致鎖定屏幕控件根本不顯示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.