I'm using AVPlayerViewController for playing video it works fine with iOS 11 but the same code doesn't work in iOS 12. could anyone help me to fix this issue please?
let url: URL = Bundle.main.url(forResource: "sampleVideo", withExtension: ".mp4")!
let avAsset = AVURLAsset(url: url)
let playerItem = AVPlayerItem(asset: avAsset)
player = AVPlayer(playerItem: playerItem)
playerController = AVPlayerViewController()
playerController?.player = player
playerController?.view.frame = videoHolderView?.bounds ?? CGRect.zero
guard let videoView = playerController?.view else { return }
videoView.tag = 101
videoHolderView.addSubview(videoView)
player?.play()
Finally figured out the issue!!!
in iOS 12 if you disable PlayBackControls of AVPlayerController, AVPlayer will still consume the tap gesture.
playerController?.showsPlaybackControls = false
Related
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
}()
I am trying to play a video in AVplayer with below code but it's showing black screen.I also tried googling but didn't get any luck
let url = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let asset = AVURLAsset(url: url!)
let item = AVPlayerItem(asset: asset)
let player = AVPlayer.init(playerItem: item)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
I also added plist entry for TransportSecurity.
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)
}
}
I am using AVPlayer to play local video in background using loop and video is playing fine but after finishing video it takes pause to play video in loop. I have tried many methods and also seen many post on stack overflow but i failed to find appropriate solution. I am using Swift3.
Code is here :
var videoplayer :AVPlayer = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "background4", ofType: "mp4")
videoplayer = AVPlayer(url: URL(fileURLWithPath: path!))
videoplayer.volume = 0
videoplayer.actionAtItemEnd = AVPlayerActionAtItemEnd.none;
let playerLayer = AVPlayerLayer(player: videoplayer)
playerLayer.frame = self.view.frame
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
if (videoplayer.rate != 0) {
print("playing videoplayer")
self.blurBgImage.isHidden = true
}
playerLayer.zPosition = -1
videoplayer.rate = 0
videoplayer.play()
self.blurBgImage.layer.addSublayer(playerLayer)
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: videoplayer.currentItem, queue: nil, using: { (_) in
DispatchQueue.main.async {
let t1 = CMTimeMake(5, 100)
self.videoplayer.seek(to: t1)
self.videoplayer.play()
}
})
}
I have also tried AVPlayerLooper.
Code is :
var playerLooper: NSObject?
var playerLayer:AVPlayerLayer!
var queuePlayer: AVQueuePlayer?
func playVideo(_ filmName: String){
if let path = Bundle.main.path(forResource: filmName, ofType: "mp4") let url = URL(fileURLWithPath: path)
if #available(tvOS 10.0, *) {
let playerItem = AVPlayerItem(url: url as URL)
self.videoplayer = AVQueuePlayer(items: [playerItem])
self.playerLayer = AVPlayerLayer(player: self.videoplayer)
self.playerLooper = AVPlayerLooper(player: self.videoplayer as! AVQueuePlayer, templateItem: playerItem)
self.blurBgImage.layer.addSublayer(playerLayer!)
self.playerLayer?.frame = self.view.frame
self.videoplayer.volume = 10
self.videoplayer.play()
} else {
videoplayer = AVPlayer(url: url)
videoplayer.play()
loopVideo(videoplayer)
}
}
}
What should i do for seamless looping? Thanks in Advance.
FYI there is sample code here: https://developer.apple.com/library/content/samplecode/avloopplayer/Introduction/Intro.html
#matt's deleted answer works fine for me (on device / simulator) for iOS 10+ devices:
Use AVPlayerLooper. That is exactly what it is for.
https://developer.apple.com/reference/avfoundation/avplayerlooper
Basically it implements AVQueuePlayer for you, constantly updating the
queue so that it never ends.
It seamlessly loops without any white/black blip.
E.g.
private var looper: AVPlayerLooper?
...
let queuePlayer = AVQueuePlayer(playerItem: item)
looper = AVPlayerLooper(player: queuePlayer, templateItem: item)
videoPlayerLayer.player = queuePlayer
If you end up doing this in a reusable cell (e.g. UICollectionView), then make sure you disable looping before cell re-use or you'll get some obscure crashes:
looper?.disableLooping()
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
}