AVPlayer is not playing a locally saved video file - ios

I have this code attempting to play a previously saved file (which exists) but I see a black screen while playing the video.
static func playVideo(from filename: String, vc: UIViewController, view: UIView) -> Bool {
let filepath = videosDirectoryPath + filename
if (MediaUtils.fileExists(filePath: filepath)) {
Logging.logError("Playing video failed, file not found: \(filepath)")
return false
}
let player = AVPlayer(url: URL(fileURLWithPath: filepath))
let playerViewController = AVPlayerViewController()
playerViewController.player = player
playerViewController.view.frame = view.frame
view.addSubview(playerViewController.view)
vc.present(playerViewController, animated: true) {
player.play()
}
return true
}
I do see an error Unbalanced calls to begin/end appearance transitions for <AVPlayerViewController: 0x109010e00>. but not sure if that has anything to do here. I am able to play a URL fine with this:
static func playVideo(videoUrl: String, vc: UIViewController, view: UIView) {
Logging.logDebug("Playing video \(videoUrl)")
let player = AVPlayer(url: URL(string: videoUrl)!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
playerViewController.view.frame = view.frame
view.addSubview(playerViewController.view)
vc.present(playerViewController, animated: true) {
player.play()
}
}
Any idea why the locally saved file cannot be played. Wish there is an error from player.play().

You can give this a shot 😉
func playVideo(videoUrl: String, vc: UIViewController, view: UIView) {
let player = AVPlayer(url: NSURL(fileURLWithPath: videoUrl) as URL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resizeAspect
playerLayer.frame = logoImageView.frame
view.layer.addSublayer(playerLayer)
vc.present(playerViewController, animated: true) {
player?.play()
}
}

Related

Video not opening when button is clicked in Swift 3/4?

I am trying to build an app where I have a button and when you click on a button it should open the video. But for some reason, the button is being tapped but not opening the video. Can any one check, what I have done wrong. The video is in my Folder Directory in Xcode.
Code:
var playerview = AVPlayer()
var playerviewcontroller = AVPlayerViewController()
#IBAction func playBtnTapped(_ sender: Any) {
//print("btn tapped")
let videoURL = URL(string: "Promo.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
}
I got it to solved by doing and using the AVPlayerViewController:
let videoURL = Bundle.main.url(forResource: "Promo", withExtension: ".mp4")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
Try this as url:
Bundle.main.url(forResource: "Promo", withExtension: ".mp4")

Background video loop not working in swift

I have been created page control, and in that the background videos will be get loaded, the videos is getting played but the loop is not working, i had refered in stackoverflow but i couldn't get the right answer
here is mine code:
override func viewDidLoad() {
super.viewDidLoad()
self.pageController.currentPage = 0
loadVideo(currentPage: 0)
}
func loadVideo(currentPage: Int) {
DispatchQueue.main.async {
self.videoPath = Bundle.main.url(forResource: "BoardVideoArray[currentPage]", withExtension: "mp4")
self.player = AVPlayer(url: self.videoPath!)
self.player?.actionAtItemEnd = .none
self.player?.isMuted = true
self.playerLayer = AVPlayerLayer(player: self.player)
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
self.playerLayer.zPosition = -1
}
playerLayer.frame = videoVIew.frame
videoVIew.layer.addSublayer(playerLayer)
videoVIew.bringSubview(toFront: pageController)
player?.play()
//loop video
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player?.currentItem, queue: nil, using: { (_) in
DispatchQueue.main.async {
self.player?.seek(to: kCMTimeZero)
self.player = AVPlayer(url: self.videoPath!)
self.player.play()
}
})
}
I have declared the player and avplayerLayer out of scope
var player: AVPlayer!
var playerLayer = AVPlayerLayer()
I spend more than 4 hours, but i couldn't found the mistake which i have made.
The issue is video is not geting looping
Try this way.
guard let path = Bundle.main.path(forResource: "video", ofType:"m4v") else {
debugPrint("video.m4v not found")
return
}
//VideoPlayer is a subClass of AVPlayer
let player = VideoPlayer(url: URL(fileURLWithPath: path))
playerViewController = AVPlayerViewController()
playerViewController.videoGravity = AVLayerVideoGravityResizeAspectFill
playerViewController.player = player
//self.videoPlayerView is a UIView in which i want to show video.
playerViewController.view.frame = self.videoPlayerView.bounds
self.videoPlayerView.addSubview(playerViewController.view)
playerViewController.showsPlaybackControls = false
playerViewController.player?.play()
player.shouldMute(mute: true)
NotificationCenter.default.addObserver(self, selector: #selector(HomeViewController.videoDidStopPlaying(notification:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
func videoDidStopPlaying(notification:Notification) {
self.playerViewController.player?.seek(to: kCMTimeZero)
self.playerViewController.player?.play()
}
I wrote a Swift UIView subclass called SDLoopingVideoView that plays and loops any compatible AVPlayer video file and automatically scales it to fill the view. It can be entirely setup in Interface Builder This work great as a video background and you won't have to worry about the video looping since it's managed automatically. You can find it on Github here: SDLoopingVideoView

Play Local Video File

I have followed a tutorial and trying to get a video to pop up when I press a button. The video pops up but the actual video does not start playing and I just get this screen....
Blank Screen image
the code is as follows:
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
var playerController = AVPlayerViewController()
var player:AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
let videoString:String? = Bundle.main.path(forResource: "motorbikes", ofType: ".m4v")
if let url = videoString {
let videoURL = NSURL(fileURLWithPath: url)
self.player = AVPlayer(url: videoURL as URL)
}
}
#IBAction func goButton(_ sender: Any) {
self.present(self.playerController, animated: true, completion: {
self.playerController.player?.play()
})
}
}
You have forgot to set playercontroller's player instance as phuc-nguyen said
or else you can make use of AVPlayerLayer to play the video without using AVPlayerViewController
let player = AVPlayer(url: URL(string: "path/myvideo.mp4")!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
Using AVPlayerLayer you can customize the player like adding your own controls or views and it won't be a full screen view usually though.
But if you are not looking for any customization its good to stick with AVPlayerViewController it has all the controls baked in
Depends on your requirement

Stop AV Player after done is pressed

I get my local video to load into a new view controller, but after hitting done it disappears and immediately pops back up again. I don't know if there is a way to track the notifications or what.
Here is my code:
import UIKit
import AVKit
import AVFoundation
class VideoController1: UIViewController {
var playerViewController = AVPlayerViewController()
var playerView = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
let fileURL = NSURL(fileURLWithPath: "/Users/jamesjarrett/Desktop/NIMS/NIMS/NIMS/Vid2.mp4")
playerView = AVPlayer(url: fileURL as URL)
playerViewController.player = playerView
self.present(playerViewController, animated: true){
self.playerViewController.player?.play()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
You put your code
let fileURL = NSURL(fileURLWithPath: "/Users/jamesjarrett/Desktop/NIMS/NIMS/NIMS/Vid2.mp4")
playerView = AVPlayer(url: fileURL as URL)
playerViewController.player = playerView
self.present(playerViewController, animated: true){
self.playerViewController.player?.play()
}
inside of ViewDidAppear for the ViewController.
ViewDidAppear() occurs when your view is presented on the screen again. So this means, that when the AVPlayer is closed, it goes back to that View, and the AVPlayer will be presented again.
You have 2 options:
Make a UIButton and an Action to that button
var button = UIButton(frame: CGRect(0, self.view.frame.height*0.9, self.view.frame.width, self.view.frame.height*0.1))
button.addTarget(self, #selector(VideoController1.playController), nil)
self.view.addSubview(button)
let fileURL = NSURL(fileURLWithPath: "/Users/jamesjarrett/Desktop/NIMS/NIMS/NIMS/Vid2.mp4")
func playController() {
playerView = AVPlayer(url: fileURL as URL)
playerViewController.player = playerView
self.present(playerViewController, animated: true){
self.playerViewController.player?.play()
}
}
-- OR --
If you want the controller to play only the first time the user sees the view, and that is it, then you can do
var played = false
func viewDidAppear() {
if(!played) {
let fileURL = NSURL(fileURLWithPath: "/Users/jamesjarrett/Desktop/NIMS/NIMS/NIMS/Vid2.mp4")
playerView = AVPlayer(url: fileURL as URL)
playerViewController.player = playerView
self.present(playerViewController, animated: true){
played = true
self.playerViewController.player?.play()
}
}
}
Don't quote me on the code above, just typed it up real quick to give you a general idea.

I can't load video file from local server in swift

I'm working with iOS using Swift.
I want to load a video file using AVPlayer in Swift.
I was able to load an image and a text file from the local server. But I couldn't load a video file. This is the code that I'm using.
let movieUrl = NSURL(string: "http://akjfsjf/ajdfl/ajkf/car.mp4")
let player = AVPlayer(URL: movieUrl!)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
I have tried with following URL and function
func playvideo (){
let movieUrl = NSURL(string: "http://www.w3schools.com/html/movie.mp4")
let player = AVPlayer(URL: movieUrl!)
let playerController = AVPlayerViewController()
playerController.player = player
player.play()
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
}
Also from your code i found you are using http rather than https so you need to add following property in plist file.
Try this one,
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
var playerVC : AVPlayerViewController!
var playerItem : AVPlayerItem!
var player : AVPlayer!
var playerLayer: AVPlayerLayer!
override func viewDidAppear(animated: Bool) {
let url = NSURL.init(fileURLWithPath: "your file path")
self.playerItem = AVPlayerItem.init(URL: url!)
self.player = AVPlayer.init(playerItem: self.playerItem)
self.playerVC = AVPlayerViewController.init();
self.playerVC.player = self.player;
self.player.currentItem!.playbackLikelyToKeepUp
self.presentViewController(self.playerVC, animated: true) { () -> Void in
self.playerVC.player?.play()
}
}
}
Hope this helps!

Resources