NSNotificationCenter to AVQueuePlayer in swift - swift

I added list of AVQueuePlayer to UITableViewCell. I added array of videos to AVQueuePlayer as follows:
if let urls = delegate.arrayVideofeedItemsList[indexPath.row] as? NSArray
{
let player = AVQueuePlayer()
for currentVideoObject in urls {
let avAsset = AVURLAsset(URL: NSURL(fileURLWithPath: "\(currentVideoObject)"))
avAsset.loadValuesAsynchronouslyForKeys(["playable", "tracks", "duration"], completionHandler: {
dispatch_async(dispatch_get_main_queue(), {
// self.enqueue(avAsset)
let item = AVPlayerItem(asset: avAsset)
player.insertItem(item, afterItem: nil)
})
})
}
let playerLayer = AVPlayerLayer(player: player)
// playerLayer.backgroundColor = UIColor.greenColor().CGColor
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
playerLayer.frame = CGRectMake(0, 0,SCREEN_WIDTH, 250)
cell.contentView.layer.addSublayer(playerLayer)
player.play()
player.actionAtItemEnd = AVPlayerActionAtItemEnd.Advance
player.volume = 0.0
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification,object:player.currentItem)
}
func playerItemDidReachEnd(notification: NSNotification) {
let p: AVPlayerItem = notification.object as! AVPlayerItem
p.seekToTime(kCMTimeZero)
}
I have three videos in AVQueuePlayer. Three videos are played sequentially but After completion of this first video is not playing. What I need to add to this ? Please help me to complete it.

Try to add this line after initializing player:
player.actionAtItemEnd = AVPlayerActionAtItemEnd.None

Related

How do I add configure an AVPlayer to present an image rather than video?

I have a page view controller - a feed - which an AVPlayer is nested in. How would I configure the AVPlayer to present just an image, or add a switch statement, to present an ImageViewController when an image is loaded into the newsfeed hierarchy?
private func configureVideo() {
guard let path = Bundle.main.path(forResource: "video", ofType: "mp4") else {
return
}
let url = URL(fileURLWithPath: path)
player = AVPlayer(url: url)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resizeAspectFill
playerLayer.frame = self.view.frame
playerLayer.frame = self.view.frame.inset(by: UIEdgeInsets(top: 0, left: 0.5, bottom: 80, right: 0.5))
self.view.layer.addSublayer(playerLayer)
player?.volume = 0
player?.play()
guard let player = player else {
return
}
playerDidFinishObserver = NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime,
object: player.currentItem,
queue: .main
) { _ in
player.seek(to: .zero)
player.play()
}
}
}

overlay UIView over AVPlayer - Swift - Programmatically

I have a AVPlayer which I set in this way:
fileprivate func setUpPlayer(_ videoURL: String, completion: #escaping () -> Void) {
let url = URL(string: videoURL)
self.player = AVPlayer(url: url!)
self.playerLayer = AVPlayerLayer(player: player)
let audioSession = AVAudioSession.sharedInstance()
playerLayer.frame = view.bounds
playerLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(playerLayer)
player.volume = 1
player.play()
player.actionAtItemEnd = .none
}
I want to put a UIView which contains textViews and drawings over it but I don't know how to do it.
The ideal would be something similar to UIGraphicsGetImageFromCurrentImageContext() with images

AVPlayer not working properly - Swift

//Firstly my English very bad, I'm sorry...
I'm implementing AVPlayer my app. Player is working fine but if I play another videos many times Player is not working and only just this screen looks.
Here's my code
let player:AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
let videoFile = object?.objectForKey("video") as? PFFile
videoFile?.getFilePathInBackgroundWithBlock({ (filePath, error) in
if error == nil {
let videoURL = NSURL(fileURLWithPath: filePath!)
self.player = AVPlayer(URL: videoURL)
let playerController = AVPlayerViewController()
playerController.player = self.player
playerController.showsPlaybackControls = false
playerController.videoGravity = AVLayerVideoGravityResizeAspectFill
playerController.view.frame = self.videoView.bounds
self.videoView.addSubview(playerController.view)
self.addChildViewController(playerController)
self.player!.play()
} else {
print(error)
}
})
}
I don't understand the way you fetch the video object but you can try the following code. It works fine.
var player:AVPlayer! // this shouldn't be let.
override func viewDidLoad() {
super.viewDidLoad()
let query = PFQuery(className:"TestClass") //your classname in parse. Change it with your clas name
query.getObjectInBackgroundWithId("SALe4gX2nk") { // get the video file with id. Change it with your id.
(object: PFObject?, error: NSError?) -> Void in
let videoFile = object?.objectForKey("video") as? PFFile
let url = NSURL(string: (videoFile?.url)!)
self.player = AVPlayer(URL: url!)
let playerController = AVPlayerViewController()
playerController.player = self.player
playerController.showsPlaybackControls = false
playerController.videoGravity = AVLayerVideoGravityResizeAspectFill
playerController.view.frame = self.view.bounds
self.view.addSubview(playerController.view)
self.addChildViewController(playerController)
self.player!.play()
}
}

AVPlayer videoGravity AVLayerVideoGravityResizeAspectFill doesn't work Swift2.2

Please help me with AVPlayer. I need resize video when iPhone rotate (landscape orientation). Here is my code:
var player: AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
// Load the video from the app bundle.
let videoURL: NSURL = NSBundle.mainBundle().URLForResource("bg_animate", withExtension: "3gp")!
player = AVPlayer(URL: videoURL)
player?.actionAtItemEnd = .None
player?.muted = true
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
playerLayer.zPosition = -1
playerLayer.frame = view.frame
view.layer.addSublayer(playerLayer)
player?.play()
//loop video
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.loopVideo), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
}
But string:
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
doesnt work.
I also set value as AVLayerVideoGravityResize and AVLayerVideoGravityResizeAspect, but had similar results...

How to detect SKVideoNode playback completion

I am using an SKVideoNode to display animated splash scene using this code:
SKVideoNode *video = [SKVideoNode videoNodeWithVideoFileNamed:#"splash_video.mp4"];
video.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
[self addChild:video];
[video play];
I wish to display the next scene once video playback is done. How can I achieve that?
Add this to your viewDidLoad or elsewhere suitable
let videoURL = NSURL(string: url)
let player = AVPlayer(URL: videoURL!)
player.actionAtItemEnd = .None
let videoSpriteKitNode = SKVideoNode(AVPlayer: player)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification, object: player.currentItem)
And add this function:
func playerItemDidReachEnd(notification: NSNotification) {
if let playerItem = notification.object as? AVPlayerItem {
//Start your next video here
}
}