I'm trying to use HLS to live stream but i get error:
error: Optional("The operation could not be completed"), error: Optional(Error Domain=AVFoundationErrorDomain Code=-11800 "Media format - sample description is invalid (e.g. invalid size)" UserInfo={NSUnderlyingError=0x60000005b510 {Error Domain=NSOSStatusErrorDomain Code=-12714 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-12714), NSDebugDescription=Media format - sample description is invalid (e.g. invalid size), NSLocalizedDescription=The operation could not be completed}) 2018-04-25 12:14:51.608117+0200 morethen2[11681:374192] Task .<3> finished with error - code: -999
I can't share link to stream, its private.
Here is code:
class ViewController: UIViewController {
var player = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https:can-t-share-it/LIVE-008900021A-LIP-0-channelNo2_360p/manifest.m3u8")!
let asset = AVURLAsset(url: url)
let playerItem = AVPlayerItem(asset: asset)
player = AVPlayer(url: url)
let layer = AVPlayerLayer(player: player)
layer.frame = view.layer.frame
view.layer.addSublayer(layer)
self.player.addObserver(self, forKeyPath: #keyPath(AVPlayer.status), options: [.new, .initial], context: nil)
self.player.addObserver(self, forKeyPath: #keyPath(AVPlayer.currentItem.status), options:[.new, .initial], context: nil)
// Watch notifications
let center = NotificationCenter.default
center.addObserver(self, selector:"newErrorLogEntry:", name: .AVPlayerItemNewErrorLogEntry, object: player.currentItem)
center.addObserver(self, selector:"failedToPlayToEndTime:", name: .AVPlayerItemFailedToPlayToEndTime, object: player.currentItem)
player.play()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func dismiss(_ sender: Any) {
UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: true, completion: nil)
}
// Observe If AVPlayerItem.status Changed to Fail
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
if let player = object as? AVPlayer, keyPath == #keyPath(AVPlayer.currentItem.status) {
let newStatus: AVPlayerItemStatus
if let newStatusAsNumber = change?[NSKeyValueChangeKey.newKey] as? NSNumber {
newStatus = AVPlayerItemStatus(rawValue: newStatusAsNumber.intValue)!
} else {
newStatus = .unknown
}
if newStatus == .failed {
NSLog("Error: \(String(describing: player.currentItem?.error?.localizedDescription)), error: \(String(describing: player.currentItem?.error))")
}
}
}
// Getting error from Notification payload
func newErrorLogEntry(_ notification: Notification) {
guard let object = notification.object, let playerItem = object as? AVPlayerItem else {
return
}
guard let errorLog: AVPlayerItemErrorLog = playerItem.errorLog() else {
return
}
NSLog("Error: \(errorLog)")
}
func failedToPlayToEndTime(_ notification: Notification) {
let error = notification.userInfo!["AVPlayerItemFailedToPlayToEndTimeErrorKey"]
NSLog("error: \(error)")
} }
Also same stream works on Android apps and on web