AVPlayer Inside A UIView - ios

So I have a view on my HomeViewController that I want to play a video inside of. on viewDidLoad() I am calling playVideo() here is my code:
func playVideo() {
guard let filePath = Bundle.main.path(forResource: "video", ofType: "mov") else {
debugPrint("video.mov not found.")
return
}
let avPlayer = AVPlayer(url: URL(fileURLWithPath: filePath))
let avPlayerController = AVPlayerViewController()
avPlayerController.player = avPlayer
avPlayerController.view.frame = playerView.bounds
avPlayerController.showsPlaybackControls = false
avPlayerController.player?.play()
playerView.addSubview(avPlayerController.view)
playerView.bringSubview(toFront: QRButton)
}
All I get is a black background and no video. What am I doing wrong? I have tried to solve this for a while now.
iOS: 11.2 beta
Xcode: 9.2 beta

Try to use avPlayer as a class variable, maybe it has been released before it can start to play.
class ViewController: UIViewController {
var avPlayer:AVPlayer?
func playVideo() {
guard let filePath = Bundle.main.path(forResource: "video", ofType: "mov") else {
debugPrint("video.mov not found.")
return
}
avPlayer? = AVPlayer(url: URL(fileURLWithPath: filePath))
let avPlayerController = AVPlayerViewController()
avPlayerController.player = avPlayer
avPlayerController.view.frame = playerView.bounds
avPlayerController.showsPlaybackControls = false
avPlayerController.player?.play()
playerView.addSubview(avPlayerController.view)
playerView.bringSubview(toFront: QRButton)
}
}

Related

How to play a video from file path in AVPlayer?

I have recorded a video from my app which saves the video in Photos app in iPhone. The file path of the video saved is:
file:///var/mobile/Containers/Data/Application/0A8F1D57-7336-4D52-8B64-928C381DBC4F/Documents/2022-04-14#14-26-59GMT+05:00ARVideo.mp4
How can I play the video from the file path in AVPlayer?
class SecondViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
playVideo()
}
private func playVideo() {
guard let filePath = Bundle.main.path(forResource: "file:///var/mobile/Containers/Data/Application/0A8F1D57-7336-4D52-8B64-928C381DBC4F/Documents/2022-04-14#14-26-59GMT+05:00ARVideo.mp4", ofType: "mp4") else { return}
let player = AVPlayer(url: URL(fileURLWithPath: filePath))
let vc = AVPlayerViewController()
vc.player = player
present(vc, animated: true)
}
}
import AVKit
func playVideo(){
let player = AVPlayer(url: URL(video url string) ?? URL(fileURLWithPath: "") )
let playerController = AVPlayerViewController()
playerController.player = player
self.addChild(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
playerController.exitsFullScreenWhenPlaybackEnds = true
player.play()
}
class SecondViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
playVideo()
}
// if u have video in app directory/ document directory
private func playVideo() {
guard let url = URL(string: "file:///var/mobile/Containers/Data/Application/0A8F1D57-7336-4D52-8B64-928C381DBC4F/Documents/2022-04-14#14-26-59GMT+05:00ARVideo.mp4")
else {
print("url not exist")
return
}
let player = AVPlayer(url: url)
let vc = AVPlayerViewController()
vc.player = player
present(vc, animated: true)
}
// if u have video locally that u put in ur project then
private func playVideoFromLocal() {
guard let fileURL = Bundle.main.url(forResource: "file_example_MOV_480_700kB", withExtension: "mov") else {return}
let player = AVPlayer(url: fileURL)
let playerController = AVPlayerViewController()
let vc = AVPlayerViewController()
vc.player = player
present(vc, animated: true)
}
}
The path of the documents changes for the app each time it is launched, which changes the first part of the URL.
Try the following updated function to get the current location of the file:
private func playVideo() {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let playerItem = AVPlayerItem(url: URL(fileURLWithPath: documentsPath.appendingFormat("/your-video-name-here.mp4")))
let player = AVPlayer(playerItem: playerItem)
let vc = AVPlayerViewController()
vc.player = player
present(vc, animated: true)
}

Issue displaying subviews above video | Swift 5

