How to use AVPlayer with Swift

Asked 2 years ago, Updated 2 years ago, 29 views

 classAVPlayerView: UIView {//...(1) Create AVPlayerView by inheriting class from UIView
    required init (coder adecoder:NSCoder) {
        super.init(coder:aDecoder)
    }
    override init (frame:CGRect) {
        super.init (frame:frame)
    }
    override class funclayerClass() - > AnyClass {
        return AVPlayerLayer.self// AV Return only the AVPlayerLayer type itself
    }
}

 override func viewDidLoad(){
        // Generate an asset from the path.
        let path = NSBundle.mainBundle().pathForResource("test", ofType:"mov")
        let fileURL = NSURL (fileURLWithPath:path!)
        let avAsset = AVURLAsset (URL: fileURL, options:nil)
        // Generate items for AVPlayer to play.
        playerItem=AVPlayerItem(asset:avAsset)
        // Generate AVPlayer.
        videoPlayer=AVPlayer(playerItem:playerItem)
        // Generate View.
        let videoPlayerView = AVPlayerView (frame:self.view.bounds)
          // vInstantize AVPlayerView with the same size as the view and make it a videoPlayerView
        // Make the layer of the UIView AVPlayerLayer.
        letlayer=videoPlayerView.layer as!AVPlayerLayer
          // v Forced downcast videoPlayerView.layer to AVPlayerLayer?
        layer.videoGravity=AVLayerVideoGravityResizeAspect
        layer.player=videoPlayer
        // add layers
        self.view.layer.addSublayer(layer)
・・・

Some of the code for video playback using AVPlayer on swift is incomprehensible.

in the annotation section AVReturn only the AVPlayerLayer type
But what does override class funclayerClass() do?If I use ".layer", should I return only the type of class?

4 Force downcast videoPlayerView.layer to AVPlayerLayer?
AVPlayerLayer is
NSObject
|- CALayer |- AVPlayerLayer
VideoPayerView is an instance that inherits the UIView, although it is hierarchical like .
NSObject
|- UIResponder |- UIView
What are they doing (what they get from forcing downcasting)?

I feel that the understanding is wrong in the first place.Please let me know the explanation and URL that will give me a hint of understanding.
What I don't understand is
(1) Relationship between UIView and AVPlayerLayer
(2) I don't think layerClass(), .layer understanding is correct.
(3) (4) Reasons for forced downcasting (effect)
That's it.

There are many things I don't understand, so I don't know how to ask questions, but I appreciate your cooperation.

swift

2022-09-29 21:32

1 Answers

UIView has an instance of CALayer internally and exposes it through the layer property.

When UIView creates this instance, it calls layerClass() to determine exactly which class instance to create and creates an instance of the returned class.Now that you have returned AVPlayerLayer.self, the instance that is created is an instance of the AVPlayerLayer class.

Downcasting videoPlayerView.layer to AVPlayerLayer is acceptable and is the only way to retrieve the instance because UIView ensures that the instance is an instance of AVPlayerLayer.


2022-09-29 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.