MPRemoteCommandCenter在iOS13上没有出现

问题描述 投票:0回答:1

我有一个视频播放器,显示.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
    }

}

希望信息足够。谢谢

swift xcode avplayer mpremotecommandcenter ios13.2
1个回答
0
投票

远程控制在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
       }



    }
© www.soinside.com 2019 - 2024. All rights reserved.