幸いにも、私は自分で答えを見つけることができました。 UIScreenはiOSデバイスと同様にtvOSと同様に動作します。私の完全な解決策は以下の通りです。私は終わりを引き起こす観察者も含めています。
、私はいくつかの明快
を追加するためにラベルを追加しました。これは、あなたが境界に
let screenBounds = UIScreen.main.bounds
let centerX = screenBounds.size.width/2
let centerY = screenBounds.size.height/2
let loadingLabelCenterY = centerY + 48
let activityIndicatorCenter = CGPoint(x: centerX, y: centerY)
loadingActivityIndicator.center = activityIndicatorCenter
を取得するためにUIScreenを使用する部分である
class VideoLoadingActivityIndicator {
var loadingActivityIndicator:UIActivityIndicatorView!
var loadingLabel: UILabel!
func beginLoadingActivity(viewController:UIViewController)
{
loadingActivityIndicator = UIActivityIndicatorView(frame:CGRect(x: 500, y: 500, width: 500, height: 500)) as UIActivityIndicatorView
loadingActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
loadingActivityIndicator.sizeToFit()
loadingActivityIndicator.color = UIColor.white
活動を処理するために使用するクラス
loadingLabel = UILabel(frame: CGRect(x: centerX, y: loadingLabelCenterY, width: 600, height: 100))
loadingLabel.font = UIFont(name: "Helvetica", size: 30)
loadingLabel.text = "Loading Video"
loadingLabel.numberOfLines = 1
loadingLabel.sizeToFit()
loadingLabel.textAlignment = .center
// reset frame so it is centered in screen
let labelWidth = loadingLabel.frame.width
let labelHeight = loadingLabel.frame.height
let loadingLabelCenterX = centerX - (labelWidth/2)
loadingLabel.frame = CGRect(x: loadingLabelCenterX, y: loadingLabelCenterY, width: labelWidth, height: labelHeight)
loadingLabel.textColor = UIColor.white
// Add indicator and label to subview
viewController.view.addSubview(loadingActivityIndicator)
viewController.view.addSubview(loadingLabel)
self.loadingActivityIndicator.startAnimating()
}
func endLoadingActivity(viewController:UIViewController)-> Void
{
loadingActivityIndicator.removeFromSuperview()
loadingLabel.removeFromSuperview()
}
そして、AVPlayerViewControllerにいくつかの読み込みアクティビティを追加したいと考えています。
まず活動の指標オブジェクトの作成:
let indicator = VideoLoadingActivityIndicator()
はあなたの資産を作成し、playerItemを
let asset = AVAsset(url: runFile)
let assetKeys = [
"playable",
"hasProtectedContent"
]
let playerItem = AVPlayerItem(asset: asset, automaticallyLoadedAssetKeys: assetKeys)
活動の指標にアニメーションを開始(私はいくつかのプレーヤーの項目キーが設定)
indicator.beginLoadingActivity(viewController: self, color: .white)
プレイヤーに通知を加えて再生を開始
これは私がやった同じ問題のいくつかに走る他の誰かを助け
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "status" && player?.status == AVPlayerStatus.readyToPlay {
indicator.endLoadingActivity(viewController: self)
}
}
希望:3210
playerItem.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.new, context: nil)
player = AVPlayer(playerItem: playerItem)
player?.play()
最後に、オブザーバーを扱います。