iOS Swift Avplayer Inside Uiview How to Make It Work

IOS swift avplayer inside UiView how can I make it work

You are getting the error message because you are trying to present the same instance of your CustomAVPlayerC that you just added as a child view controller. You don't need to present the VC, try this code which is a little cleaner approach:

@IBOutlet weak var newView: UIView!
let avPlayerViewController = CustomAVPlayerC()
var playerView: AVPlayer?

override func viewDidLoad() {
super.viewDidLoad()

guard let movieURL = URL(string: "my url")
else { return }

playerView = AVPlayer(url: movieURL)
avPlayerViewController.player = playerView
avPlayerViewController.view.frame = newView.bounds
self.addChildViewController(avPlayerViewController)
newView.addSubview(avPlayerViewController.view)
avPlayerViewController.didMove(toParentViewController: self)
}

I would recommend going over Apple's documentation on "Implementing a Container View Controller" for best practices in dealing with a container view set up like this.

Embedding an AVPlayerViewController into a UIView

Change this line:

smallVideoPlayerViewController.view.frame = videoView.frame

to this:

smallVideoPlayerViewController.view.frame = videoView.bounds

The reason is because videoView.frame includes the origin in its superview which you don't want. videoView.bounds is just the size with an origin of 0,0.

Of course you might want to go further set the auto-resizing mask to keep the video player frame the same like this:

smallVideoPlayerViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

or even to go full blown auto-layout.

how to play a video in UIView swift?

the only way to add a video to a UIView with fixed constraints in storyboard, was this :

let url = URL(string:myURL)

player = AVPlayer(url: url!)

avpController.player = player

avpController.view.frame.size.height = videoView.frame.size.height

avpController.view.frame.size.width = videoView.frame.size.width

self.videoView.addSubview(avpController.view)

I hope other can use this! :)

Place AVPlayerViewController inside UIView

You can initialize the AVPlayerViewController and add it as a child to your ViewController , and insert it as a subview.

[self addChildViewController:yourPlayerViewController];
[self.view addSubview:yourPlayerViewController.view];
[yourPlayerViewController didMoveToParentViewController:self];

And to remove it:

[yourPlayerViewController willMoveToParentViewController:nil];
[yourPlayerViewController.view removeFromSuperview];
[yourPlayerViewController removeFromParentViewController];

EDIT SWIFT METHOD:
Check here for Swift

AVPlayer on top of UIView

yes, basically all you have to do is call self.view.addSubview(AVPlayerLayer), and this is a layer for the APPlayer; after you instantiate the cell view, then make sure that AVPlayer.frame = view.bounds

AVPlayer view in UIView is missing controls

Try setting showsPlaybackControls and requiresLinearPlayback to true,

playerViewController.showsPlaybackControls = YES;
playerViewController.requiresLinearPlayback = YES;

Currently, you didn't add playerViewController as child viewController so you will not be able to see anything. Add as below,

[self addChildViewController:playerViewController];
[self.view.layer addSublayer:playerLayer];
[self.view addSubview:playerViewController.view];

How to embed a local video into a UIView in ios 12

You need to add the AVPlayerViewController as a child view controller so something like this:

import UIKit
import AVFoundation
import AVKit

class ViewController: UIViewController {

var avPlayer: AVPlayer!

override func viewDidLoad() {

super.viewDidLoad()
let filepath: String? = Bundle.main.path(forResource: "qidong", ofType: "mp4")
let fileURL = URL.init(fileURLWithPath: filepath!)

avPlayer = AVPlayer(url: fileURL)

let avPlayerController = AVPlayerViewController()
avPlayerController.player = avPlayer
avPlayerController.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)

avPlayerController.showsPlaybackControls = false

avPlayerController.player?.play()
self.addChild(avPlayerController)
self.view.addSubview(avPlayerController.view)
avPlayerController.didMove(toParent: self)
}

If you are not doing that then the AVPlayerViewController is probably getting released early.

Another minor issue but don't use the size of the UIScreen but the frame of the view controllers view instead (or even better do something like use auto layout).

AVPlayer inside UITableViewCell with other Views auto layout

You need to tell autolayout how the playerView height will be calculated.
You can try the following options:

  1. set a constant height to playerView

    playerView.heightAnchor.constraint(equalToConstant: 100)
  2. or set a aspect ratio to playerView

    playerView.heightAnchor.constraint(equalTo: playerView.widthAnchor, multiplier: 1.0)

So autolayout will know how to position your playerView. otherwise playerView height will be 0.

Recommendations:

Don't create AVPlayer in cellFor instead do it in cell init or awakeFromNib (if storyboard is used). So AVPlayer will be reusable too. Just reset the setup with new url or playerItem. You can also use play or pause depending on ur case. But just don't recreate the AVPlayer every time cellFor is called.

UIView added as Overlay In AVPlayer won't fire any action when touched

The problem is that the adBanner has zero size (because you gave it no frame). A subview of a view with zero size, like your gad banner view, can be seen but cannot be touched.



Related Topics



Leave a reply



Submit