AVPlayer with Cloudflare - swift

I am playing videos from url and it works well but now we are trying to switch to cloudflare and would need to handle the videos that are sent. The only problem now is every other video url works except a cloudflare url
how I am playing
let fileURL = URL(string: "https://watch.cloudflarestream.com/cb9618c34c4fbf6fa88bb48b73")
player = AVPlayer(URL: fileURL!)
playerLayer = AVPlayerLayer(player: player)
playerLayer!.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer!)
player!.play()
how can I make AVPlayer play cloudflare videos

I recommend using the m3u8 format and the full link will be like this
https://videodelivery.net/5d5bc37ffcf54c9b82e996823bffbb81/manifest/video.m3u8 where 5d5bc37ffcf54c9b82e996823bffbb81 - VideoID. This ID I took from the browser source, but I can't play your example, perhaps you need to set some additional settings for your video to play. I mean videoID cb9618c34c4fbf6fa88bb48b73
You can test this code:
class ViewController: UIViewController {
var player = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let fileURL = URL(string: "https://videodelivery.net/5d5bc37ffcf54c9b82e996823bffbb81/manifest/video.m3u8")
player = AVPlayer(url: fileURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
}
}

Related

AVFoundation custom video player doesn't display the video?

I'm building a custom video player. To create it, I use play button and view. The video I want to see in the player, I get from the iPhone's Photos. The problem with the player is that I have a working URL of the video, but the view just doesn't display that, actually here is the code of it:`
#IBAction func playVideo(_ sender: Any) {
print("play video")
let videoURL: NSURL = NSURL(string: uurl.url!)!
let player = AVPlayer(url: videoURL as URL)
playerLayerr = AVPlayerLayer(player: player)
playerLayerr.frame = vlogView.bounds
self.vlogView!.layer.addSublayer(playerLayerr)
player.play()
}
The interesting fact is that player finds the video because I can hear the video, but it's not displayed.
Can you try with the bellow code ,it is working for me.
#IBOutlet weak var vlogView: UIView!
var avPlayer = AVPlayer()
var avPlayerLayer: AVPlayerLayer!
#IBAction func playVideo(_ sender: Any) {
let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
avPlayerLayer = AVPlayerLayer(player: avPlayer)
self.avPlayerLayer.frame = self.vlogView.frame
avPlayerLayer.videoGravity = .resizeAspectFill
vlogView.layer.insertSublayer(avPlayerLayer, at: 0)
vlogView.layoutIfNeeded()
let playerItem = AVPlayerItem(url: videoURL!)
avPlayer.replaceCurrentItem(with: playerItem)
avPlayer.play()
}

How to add signed cookies in URL of streaming video?

//MARK:- setting player
fileprivate func setPlayRemoteUrl() {
if playUrl.isEmpty { return }
let asset = AVAsset(url: URL(string: playUrl)!)
playerItem = AVPlayerItem(asset: asset)
player = AVPlayer(playerItem: playerItem)
playerLayer = AVPlayerLayer(player: player)
playerLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
playerLayer?.contentsScale = UIScreen.main.scale
layer.insertSublayer(playerLayer!, at: 0)
}
How to add signed cookies in URL of streaming video?

AVPlayer Not Playing Videos

I'm trying to play video from my parse server I'm getting empty video player And when i print the url i get the correct video url
let thevideo:PFFile! = (self.selectedmsg["file"] as! PFFile)
let url:NSURL = NSURL(string: thevideo.url!)!
let player = AVPlayer(URL: url)
let playercontroller = AVPlayerViewController()
playercontroller.player = player
self.presentViewController(playercontroller, animated: true) {
player.play()
}
Any help? to play videos from my parse.

AVPlayerItem's canPlaySlowForward property never called

I'm trying to enable slow playback rate for a video I'm playing using AVPlayerLayer. To do so the documentation states it will enable rate in the range of 0.0 - 1.0 if the AVPlayerItem returns true for canPlaySlowForward. This property is readonly, so you need to subclass AVPlayerItem and override this property to accomplish this. I've done that, but the video still does not playing at a slow rate, it's always the 1.0 rate. It never even calls the canPlaySlowForward property. Why is this?
import AVFoundation
class SlowMoPlayerItem: AVPlayerItem {
override var canPlaySlowForward: Bool {
return true
}
}
Playing the video:
let asset = AVAsset(URL: NSBundle.mainBundle().URLForResource("some-video", withExtension: "mp4")!)
let playerItem = SlowMoPlayerItem(asset: asset)
let player = AVPlayer(playerItem: playerItem)
player.rate = 0.5
player.muted = true
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.layer.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
The only other thing I do related to video/audio is prevent it from stopping background audio, via:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
}
The video file does not include an audio track but this was still necessary to include.
Set the rate after calling play(). No need to subclass AVPlayerItem.
func playVideo() {
let asset = AVAsset(URL: NSBundle.mainBundle().URLForResource("SampleVideo", withExtension: "mp4")!)
let playerItem = AVPlayerItem(asset: asset)
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
player.rate = 0.5
}

AVQueuePlayer video is not working in Swift

I am currently trying to implement advertisement with video in Swift. I tried to use AVPlayerViewController and AVQueuePlayer. However AVQueuePlayer is not playing the videos after first one finishes. Here is my code:
override func viewDidLoad() {
super.viewDidLoad()
var adItem = AVPlayerItem(URL: NSURL(string: adUrl!))
var videoItem = AVPlayerItem(URL: NSURL(string: videoUrl))
player = AVQueuePlayer(items: NSArray(objects: adItem,videoItem) as [AnyObject])
playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
}
Can you tell me what is my wrong? Any help would be appreciated.
Have you guys tried this?
let quePlayer = AVQueuePlayer.init(items: allVideos)
quePlayer.actionAtItemEnd = AVPlayerActionAtItemEnd.Advance
I have try this and it works with me
let url = NSURL(string: urlString as String)
self.player = AVPlayer(URL: url!)