AVPlayerViewController black screen when swiping on iOS 11
Okay I found the mistake:
To close the Airplay video playback when pressing "Done" I used this code:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if avPlayerController?.isBeingDismissed ?? false {
avPlayerController?.player = nil
}
}
But with iOS 11, Apple added a feature to close the Video Player via a swipe gesture. So when I swipe, the viewWillAppear
function gets called.
Putting this code inside viewDidAppear
fixed this and preserved the AirPlay-Fix.
AVPlayerViewController show black screen some times
After so many time.. I found solution.
The problem was that I wasn't cleaning AVPlayer inside AVPlayerController. And I also added New instance inside a DispachQueue.
That's new code:
self.videoPlayerViewController?.player?.pause()
self.videoPlayerViewController?.player = nil
self.videoPlayerViewController = nil
self.videoPlayerViewController = AVPlayerViewController()
self.videoPlayerViewController?.player = viewModel.avPlayer
And after I added in viewController:
if let avController = self.videoPlayerViewController {
DispatchQueue.main.async { [weak self] in
if let strongSelf = self {
strongSelf.add(avController, in: strongSelf.playerView)
avController.player?.play()
}
}
} else {
// Error
}
I hope it could help someone!!
Playing video in Swift using AVPlayerViewController
The simple answer to your question 'Is there a way I can make all mp4 play in my iOS app?' is unfortunately, no, and the same actually applies for Android also.
MP4 is a container format and it can contain many different types of video and audio encodings. Deviecs will typically only support a defined set, although it is typically quite a wide set and it is usually straightforward to choose a format that will work on both Android and iOS, if you have the luxury of being able to specify the format, or can transcode to your chosen format on the server side.
For Android the supported media types are here:
- https://developer.android.com/guide/topics/media/media-formats
And for apple devices the best source (at the time of writing) is:
- https://developer.apple.com/documentation/http_live_streaming/hls_authoring_specification_for_apple_devices
iOS 10 - AVPlayer shows black screen when playing video
Thanks for your replies! I just found that, for some reason in iOS 10 the viewDidLayoutSubviews
did not get called as it was in iOS 9. Actually I was setting the player layer's frame in the viewDidLayoutSubviews
method. Since it didn't get called, I was not able to see the player on screen. What I did was, set the player layer's frame in the loadPlayer
method above.And it works fine. Thanks! Hope this helps someone.
EDIT
This is the line of code that I moved from viewDidLayoutSubviews
to my loadPlayer
method and it worked:
self.playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
Pre-buffering for AVQueuePlayer
Ok, I've looked over this problem again and written some code to check out AVQueuePlayer
.
jollyCocoa's answer pointed me in the right direction by suggesting to observe the status property on AVPlayerItem
. However the documentation doesn't seem to point out that this property (and it's AVPlayerItemStatusReadyToPlay
value in particular) might be related to buffering.
However the AVPlayerItem's
loadedTimeRanges
property seems more related to buffering.
Doing KVO on that array was a bit trickier - the array object itself doesn't change, only it's items do - so I resorted to printing out it's content every second.
What I found out is that a few seconds in the queue's first item, the loadedTimeRanges
for the second item shows up a new CMTimeRange
with start time 0 and some small duration. The duration can increase up to 60 or so seconds while the previous item keeps playing.
Short answer: AVQueuePlayer
will buffer the next AVPlayerItem
while playing the current one.
iPhone X hide home indicator on view controller
You should override prefersHomeIndicatorAutoHidden
in your view controller to achieve that:
override var prefersHomeIndicatorAutoHidden: Bool {
return true
}
Related Topics
Swift-Setting a Physics Body Velocity by Angle
Textfield Delegate Shouldchangecharactersinrange
How to Cast from Uint16 to Nsnumber
Background Upload with Share Extension
Ambiguous Use of Operator '-' in Swift with 'Abs()'
Zposition of Sknode Relative to Its Parent
How to Apply a Grace Time Using Rx
Saving a Codable Struct to Userdefaults with Swift
Implementing Swift Protocol Methods in a Base Class
How to Edit Swift Error Breakpoint
How to Make a Segue to Second Item of Tab Bar
How to Use the "Handler" of Uialertaction to Call Another Uialertaction
How to Select All Text in a Uitextfield Using Swift
How to Unwrap Arbitrarily Deeply Nested Optionals in Swift
Getting Data Out of Nsdata with Swift
Pattern Variable Binding Cannot Appear in an Expression
iOS Corebluetooth: Startadvertising() Error Advertising Static Data