AVPlayer锁屏控件

10

我有一个使用AVPlayer在后台播放音频的应用程序。我使用MPNowPlayingInfoCenter在锁屏和控制中心上显示歌曲的元数据。除了一件事以外,一切都运行良好。

锁屏和控制中心上的远程控件是播客应用程序的控件。它们没有“前进”和“后退”按钮。

我有一张控件的截图:

enter image description here

正如您所看到的,我没有“前进”和“后退”按钮。

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

能够看到您目前已经实现的代码将会非常有用。 - Ares
其实,这些是有声书的控件,不是播客,是吗? - matt
控制锁屏界面可以操作你的应用程序的代码在哪里?你展示的所有代码都不会实现这个功能。因此,我猜测其中的按钮是为其他应用程序(如iBooks)设计的。 - matt
@matt 看看更新后的问题。 - krypton36
不好意思,我没有看到任何相关的代码。 - matt
显示剩余2条评论
1个回答

14
与其使用 remoteControlReceivedWithEvent 中的 UIEvent 流,我建议您使用 MPRemoteCommandCenter 在锁定屏幕和控制中心上控制上一个/下一个/播放/暂停操作。
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")

previousTracknextTrackplayAudiopauseAudio都是您控制播放器的类中的函数。

您可能还需要显式禁用向前和向后跳过命令:

commandCenter.skipBackwardCommand.enabled = false
commandCenter.skipForwardCommand.enabled = false

2
优雅但是否相关? - matt
很高兴你回答了,虽然问题仍然存在。也许是操作系统的 bug? - krypton36
如果我没记错的话,从苹果文档中可以得知,MPRemoteCommandCenter是处理此类事件的更好方式,而不是使用remoteControlReceivedWithEvent(https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Remote-ControlEvents/Remote-ControlEvents.html)。如果我错了,请随时纠正我并提供您自己的替代答案。 - JAL
1
@JAL 不,我同意你的观点!但我仍然认为你的建议有效的原因是他从未将自己设为第一响应者,因此锁屏/控制中心与他的应用实际上没有任何连接。 MPRemoteCommandCenter 的注册提供了一种可靠的一步设置相同连接的方法。所以当他切换到它时,事情开始正常工作。 - matt
有一个打字错误:commandCenter.playCommand.enabled = true commandCenter.previousTrackCommand.addTarget(self, action: "playAudio") 应该改为 playCommand.addTarget - Alan Scarpa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接