使用iOS锁屏上的播放器控件进行远程控制应用
我正在尝试在iOS锁定屏幕上实现播放器控制,以供我的远程控制应用程序控制PC上的音乐播放器。 所以基本上我需要在iOS设备之外控制播放。 我已经在功能(音频)中启用了后台模式,并尝试设置MPRemoteCommandCenter锁屏控件,但它们没有显示出来。以下是代码:使用iOS锁屏上的播放器控件进行远程控制应用,ios,swift,avplayer,avaudiosession,mpremotecommandcenter,Ios,Swift,Avplayer,Avaudiosession,Mpremotecommandcenter,我正在尝试在iOS锁定屏幕上实现播放器控制,以供我的远程控制应用程序控制PC上的音乐播放器。 所以基本上我需要在iOS设备之外控制播放。 我已经在功能(音频)中启用了后台模式,并尝试设置MPRemoteCommandCenter锁屏控件,但它们没有显示出来。以下是代码: class RemoteCommandHandler: NSObject { static let shared = RemoteCommandHandler() private let player = AV
class RemoteCommandHandler: NSObject {
static let shared = RemoteCommandHandler()
private let player = AVPlayer()
private let audioSession = AVAudioSession.sharedInstance()
private let commandCenter = MPRemoteCommandCenter.shared()
func initRemote() {
//UIApplication.shared.beginReceivingRemoteControlEvents()
do {
if #available(iOS 10.0, *) {
try audioSession.setCategory(.playback, mode: .default, options: [])
} else {
// Fallback on earlier versions
try audioSession.setCategory(.playback)
}
} catch {
NSLog("\(error)")
}
do {
try audioSession.setActive(true)
NSLog("AVSession is active")
} catch {
NSLog("\(error)")
}
setupRemoteTransportControls()
setupNowPlaying()
}
func setupRemoteTransportControls() {
commandCenter.togglePlayPauseCommand.isEnabled = true
commandCenter.togglePlayPauseCommand.addTarget(self, action: #selector(controlPlayPause))
}
func setupNowPlaying() {
var nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = NowPlayingInfo.getValue(.Title)
if let image = UIImage(named: "DemoArtwork") {
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: image)
}
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = TimeInterval(exactly: 10)
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = NowPlayingInfo.getDurationMs() / 1000
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 1.0
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
}
甚至可以做我想做的事吗?我做错了什么?看起来在锁屏上设置控件的唯一可能方法是启动应用程序中真实音频文件的播放。 我在应用程序启动时做了一个很短的无声文件播放的解决方案,现在我可以访问这些控件了 请记住,应启用背景模式(音频),以便在应用程序不在屏幕上时查看控件 另一个限制是,音量控制是硬连接到设备音量的,所以您不能单独更改应用程序音量 以下是启用控件的最低代码:
import MediaPlayer
import AVFoundation
class RemoteCommandHandler: NSObject {
static let shared = RemoteCommandHandler()
private var player: AVPlayer!
private var playerItem: AVPlayerItem!
private let audioSession = AVAudioSession.sharedInstance()
private let commandCenter = MPRemoteCommandCenter.shared()
func initRemote() {
let sound = Bundle.main.path(forResource: "SilenceAAC", ofType: "m4a")
playerItem = AVPlayerItem(url: URL(fileURLWithPath: sound!))
player = AVPlayer(playerItem: playerItem)
do {
if #available(iOS 10.0, *) {
try audioSession.setCategory(.playback, mode: .default, options: [])
} else {
// Fallback on earlier versions
try audioSession.setCategory(.playback)
}
try audioSession.setActive(true)
NSLog("AVSession is active - \(audioSession)")
} catch {
NSLog("\(error)")
}
setupNowPlaying()
setupRemoteTransportControls()
player.play()
}
func setupRemoteTransportControls() {
commandCenter.togglePlayPauseCommand.addTarget(self, action: #selector(controlPlayPause))
}
func setupNowPlaying() {
var nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = NowPlayingInfo.getValue(.Title)
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
}