AVPlayer on Swift app shows a black screen but plays audio when playing video
The problem is with the video file itself / the encoding.
Deceptively, the Quicktime ".mov" format is not a video codec, but rather audio/video container format that can contain video and audio (and some other things) in any number of compression codecs (h.264, mpeg2, ProRes, MJPEG, AAC, mp3, etc.) … Your files don't work because they include video compressed with a codec which iOS does not support.
From @alexkent in this SO answer on another post.
I don't think a .mov
is necessarily not going to work for you, but you'd have to make sure it was encoded in a way that is iOS-compatible. So as you mentioned in the comments, using .mp4
ensures the video is encoded in an iOS-compatible fashion.
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);
AVPlayer blank video: playing sound but no video
In case anyone encounters this issue, the problem seems related to an iOS bug. The problem doesn't occur on iOS 8/9 devices, only on new iOS 10 devices. Starting with iOS 10.2, the problem vanishes.
Unfortunately, still no programmatic solution for 10.0.x or 10.1.x.
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!!
Related Topics
iOS Uiimagepickercontroller: Any Way of Getting the Date of the Chosen Picture
Save Struct in Class to Nsuserdefaults Using Swift
Swift Custom Uitableviewcell Not Displaying Data
Navigationlink Inside Lazyvgrid Cycles All Entries on Back, Swiftui
Sharing Image and Text to Facebook Messenger with Uiactivityviewcontroller Failing
Target Is Not Found, Please Reconnect the Device, Xcode:Device Support File
Apply Nspredicate on [(String, Array<String>)]
How to Get Only Keys from Firebase
I Want to Switch Time Scrubber to Do 15 Min Intervals
How to Create an Alert in a Swift File Model That Works for Various View Controller
How to Load Thousands Records to Realm Correctly
Dynamic Uitablecellview Height
Adding Firebase Data to an Array in iOS Swift