The login screen I am designing is near complete but fails to properly display both the username and password textfields once the view controller loads
The following function executes when the view loads - it properly plays the video and displays both the image and loginButton correctly, it appears to also appears to display the textfields for a split second until the video fully loads and plays.
func playVideo() {
guard let path = Bundle.main.path(forResource: "videoFile", ofType: "mp4") else {
return
}
let asset: AVAsset = AVAsset(url: URL(fileURLWithPath: path))
let playerItem = AVPlayerItem(asset: asset)
let queuePlayer = AVQueuePlayer(playerItem: playerItem)
self.playerLooper = AVPlayerLooper(player: queuePlayer, templateItem: playerItem)
let playerLayer = AVPlayerLayer(player: queuePlayer)
playerLayer.frame = self.view.bounds
playerLayer.videoGravity = .resizeAspectFill
self.videoLayer.layer.addSublayer(playerLayer)
queuePlayer.play()
videoLayer.bringSubviewToFront(img)
videoLayer.bringSubviewToFront(usernameTF)
videoLayer.bringSubviewToFront(passwordTF)
videoLayer.bringSubviewToFront(signinButton)
}
Any ideas what could be causing this?
You can change this code according to your need.
var player : AVPlayer?
var playerViewController : AVPlayerViewController?
func playLiveUrl(url:String){
let videoURL = URL(string: "http://bla blah.com")
self.player = AVPlayer(url: videoURL!)
self.playerViewController = AVPlayerViewController()
playerViewController?.player = self.player
playerViewController?.view.frame = self.topPlayerView.frame
playerViewController?.player?.play()
self.topPlayerView.addSubview(playerViewController?.view ?? UIView()) // Container topPlayerView which contains a player
}
var topPlayerView : UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .black
return view
}()

Create VideoPlayer With Custom Size in Swift 3

i want to show .mov file in custom size. i have an xib file for my controller.
thats top view is subclass of UIView. i want to show video in this view.
there is my code for create video view. but there is problem in size of my video player. how can solve this problem.
there is my code :
private func playVideo() {
guard let path = Bundle.main.path(forResource: "video_2017-06-30_15-49-05", ofType:"mov") else {
debugPrint("video.m4v not found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerController = AVPlayerViewController()
playerController.player = player
let avPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.frame = videoView.layer.frame
videoView.layer.insertSublayer(avPlayerLayer, at: 0)
}
If you want a native player to be added to your view with the seek bar and play pause button then use following code:
private func playVideo() {
guard let path = Bundle.main.path(forResource: "SampleVideo", ofType:"mp4") else {
debugPrint("video.m4v not found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerController = AVPlayerViewController()
playerController.player = player
playerController.view.frame = videoView.bounds
videoView.addSubview(playerController.view)
player.play()
}
and if you want just the video to be played in your view with AVPlayerLayer then you can do something like below:
var player : AVPlayer!
var avPlayerLayer : AVPlayerLayer!
#IBOutlet weak var videoView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
guard let path = Bundle.main.path(forResource: "SampleVideo", ofType:"mp4") else {
debugPrint("video.m4v not found")
return
}
player = AVPlayer(url: URL(fileURLWithPath: path))
avPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.videoGravity = AVLayerVideoGravity.resize
videoView.layer.addSublayer(avPlayerLayer)
playVideo()
}
override func viewDidLayoutSubviews() {
avPlayerLayer.frame = videoView.layer.bounds
}
private func playVideo() {
player.play()
}
This worked perfect for me hope it works for you :)

Play video from data chunks

I received video from node sever in chunks of data. But video does not play in AVPlayer. Here is my code.
let videoUrl = http://staging.teemo.me/api/video/stream/sample12.MOV
playVideo(path:videoUrl, self)
func playVideo(path:String, controller:UIViewController){
let yourFinalVideoURL = path
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])
if (yourFinalVideoURL != "") {
let player = AVPlayer(url: NSURL(fileURLWithPath: yourFinalVideoURL) as URL)
let playerController = AVPlayerViewController()
playerController.player = player
controller.present(playerController, animated: true) {
//player.play()
if #available(iOS 10.0, *) {
player.playImmediately(atRate: 1.0)
} else {
player.play()
}
}
}
}
The follow code works for me:
let videoURL = URL(string: "Some url")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
However when I tried to use your url it doesn't work.
I added the NSAppTransportSecurity in .plist, but it didn't work either.
It is obvious that you have a problem with the url, try to change the extension of the file sample12.MOV to sample12.mp4

Video Player Controllers are not working After Playing Video Swift

I am trying to play video from local and put it in a slider, video is playing but one the play button are disappear nothing is working in the video.
playVideo () {
guard let videoPath = NSBundle.mainBundle().pathForResource(files.fileImage, ofType: "mp4")
else {
print ("Error")
break
}
let player = AVPlayer(URL: NSURL(fileURLWithPath: videoPath))
let playerController = AVPlayerViewController()
playerController.player = player
playerController.view.frame = svSlider.frame
playerController.showsPlaybackControls = true
let videoPlayerView = playerController.view
videoPlayerView.frame = svSlider.frame
self.svSlider.addSubview(videoPlayerView)
fromLeft += videoPlayerView.frame.width
}

Resources