AVPlayer锁屏控制 [英] AVPlayer Lock Screen Controls
问题描述
我有一个使用AVPlayer在后台播放音频的应用。我使用MPNowPlayingInfoCenter在锁定屏幕和控制中心上显示歌曲的元数据。除了一件事,一切正常。
I have an app that plays audio on the background using AVPlayer. I use MPNowPlayingInfoCenter to display the song's metadata on the Lock Screen and Control Center. Everything works fine except for one thing.
锁屏和控制中心的遥控器是播客应用程序的遥控器。他们没有前进和上一个按钮。
The remote controls on the Lock Screen and Control Center are those of a podcast app. They don't have forward and previous buttons.
我有一个控件截图的截图:
I have a screenshot of how the controls are:
如您所见,我没有前进和上一个按钮。
As you can see, I don't have forward and previous buttons.
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
来控制锁定屏幕和控制中心上的上一个/下一个/播放/暂停操作。
Rather than using the UIEvent
stream with remoteControlReceivedWithEvent
, I would recommend you use MPRemoteCommandCenter
to control the previous/next/play/pause actions on the lock screen and control center.
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
是您班级中控制播放器的所有功能。
Where previousTrack
, nextTrack
, playAudio
, and pauseAudio
are all functions in your class where you control the player.
您可能还需要明确禁用跳过前进和后退命令:
You may need to explicitly disable the skip forward and backward commands as well:
commandCenter.skipBackwardCommand.enabled = false
commandCenter.skipForwardCommand.enabled = false
这篇关于AVPlayer锁屏控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!