I engaged into this more than two days. I couldn't get any solution please.
I have requirement is video should play Portrait inline , full screen , landscape. My problem here is how to identify the video is playing full-screen landscape or Portrait. I have implemented viewWillTransitionToSize
method. But AVPlayer
has full-screen arrow button. How I can identify user clicked that option.
The second requirement , once video complete, create view on top of video show replay or next or previous option.
This is my code;
if videoCellVal == nil {
videoCellVal = videoCell
comPlayerControl = AVPlayerViewController()
if let player = comPlayerControl {
let videoURL: String = "http://cdnapi.kaltura.com/p/11/sp/11/playManifest/entryId/"+selectedSubmission.transcodeRefId+"/format/applehttp/protocol/http/a.m3u8"
let playerItem = AVPlayerItem(URL: NSURL(string: videoURL)! )
commmentPlayer = AVPlayer(playerItem: playerItem)
player.player = commmentPlayer
player.view.frame = videoCell.frame
player.view.sizeToFit()
player.showsPlaybackControls = true
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:",
name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
videoCell.addSubview(player.view)
}
}
func playerDidFinishPlaying(note: NSNotification) {
print("Video Finished")
let DynamicView=UIView(frame: CGRectMake(100, 200, 100, 100))
DynamicView.backgroundColor=UIColor.greenColor()
DynamicView.layer.cornerRadius=25
DynamicView.layer.borderWidth=2
DynamicView.bringSubviewToFront(self.view)
self.view.addSubview(DynamicView)
}
For the detecting the Full screen or video size change you can use KVO for the AVPlayerViewController, code can be like this:
[comPlayerControl .addObserver(self, forKeyPath:"videoBounds" , options: NSKeyValueObservingOptions.New, context: nil)]
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
print("KeyPath \(keyPath)")
if keyPath == "videoBounds" {
print("New Video Bounds \(change)")
}
}
In the change
dictionary you can check the change in the video frame/bounds change
For the rotation as you mentioned you may have to use viewWillTransitionToSize
or UIDeviceOrientationDidChangeNotification
notification or you can check the current orientation using UIDevice.currentDevice().orientation
as i didn't find any method which can check the change in the ongoing video
Still if you need to detect video orientation you may check this link : https://stackoverflow.com/a/25833399/4557505 , and other answer in that link
For the repeat of the video you can set the time to zero like this in your code:
func playerDidFinishPlaying(notification: NSNotification) {
if let item = notification.object as? AVPlayerItem {
item.seekToTime(kCMTimeZero)
//you may directly play the video or can do further processing
}
}
to play item in the queue at the end of the item you may try with commmentQueuePlayer.advanceToNextItem()
You may try putting replay,next,previous button in the contentOverlayView
let topView = UIView(frame: CGRectMake(0,0, comPlayerControl.view.bounds.width,44))
topView.backgroundColor = UIColor ( red: 0.5, green: 0.5, blue: 0.5, alpha: 0.379 )
let btnNext = UIButton(frame:CGRectMake(0,0,80,44))
btnNext.setTitle("Next", forState:.Normal)
btnNext.addTarget(self, action:"playNext", forControlEvents:.TouchUpInside)
btnNext.userInteractionEnabled = true
btnNext.enabled = true
topView.addSubview(btnNext)
comPlayerControl.contentOverlayView?.addSubview(topView)
Update
For the observeValueForKeyPath
you may try something like this
var avWidth:CGFloat = 0
var avHeight:CGFloat = 0
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "videoBounds" {
let rect = change!["new"]! as! NSValue
if let newrect = rect.CGRectValue() as CGRect? {
if newrect.width > 0 || newrect.height > 0 {
if avWidth > 0 || avHeight > 0 {
if newrect.width > avWidth || newrect.height > avHeight {
print("Full Screen")
} else if newrect.width < avWidth || newrect.height < avHeight {
print("Normal screen")
} else {
}
}
avWidth = newrect.width
avHeight = newrect.height
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With