AVPlayerViewController Showing Black Screen - ios

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.

Related

Issue displaying subviews above video | Swift 5

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
}()

Unable to play streaming audio

I want to play streaming audio on an iOS app. I use this code to play audio:
let url = URL(string: "http://online.radiorecord.ru:8102/chil_320.m3u")!
let asset = AVAsset(url: url)
let playerItem = AVPlayerItem(asset: asset)
let player = AVPlayer.init(playerItem: playerItem)
player.play()
When I run the app on the Simulator I get this error:
2018-11-30 21:57:26.097577+0300 radio[7417:21800733]
[AudioHAL_Client]
AudioHardware.cpp:1210:AudioObjectRemovePropertyListener:
AudioObjectRemovePropertyListener: no object with given ID 0
How to fix this issue?
var player: AVPlayer?
let url = URL(string: "http://online.radiorecord.ru:8102/chil_320.m3u")!
player = AVPlayer(url: url)
player?.play()

AVPlayerViewController issue with iOS 12

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

how to play a video in UIView swift?

I have a video which I want to download from a server and stream it in a fixed view.
I've set a UIView in my storyboard with fixed constraints, and here is what I've done in code:
#IBOutlet weak var videoView: UIView!
var player: AVPlayer!
var avpController = AVPlayerViewController()
And in my viewDidLoad I've done this:
let url = URL(string:myURL)
player = AVPlayer(url: url!)
avpController.player = player
avpController.videoGravity = AVLayerVideoGravity.resizeAspect.rawValue
self.addChildViewController(avpController)
avpController.view.frame = videoView.frame
self.containerView.addSubview(avpController.view)
videoView.layer.masksToBounds = true
My problem is my video is not with the size that I've set to videoView and in every device my video is in a different size. In some devices, the video height is larger than the height that I've set and it overlays the items that I have below videoView. How can I play video in a view in a right way?
If you want to use AVPlayerViewController:
let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
For AVPlayer:
let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
the only way to add a video to a UIView with fixed constraints in storyboard, was this :
let url = URL(string:myURL)
player = AVPlayer(url: url!)
avpController.player = player
avpController.view.frame.size.height = videoView.frame.size.height
avpController.view.frame.size.width = videoView.frame.size.width
self.videoView.addSubview(avpController.view)
I hope other can use this! :)
You can try to use AVPlayerLayer in a AVPlayer.
let player = AVPlayer(url: video) // your video url
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = videoView.bounds
videoView.layer.addSublayer(playerLayer)
player.play()
This is how you can add and remove player in uiview.
var player: AVPlayer?
var playerController: AVPlayerViewController?
func removePlayer() {
player?.pause()
player = nil
playerController?.player?.pause()
playerController?.player = nil
if let view = playerController?.view {
videoView.willRemoveSubview(view)
}
playerController?.view.removeFromSuperview()
playerController = nil
}
func setVideo(url: URL) {
removePlayer()
player = AVPlayer(url: url)
playerController = AVPlayerViewController()
playerController?.player = player
self.videoView.addSubview((playerController?.view)!)
playerController?.view.frame = CGRect(x: 0, y: 0, width: self.videoView.bounds.width, height: self.videoView.bounds.height)
// player.play()
}

Video Player Controllers are not working After Playing Video Swift

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
}

Resources