简体   繁体   English

使用AVPlayerViewController不会立即发生Segue

[英]Segue not happening immediately with AVPlayerViewController

I am making a tvOS app. 我正在制作一个tvOS应用程序。 I am creating a simple SHOW segue. 我正在创建一个简单的SHOW segue。 In the destination view controller i want to play a remote video with AVPlayerViewController , but when when i press the button to make the segue, its not happening immediately. 在目标视图控制器中,我想使用AVPlayerViewController播放远程视频,但是当我按下按钮进行选择时,它不会立即发生。 In fact, it waits for almost 2 to 3 seconds and then goes to the destination View Controller, where the video starts almost immediately. 实际上,它会等待将近2到3秒钟,然后转到目标View Controller,在那里视频几乎立即开始播放。 I think the segue is not happening immediately because AVPlayer is loading the video. 我认为这不是立即发生,因为AVPlayer正在加载视频。 I don't want this behaviour, what i want to have is to make the transition immediately and then may be show a activity indicator (spinning view) at destination View Controller. 我不希望这种行为,我想要立即进行转换,然后在目标View Controller上显示活动指示器(旋转视图)。

Here is my code for the destination View Controller. 这是我的目标View Controller代码。

import Foundation
import UIKit
import AVKit


class PlayViewController : UIViewController {


    var avplayerVC : AVPlayerViewController?
    var videoUrlStr : String?


    override func viewDidLoad() {
        super.viewDidLoad()

        print("In PlayViewController View Did Load")

        avplayerVC = AVPlayerViewController()

        let avAsset = AVAsset(URL: NSURL.init(string: videoUrlStr!)!)
        let avPlayerItem = AVPlayerItem(asset: avAsset)
        avplayerVC?.player = AVPlayer(playerItem: avPlayerItem)

        avplayerVC?.player?.seekToTime(kCMTimeZero)
        avplayerVC?.player?.play()
        avplayerVC?.view.frame = (self.view?.frame)!
        self.view.addSubview((avplayerVC?.view!)!)

    }



    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        avplayerVC?.view.removeFromSuperview()
    }


    // MARK : AVPlayerViewControllerDelegate


}

Can anybody help me in achieving this behaviour? 有人可以帮助我实现这一目标吗? Thanks 谢谢

Take a look at asset.loadValuesAsynchronouslyForKeys(keys: [String], completionHandler:) This method will give you the ability to do some of the loading and then setup your player in the completion handler. 看一下asset.loadValuesAsynchronouslyForKeys(keys: [String], completionHandler:)这个方法将使您能够执行一些加载,然后在完成处理程序中设置播放器。 You can pass any keys to this method, but some you might consider are duration, tracks, and playable. 您可以将任何键传递给该方法,但是您可能会考虑的一些是持续时间,曲目和可播放的。

The only other caveat I can think of is that after you have loaded these values you will need to dispatch your player interactions to the main thread. 我能想到的唯一的其他警告是,在加载这些值之后,您将需要将玩家的交互分配到主线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM