我正在尝试使用HLS直播视频,但出现错误:
错误:可选(“操作无法完成”),错误:
可选(错误域= AVFoundationErrorDomain代码= -11800“媒体
格式-样本说明无效(例如大小无效)”
UserInfo = {NSUnderlyingError = 0x60000005b510 {Error
Domain = NSOSStatusErrorDomain Code = -12714“(null)”},
NSLocalizedFailureReason =发生未知错误(-12714),
NSDebugDescription =媒体格式-样本说明无效(例如
大小无效),NSLocalizedDescription =无法执行该操作
已完成})2018-04-25 12:14:51.608117 + 0200 morethen2 [11681:374192]
任务。完成并出现错误-
码:-999
我无法分享链接以进行私密直播。
这是代码:
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)")
} }
以下是视频详情:
同样的流也可以在Android应用和网络上使用
最佳答案
我将答案留在这里,以防万一其他人需要它。经过研究,尝试和错误,我终于找到了使NVP rtmp hls与AVPlayer一起工作的方法。
发生这种情况的主要原因是服务器上视频片段的元数据错误。我们必须找到一种解决方法。
这是我从我的Macbook到上游视频的命令,通常是:
ffmpeg -f avfoundation -framerate 30 -i "0:0" -f flv -s 360x240 -vcodec libx264 -c:a aac -async 1 -vsync 1 -preset slower rtmp://127.0.0.1/live/xyz
在服务器端,我在Ubuntu 18.10上配置了一个基本的Nginx RTMP HLS,可以通过谷歌搜索找到它。请注意,我使用fdk_aac重新编译了最新的ffmpeg。
RTMP有2个应用程序:一个用于接收上游,另一个用于对HLS进行转码(这也是典型的)。
为了解决元数据问题,我通过添加
-profile:v baseline -level 3.0
来对ffmpeg transcode命令进行了基线配置调整。由于错误的颜色格式,这将导致错误,因此我还添加了:-vf "scale=480:trunc(ow/a/2)*2,format=yuv420p"
。最后,我的nginx配置有效:
https://gist.github.com/chung-nguyen/d88e73e3cc8788878f5ffb8c232b4729
以及我的AVPlayer代码:
let videoURL = URL(string: "http://blogchange.live/hls/xyz_low/index.m3u8")
let playerItem = AVPlayerItem(url: videoURL!)
let adID = AVMetadataItem.identifier(forKey: "X-TITLE", keySpace: .hlsDateRange)
let metadataCollector = AVPlayerItemMetadataCollector(identifiers: [adID!.rawValue], classifyingLabels: nil)
metadataCollector.setDelegate(self, queue: DispatchQueue.main)
playerItem.add(metadataCollector)
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
self.player = player
player.play()
关于ios - AVPlayer HLS实时流IOS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50020778/