我试图使用HLS直播但我收到错误:
错误:可选("操作无法完成"),错误: 可选(错误域= AVFoundationErrorDomain代码= -11800"媒体 格式 - 样本描述无效(例如无效大小)" UserInfo = {NSUnderlyingError = 0x60000005b510 {错误 Domain = NSOSStatusErrorDomain Code = -12714"(null)"}, NSLocalizedFailureReason =发生未知错误(-12714), NSDebugDescription =媒体格式 - 样本描述无效(例如 无效大小),NSLocalizedDescription =操作不能 完成})2018-04-25 12:14:51.608117 + 0200 morethen2 [11681:374192] 任务。< 3>完成错误 - 代码:-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应用和网络
答案 0 :(得分:1)
我将我的答案留在这里,以防万一其他人需要它。经过研究,尝试和错误,我终于找到了使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有两个应用程序:一个用于接收上游,另一个用于对HLS进行转码。
为解决元数据问题,我通过添加-profile:v baseline -level 3.0
来调整ffmpeg转码命令的基线配置文件。由于错误的颜色格式,这将导致错误,因此我还添加了:-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()