Reputation: 109
So I'm working on a player using AVFoundation, I can get it to play, but it won't show on the view. Can anyone tell me what I'm missing to get it to show on the view?
Here's my code:
self.playerUrl = NSURL(string: playerStr)!
self.player = AVPlayer.playerWithURL(self.playerUrl)! as! AVPlayer
self.playerLayer.frame = CGRectMake(0, 0, 200, 200)
self.playerLayer = AVPlayerLayer(layer: self)
self.playerLayer = AVPlayerLayer(player: player)
self.view.layer.addSublayer(playerLayer)
self.player.play()
Upvotes: 1
Views: 926
Reputation: 71854
Here is your working code:
let videoURL = NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(URL: videoURL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
In your code you are making mistake in this line:
self.player = AVPlayer.playerWithURL(self.playerUrl)! as! AVPlayer
Just change it with this:
self.player = AVPlayer(URL: playerUrl)
And it will work fine.
Upvotes: 1
Reputation: 263
I have code from a previous app that uses MPMoviePlayerController, which is outdated (they suggest you use AVPlayer), that I know works. You can try it.
let url:NSURL = NSURL(string: playerStr)!
self.moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = self.moviePlayer {
player.view.frame = CGRect(x: 0, y: 50, width: self.view.frame.size.width, height: self.view.frame.size.height/2 + 50)
player.view.sizeToFit()
player.scalingMode = MPMovieScalingMode.None
player.movieSourceType = MPMovieSourceType.File
player.repeatMode = MPMovieRepeatMode.One;
self.view.addSubview(player.view)
player.play()
}
Upvotes: 0