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
    }

}

希望信息足够。谢谢

4

1 回答 1

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
       }



    }
于 2019-12-13T09:35:02.887 回答