Reputation: 34780
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?
Upvotes: 1
Views: 941
Reputation: 27428
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];
Upvotes: 2
Reputation: 34780
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.
Upvotes: 1
Reputation: 1589
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()
}
}
Upvotes: -1