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

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")

Related

AVPlayer is not playing a locally saved video file

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()
}
}

how can I remove progress bar or video bar from a video on a tvOS?

#IBAction func buttonPlay(_ sender: Any) {
guard let path = Bundle.main.path(forResource: "NORWAY", ofType: "m4v") else {
debugPrint("video.m4v not found")
print("video playing")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
let controller = AVPlayerViewController()
controller.player = player
present(controller, animated: true) {
player.play()
player.volume = 0
}
}
I want a video to be played in a loop where the user cannot forward or rewind the video.
I want a video wallpaper but not a video player.
video bar
You just have to hide your playback controls: controller.showsPlaybackControls = false

add AVPlayerViewController view to external window

I want to programmatically add an AVPlayerViewController view to an external window from an iPad. The problem is that when adding it, the player view says "This video is playing on the TV"
example code:
let player = AVPlayer(url: someUrl)
//these two properties seem to have no effect
//player.usesExternalPlaybackWhileExternalScreenIsActive = true/false
//player.allowsExternalPlayback = true/false
let viewController = AVPlayerViewController()
viewController.player = player
viewController.view.frame = frame
externalWindow.addSubview(viewController.view)
player.play()
Is there a way to add the video view myself to the external window so I can decide the size?
If you want to control the size of the AVPlayerViewController you can use the layer or you can add it as a childViewController
Using Layer
let videoURL = URL(url to your file)
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
In the above code the frame can be adjusted as per the requirement.
Using ChildViewController:
let videoURL = URL(fileURLWithPath: videoPath)
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.view.frame = CGRect(give the appropriate size)
playerViewController.player = player
self.addChildViewController(playerViewController)
self.view.addSubview(playerViewController.view)
playerViewController.didMove(toParentViewController: self)
Let me know if this helps!! or anything else is needed.

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

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