Search code examples
iosobjective-cxcodeios13

Cannot get AVPlayerLayer on iOS13.x by Objective-C


I'm making an iOS app with objective-c.
Please tell me how to fix AVPlayerView in iOS13.x environment.

ViewController.m write

AVPlayerLayer* layer = (AVPlayerLayer*)self.videoView.layer;

VideoView contains AVPlayerView

AVPlayerView.m write

#import "AVPlayerView.h"
#import <AVFoundation/AVFoundation.h>

@implementation AVPlayerView

+(Class)layerClass{
    return AVPlayerLayer.class;
}

@end

The layerClass method is not called for some reason.
Also, the class returned by (AVPlayerLayer *) self.videoView.layer
AVPresentationVontainerViewLayer.
I want an AVPlayerView class.
How should i fix it?


Solution

  • Try this:

        NSURL *url = [NSURL fileURLWithPath:path];
        AVPlayer *player = [AVPlayer playerWithURL:url];
        AVPlayerViewController *AVPlayerVc = [[AVPlayerViewController alloc] init];
        AVPlayerVc.player = player;
        if (AVPlayerVc) {
            [self presentViewController:AVPlayerVc animated:YES completion:^{
                [player play];
            }];
        }
    

    Swift Version:

     let player = AVPlayer(url: YOUR_URL)
     let playerController = AVPlayerViewController()
     playerController.player = player
     present(playerController, animated: true) {
             player.play()
     }