Video not play in AVPlayer or safari but play in chrome - ios

I have open AVPlayer to play the video by url link, but video not play in player and also not play in safari but video play on chrome browser.
My code is >>>>>>>>>
if url != nil {
let player = AVPlayer(url: url!)
player.allowsExternalPlayback = true
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resizeAspectFill
player.play()
}

Try this >>>
let player = AVPlayer(url: url)
player.rate = 1
playerViewController.player = player
playerViewController.view.frame = self.view.frame
playerViewController.showsPlaybackControls = true
addChild(playerViewController)
self.view.addSubview(playerViewController.view)
playerViewController.didMove(toParent: self)
It is work fine .

Related

Why does my AVPlayerViewController not show playback controls?

My AVPlayerViewController does not show the video controls even though .showsPlaybackControls is set to true. This started with iOS 16 and the weird thing is .showsPlaybackControls = false will dismiss the controls visible when they're visible through clicking on the video.
var playerController = AVPlayerViewController()
var playerItem = AVPlayerItem(asset: assetComp)
player = AVPlayer(playerItem: playerItem)
playerController.player = player
playerController.showsPlaybackControls = true
self.view.addSubview(playerController.view)
self.addChild(playerController)
player.play()

How to play AVQueuePlayer in loop

I have a couple of videos that need to be played in a loop.
let player = AVQueuePlayer(items: items)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = view.bounds
playerLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(playerLayer)
player.play()
I am trying to achieve by notification but I am getting the proper update.
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) {[weak self] (notification) in
print("Notification: \(notification.object)")
}
Also tried to use PlayerLooper but it's playing the single item in loop.
queuePlayer = AVQueuePlayer(items: items)
playerLooper = AVPlayerLooper(player: queuePlayer!, templateItem: items[0])
let playerLayer = AVPlayerLayer(player: queuePlayer!)
playerLayer.frame = view.bounds
playerLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(playerLayer)
queuePlayer?.play()

IOS 12.4.4 AVPlayer is not playing audio

I am trying to develop radio streaming app from URL audio using AVPlayer I was testing (simulator IOS 13.0 ) and everything was working.
issue is when I try to run physical device with IOS 12.4.4 nothing is working(playing)
this is what I did so far.
let url = "http://radiostreaming"//streaming url
var isPlaying = false;
var player : AVPlayer!
var playerItem : AVPlayerItem!
override func viewDidLoad() {
super.viewDidLoad()
playerItem = AVPlayerItem(url: URL(string: url)!)
player = AVPlayer(playerItem: playerItem)
player.volume = 1.0
player.rate = 1.0
player.allowsExternalPlayback = true
player.play()
}
any help please

add AVPlayerViewController view to external window

I want to programmatically add an AVPlayerViewController view to an external window from an iPad. The problem is that when adding it, the player view says "This video is playing on the TV"
example code:
let player = AVPlayer(url: someUrl)
//these two properties seem to have no effect
//player.usesExternalPlaybackWhileExternalScreenIsActive = true/false
//player.allowsExternalPlayback = true/false
let viewController = AVPlayerViewController()
viewController.player = player
viewController.view.frame = frame
externalWindow.addSubview(viewController.view)
player.play()
Is there a way to add the video view myself to the external window so I can decide the size?
If you want to control the size of the AVPlayerViewController you can use the layer or you can add it as a childViewController
Using Layer
let videoURL = URL(url to your file)
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
In the above code the frame can be adjusted as per the requirement.
Using ChildViewController:
let videoURL = URL(fileURLWithPath: videoPath)
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.view.frame = CGRect(give the appropriate size)
playerViewController.player = player
self.addChildViewController(playerViewController)
self.view.addSubview(playerViewController.view)
playerViewController.didMove(toParentViewController: self)
Let me know if this helps!! or anything else is needed.

Video not opening when button is clicked in Swift 3/4?

I am trying to build an app where I have a button and when you click on a button it should open the video. But for some reason, the button is being tapped but not opening the video. Can any one check, what I have done wrong. The video is in my Folder Directory in Xcode.
Code:
var playerview = AVPlayer()
var playerviewcontroller = AVPlayerViewController()
#IBAction func playBtnTapped(_ sender: Any) {
//print("btn tapped")
let videoURL = URL(string: "Promo.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
}
I got it to solved by doing and using the AVPlayerViewController:
let videoURL = Bundle.main.url(forResource: "Promo", withExtension: ".mp4")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
Try this as url:
Bundle.main.url(forResource: "Promo", withExtension: ".mp4")

Resources