AVPlayerLayer Position in UIView Layer - ios

I am writing an application using Swift to view surveillance cameras via HLS. I have the basic device list working and I am able to segue to the Live view and display the stream however, I need to move the AVPlayerLayer and I am having trouble figuring this out. Here is my current code:
let player = AVPlayer(URL: url!)
let playerLayer = AVPlayerLayer(player: player)
let view = UIView(frame: CGRectMake(0, 0, screenSize.width, screenSize.height))
self.view.layer.borderWidth = 1
self.view.layer.borderColor = UIColor(red:222/255.0, green:225/255.0, blue:227/255.0, alpha: 1.0).CGColor
self.view.layer.addSublayer(playerLayer)
playerLayer.frame = view.bounds
player.play()
I want the AVPlayerLayer to be placed 50 points below the top because I have a header for each view scene.
Thank you!

So it just so happens that you cannot place a sub layer in a view in a specific position. Here is the solution:
let player = AVPlayer(URL: url!)
let playerController = AVPlayerViewController()
playerController.player = player
playerController.view.frame = CGRectMake(0, 50, screenSize.width, 240)
self.view.addSubview(playerController.view)
self.addChildViewController(playerController)
player.play()
I had to set a specific height of the player and then the position.

I don't see where you associate the PlayerLayer with a View and/or add it as a subview/layer to anything? You will need to add it as a sublayer.
let player = AVPlayer(URL: url!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRectMake(0,50,screenSize.width, screenSize.height)
self.view.layer.addSublayer(playerLayer)
player.play()
I moved the Y position by 50 as you suggested you wanted to do in your question.

Related

Add AVPlayer subview to UIView

I am using the following code to create a vertical scrollview with paging enabled which works perfectly fine. Now I would like to add a subview of the type AVPlayer (?) to homeView, view2, view3. I can put setupCustomPlayer() in the initiateScrollView() function which works very well, too. However, it is not attached to any of the previously-mentioned views. If I try to add a subview using the function addSubview(anything) it tells me a UIView is expected. I need the UIViews as the vertical sliding feed is very important. Does someone know how to add it as a subview? The video is going to cover the entire screen and going to serve as kind of a background video one could say (if you would like to know)
Code:
func setupCustomPlayer() {
print("check")
AVPlayerVC.view.frame = self.view.frame
AVPlayerVC.view.sizeToFit()
AVPlayerVC.showsPlaybackControls = false
self.view.addSubview(AVPlayerVC.view)
let videoURL = NSURL(string: "http://URL")
let player = AVPlayer(url: videoURL! as URL)
AVPlayerVC.player = player
AVPlayerVC.player?.play()
}
func initiateScrollView() {
//create scrollView with paging enabled
let scrollView = UIScrollView(frame: view.bounds)
scrollView.isPagingEnabled = true
view.addSubview(scrollView)
//do not show vertical scroll indicator
scrollView.showsVerticalScrollIndicator = false;
//get page size
let pageSize = view.bounds.size
//individual views
let homeView = UIView()
homeView.backgroundColor = .green
let view2 = UIView()
view2.backgroundColor = .blue
let view3 = UIView()
view3.backgroundColor = .red
//array with individual views
let pagesViews = [homeView, view2, view3]
//amount of views
let numberOfPages = pagesViews.count
//add subviews (pages)
for (pageIndex, page) in pagesViews.enumerated(){
page.frame = CGRect(origin: CGPoint(x:0 , y:CGFloat(pageIndex) * pageSize.height), size: pageSize)
scrollView.addSubview(page)
}
//define size of scrollView
scrollView.contentSize = CGSize(width: pageSize.width, height: pageSize.height * CGFloat(numberOfPages))
}
Could you try this?
func setupCustomPlayer() {
print("check")
let player = AVPlayer(url: yourFileUrl)
var playerLayer: AVPlayerLayer?
playerLayer = AVPlayerLayer(player: player)
playerLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
playerLayer!.frame = self.view.frame
self.view.layer.addSublayer(playerLayer!)
player.play()
}

how to show video in a small frame in ios

I want to show a video of a topic in top half of the view and its matter in textview in the bottom half of the view. For that video control i want to have the features like play,pause,stop,ff etc. Also i want to play it from local resource as my web services hasn't been setup yet. pls suggest a good solution
I have tried UIWebView and added constraints to webview and textview but for some reason the web view is not showing the video correctly. below is my code
let purl = NSURL(fileURLWithPath: "/Users/Rohit/Desktop/videos/demo/demo/video1.mp4") webView.loadHTMLString("<iframe width = \" \(webView.frame.width) \" height = \"\(webView.frame.height)\" src = \"\(purl)\"></iframe>", baseURL: nil)
webView.backgroundColor = UIColor.green
webView.mediaPlaybackRequiresUserAction = true
webView.scrollView.isScrollEnabled = true
webView.isUserInteractionEnabled = true
Import AVFoundation and AVKit
Then play the video using an URL object (in Swift 3 NSURL is renamed to URL)
let player = AVPlayer(URL: URI)
let controller = AVPlayerViewController()
controller.player = player
self.addChildViewController(controller)
let screenSize = UIScreen.main.bounds.size
let videoFrame = CGRect(x: 0, y: 10, width: screenSize.width, height: (screenSize.height - 10) * 0.5)
controller.view.frame = videoFrame
self.view.addSubview(controller.view)
player.play()
You can use AVPlayerLayer and give it bounds.
private func inits() {
//let rootLayer: CALayer = self.layer
// rootLayer.masksToBounds = true
avPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.bounds = self.bounds
// avPlayerLayer.backgroundColor = UIColor.yellowColor().CGColor
self.layer.insertSublayer(avPlayerLayer, atIndex: 0)
}

How to have video be ready to play in swift

The following code has a video being played. But the video starts immediately when the view controller is launch. When the view controller is launch I would like the user to hit the play button for the video to play. Now the video plays as soon as the view controller is launched.
let path = NSBundle.mainBundle().pathForResource("x", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
self.moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = self.moviePlayer {
player.view.frame = CGRect(x: 55, y: 75, width: self.view.frame.size.width/2, height: self.view.frame.size.height / 5)
player.view.sizeToFit()
player.scalingMode = MPMovieScalingMode.AspectFit
player.fullscreen = false
player.controlStyle = MPMovieControlStyle.Default
player.movieSourceType = MPMovieSourceType.File
player.repeatMode = MPMovieRepeatMode.One
player.play()
self.view.addSubview(player.view)

AVPlayer too big for screen

I'm trying to play a video in a scrollView. For that matter, I ended up using an AVPlayerViewController because it worked really well with the spacing between multiple videos. The problem is however, that all the videos are approx. 1/3 bigger than the size of the screen. What did I do wrong?
let player = AVPlayer(URL: NSURL(string: videoLink))
let playerController = AVPlayerViewController()
playerController.player = player
playerController.showsPlaybackControls = false
playerController.videoGravity = AVLayerVideoGravityResizeAspect
playerController.view.frame = CGRectMake(0, (UIScreen.mainScreen().bounds.height) * CGFloat(index), UIScreen.mainScreen().bounds.width,0)
UPDATE:
The following code still shows the video. The frame is really small, but still shows. I tried debugging it by printing out its "view.frame.bounds.width", "view.frame.bounds.size.width", "view.frame.size.width", and "view.frame.width" and they all said "0"
playerController.view.frame = CGRectMake(0, (self.view.frame.size.height - 64) * CGFloat(index) + 30, 0, 0)

AVPlayer and black video

I'm recording a video in the first UIViewController, writing this video to file and I want to show this video in the second UIViewController but sometimes (quite often) there is just a black frame instead of video.
player.status.rawValue and player.currentItem?.status.rawValue are 0 every time.
The file is here every time, I can process it, so videoURL is ok. I'm getting no errors, just the black screen.
videoURLAsset = videoAsset as! AVURLAsset
let videoURL = videoURLAsset.URL
if playerLayer != nil {
player = nil
playerLayer.removeFromSuperlayer()
}
player = AVPlayer(URL: videoURL)
player.volume = 0.0
playerLayer = AVPlayerLayer(player: player)
playerLayer.backgroundColor = UIColor.blackColor().CGColor
playerLayer.frame = CGRectMake(10, 70, screenWidth - 20, screenHeight / 2 - 90)
self.view.layer.addSublayer(playerLayer)
player.play()
What is my mistake?

Resources