简体   繁体   中英

AVPlayer with AVPlayerLayer not playing video

I've got a video file named Intro.m4v which has a target membership on my app target (and the video itself is straight from iPhone so I'm pretty sure it's iPhone-supported).

I'm trying to play the video as follows ( self.videoContainer is a fullscreen view inside my view controller, and double checked, it's not nil . Also, the player and player layer are strongly referenced instance variables inside my view controller class):

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:@"Intro.m4v"]];
    playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    playerLayer.frame = self.videoContainer.layer.frame;
    [self.videoContainer.layer addSublayer:playerLayer];

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [player play];
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    playerLayer.frame = self.videoContainer.layer.frame;
}

However, nothing is playing. All I'm seeing is the background color of my video container view (and all the views in front of it, in that manner). What am I doing wrong?

Your URL should be like this if your video is in a bundle:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Intro" ofType:@"m4v"];
NSURL *yourVideoUrl = [NSURL fileURLWithPath:path];

Oops found the problem myself. I was using [NSURL fileURLWithPath:@"Intro.m4v"] whereas I had to use [NSBundle mainBundle] URLForResource:@"Intro" withExtension:@"m4v"] .

Fixing the URL resolved the issue.

Try This

func playVideo(urlString: String, location : Int) {        
        let videoURL = URL(string: urlString)
        let player = AVPlayer(url: videoURL!)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.present(playerViewController, animated: true) {
            playerViewController.player!.play()
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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