AVPlayer 锁屏控件
AVPlayer Lock Screen Controls
我有一个使用 AVPlayer 在后台播放音频的应用程序。我使用 MPNowPlayingInfoCenter 在锁定屏幕和控制中心上显示歌曲的元数据。一切正常,除了一件事。
锁定屏幕和控制中心上的遥控器是播客应用程序的遥控器。他们没有前进和上一个按钮。
我有一张控件的屏幕截图:
如您所见,我没有前进和上一个按钮。
override func viewDidLoad() {
super.viewDidLoad()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
} catch {
print(error)
}
}
@IBAction func play(sender: AnyObject) {
if isURLAvailable == false {
return
}
if (player!.rate != 0 && player!.error == nil) {
player!.pause()
} else {
player!.play()
}
updateImage()
}
func playSong(song: Song) {
let documentsDirectoryURL = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as NSURL?
let url: NSURL? = documentsDirectoryURL?.URLByAppendingPathComponent(song.fileName)
let avItem = AVPlayerItem(URL: url!)
player = AVPlayer(playerItem: avItem)
player?.play()
let artworkProperty = MPMediaItemArtwork(image: song.artwork)
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = [MPMediaItemPropertyTitle : lblSongName.text!, MPMediaItemPropertyArtist : song.artist, MPMediaItemPropertyArtwork : artworkProperty, MPNowPlayingInfoPropertyDefaultPlaybackRate : NSNumber(int: 1), MPMediaItemPropertyPlaybackDuration : CMTimeGetSeconds((player!.currentItem?.asset.duration)!)]
}
override func remoteControlReceivedWithEvent(event: UIEvent?) {
print(event!.type)
if event!.type == UIEventType.RemoteControl {
if event?.subtype == UIEventSubtype.RemoteControlPlay || event?.subtype == UIEventSubtype.RemoteControlPause {
play(self)
}
if event?.subtype == UIEventSubtype.RemoteControlNextTrack {
next(self)
}
if event?.subtype == UIEventSubtype.RemoteControlPreviousTrack {
previous(self)
}
}
}
与其将 UIEvent
流与 remoteControlReceivedWithEvent
一起使用,我建议您使用 MPRemoteCommandCenter
来控制锁定屏幕和控制中心上的 previous/next/play/pause 操作。
import MediaPlayer
// ...
let commandCenter = MPRemoteCommandCenter.sharedCommandCenter()
commandCenter.previousTrackCommand.enabled = true;
commandCenter.previousTrackCommand.addTarget(self, action: "previousTrack")
commandCenter.nextTrackCommand.enabled = true
commandCenter.nextTrackCommand.addTarget(self, action: "nextTrack")
commandCenter.playCommand.enabled = true
commandCenter.playCommand.addTarget(self, action: "playAudio")
commandCenter.pauseCommand.enabled = true
commandCenter.pauseCommand.addTarget(self, action: "pauseAudio")
其中 previousTrack
、nextTrack
、playAudio
和 pauseAudio
都是 class 中您控制玩家的功能。
您可能还需要明确禁用向前和向后跳过命令:
commandCenter.skipBackwardCommand.enabled = false
commandCenter.skipForwardCommand.enabled = false
我有一个使用 AVPlayer 在后台播放音频的应用程序。我使用 MPNowPlayingInfoCenter 在锁定屏幕和控制中心上显示歌曲的元数据。一切正常,除了一件事。
锁定屏幕和控制中心上的遥控器是播客应用程序的遥控器。他们没有前进和上一个按钮。
我有一张控件的屏幕截图:
如您所见,我没有前进和上一个按钮。
override func viewDidLoad() {
super.viewDidLoad()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
} catch {
print(error)
}
}
@IBAction func play(sender: AnyObject) {
if isURLAvailable == false {
return
}
if (player!.rate != 0 && player!.error == nil) {
player!.pause()
} else {
player!.play()
}
updateImage()
}
func playSong(song: Song) {
let documentsDirectoryURL = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as NSURL?
let url: NSURL? = documentsDirectoryURL?.URLByAppendingPathComponent(song.fileName)
let avItem = AVPlayerItem(URL: url!)
player = AVPlayer(playerItem: avItem)
player?.play()
let artworkProperty = MPMediaItemArtwork(image: song.artwork)
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = [MPMediaItemPropertyTitle : lblSongName.text!, MPMediaItemPropertyArtist : song.artist, MPMediaItemPropertyArtwork : artworkProperty, MPNowPlayingInfoPropertyDefaultPlaybackRate : NSNumber(int: 1), MPMediaItemPropertyPlaybackDuration : CMTimeGetSeconds((player!.currentItem?.asset.duration)!)]
}
override func remoteControlReceivedWithEvent(event: UIEvent?) {
print(event!.type)
if event!.type == UIEventType.RemoteControl {
if event?.subtype == UIEventSubtype.RemoteControlPlay || event?.subtype == UIEventSubtype.RemoteControlPause {
play(self)
}
if event?.subtype == UIEventSubtype.RemoteControlNextTrack {
next(self)
}
if event?.subtype == UIEventSubtype.RemoteControlPreviousTrack {
previous(self)
}
}
}
与其将 UIEvent
流与 remoteControlReceivedWithEvent
一起使用,我建议您使用 MPRemoteCommandCenter
来控制锁定屏幕和控制中心上的 previous/next/play/pause 操作。
import MediaPlayer
// ...
let commandCenter = MPRemoteCommandCenter.sharedCommandCenter()
commandCenter.previousTrackCommand.enabled = true;
commandCenter.previousTrackCommand.addTarget(self, action: "previousTrack")
commandCenter.nextTrackCommand.enabled = true
commandCenter.nextTrackCommand.addTarget(self, action: "nextTrack")
commandCenter.playCommand.enabled = true
commandCenter.playCommand.addTarget(self, action: "playAudio")
commandCenter.pauseCommand.enabled = true
commandCenter.pauseCommand.addTarget(self, action: "pauseAudio")
其中 previousTrack
、nextTrack
、playAudio
和 pauseAudio
都是 class 中您控制玩家的功能。
您可能还需要明确禁用向前和向后跳过命令:
commandCenter.skipBackwardCommand.enabled = false
commandCenter.skipForwardCommand.enabled = false