MPRemoteCommandCenter没有出现在iOS13上

时间:2019-12-13 09:18:27

标签: swift xcode avplayer mpremotecommandcenter ios13.2

我有一个显示.m3u8流的视频播放器。我已经尝试实现远程控制,但是该小部件没有同时出现在锁定屏幕,通知中心和命令中心中。

我只需要具有播放暂停,音量控制以及一些静态信息(例如标题,艺术家和艺术品)。

这是我的代码:

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        print(error.localizedDescription)
    }

    return true
}

ViewController.swift

   class ViewController: UIViewController {

    var player = AVPlayer()
    var playerViewcontroller = AVPlayerViewController()
    var playerItem: AVPlayerItem!
    var playerLayer = AVPlayerLayer()

    @IBAction func playVideo(_ sender: Any) {
        guard let url = URL(string: "https://video.m3u8") else {
            return
        }

        // Create an AVPlayer, passing it the HTTP Live Streaming URL.
        playerItem = AVPlayerItem(url: url)
        playerItem.preferredForwardBufferDuration = 8
        player = AVPlayer(playerItem: playerItem)

        // Create a new AVPlayerViewController and pass it a reference to the player.
        playerViewcontroller.player = player
        playerViewcontroller.showsPlaybackControls = true

        // Modally present the player and call the player's play() method when complete.
        present(playerViewcontroller, animated: true) {
            self.player.play()
        }

        setupNowPlaying()

    }

    override func viewDidLoad() {
        //super.viewDidLoad()
    }

    public func disconnectAVPlayer() {
        playerViewcontroller.player = nil
    }

    public func reconnectAVPlayer() {
        playerViewcontroller.player = player
    }


    func setupNowPlaying() {
        print("_________________________________setupPlaying")
        // Define Now Playing Info
        let nowPlayingInfoCenter = MPNowPlayingInfoCenter.default()
        var nowPlayingInfo = nowPlayingInfoCenter.nowPlayingInfo ?? [String: Any]()

        let title = "TV NAME"
        let album = "TV DESCRIPTION"
        let image = UIImage(named: "ICON") ?? UIImage()
        let artwork = MPMediaItemArtwork(boundsSize: image.size, requestHandler: {  (_) -> UIImage in
          return image
        })

        nowPlayingInfo[MPMediaItemPropertyTitle] = title
        nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = album
        nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
        nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = NSNumber(value: 1.0)
        nowPlayingInfoCenter.nowPlayingInfo = nowPlayingInfo
    }

}

希望信息足够。谢谢

1 个答案:

答案 0 :(得分:1)

远程控制在iOS 13中工作正常,只是您没有首先进行设置。分配给您的AVPlayer后添加此代码

func commandCenterSetup() {

    UIApplication.shared.beginReceivingRemoteControlEvents()
    let commandCenter = MPRemoteCommandCenter.shared()


   setupNowPlaying()

    commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in

        print("PAUSE")
        self.playerViewcontroller.player.pause()
        return .success

       }


       commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in

        print("PLAY")
        playerViewcontroller.player.play()
        return .success
       }



    }