Can't play video from URL using AVPlayer, Error: NSOSStatusErrorDomain Code=-2
Can't play video from URL using AVPlayer, Error: NSOSStatusErrorDomain Code=-2
我使用库 https://github.com/VeinGuo/VGPlayer 在我的自定义视图中播放视频。但是我遇到了错误
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-2), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x281933e40 {Error Domain=NSOSStatusErrorDomain Code=-2 "(null)"}}
我刚刚在 GitHub 示例的项目中实现了 Embed in cell of tableView。由于出现错误原因,我刚刚创建了新的空项目并在其中做了同样的事情,我感到震惊它在其中工作正常(相同的设备,相同的 xcode)。我已经研究了很多,但我找不到我的项目中的实际问题。请帮助我!
我用thisurl来播放视频。它在我的新项目中运行良好,但在我的实际项目中运行不正常。
下面是 VGEmbedTableViewController
文件中的 addPlayer
方法。其他代码与 gitHub 存储库的示例相同。我只改变了 url.
func addPlayer(_ cell: UITableViewCell) {
if player != nil {
player.cleanPlayer()
}
configurePlayer()
cell.contentView.addSubview(player.displayView)
player.displayView.snp.makeConstraints {
[=10=].edges.equalTo(cell)
}
player.replaceVideo(URL(string:"https://s3.eu-west-2.amazonaws.com/gymstar-app/7A77786B4870594D7165625046614E74/post_videos/postVideo1582781434.005436.mp4")!)
player.play()
}
我使用了简单的 swift 代码来播放内容。
func play() {
// Get AVPlayerItem
let url = URL(string:"https://s3.eu-west-2.amazonaws.com/gymstar-app/7A77786B4870594D7165625046614E74/post_videos/postVideo1582781434.005436.mp4")
//let url = URL(string:"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8")
self.asset = AVURLAsset(url: url!)
self.playerItem = AVPlayerItem(asset: asset!)
// Get AVPlayer
self.player = AVPlayer(playerItem: self.playerItem)
self.playerViewController = AVPlayerViewController()
self.playerViewController!.player = self.player;
self.present(self.playerViewController!, animated: true) {
self.player?.play()
}
}
我的问题已通过清除由 VGPlayer
库保存的缓存解决。他们提供了清除缓存和删除文件的方法,
open func cleanAllCache() {
ioQueue.sync {
do {
let cacheDirectory = VGPlayerCacheManager.cacheDirectory()
try fileManager.removeItem(atPath: cacheDirectory)
} catch { }
}
}
open func cleanOldFiles(completion handler: (()->())? = nil) {
ioQueue.sync {
let cacheDirectory = VGPlayerCacheManager.cacheDirectory()
var (URLsToDelete, diskCacheSize, cachedFiles) = self.cachedFiles(atPath: cacheDirectory, onlyForCacheSize: false)
for fileURL in URLsToDelete {
do {
try fileManager.removeItem(at: fileURL)
} catch _ { }
}
if cacheConfig.maxCacheSize > 0 && diskCacheSize > cacheConfig.maxCacheSize {
let targetSize = cacheConfig.maxCacheSize / 2
let sortedFiles = cachedFiles.keysSortedByValue {
resourceValue1, resourceValue2 -> Bool in
if let date1 = resourceValue1.contentAccessDate,
let date2 = resourceValue2.contentAccessDate
{
return date1.compare(date2) == .orderedAscending
}
return true
}
for fileURL in sortedFiles {
let (_, cacheSize, _) = self.cachedFiles(atPath: fileURL.path, onlyForCacheSize: true)
diskCacheSize -= cacheSize
do {
try fileManager.removeItem(at: fileURL)
} catch { }
URLsToDelete.append(fileURL)
if diskCacheSize < targetSize {
break
}
}
}
DispatchQueue.main.async {
if URLsToDelete.count != 0 {
let cleanedHashes = URLsToDelete.map { [=10=].lastPathComponent }
NotificationCenter.default.post(name: .VGPlayerCacheManagerDidCleanCache, object: self, userInfo: [VGPlayerCacheManager.VGPlayerCleanCacheKey: cleanedHashes])
}
handler?()
}
}
}
我使用库 https://github.com/VeinGuo/VGPlayer 在我的自定义视图中播放视频。但是我遇到了错误
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-2), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x281933e40 {Error Domain=NSOSStatusErrorDomain Code=-2 "(null)"}}
我刚刚在 GitHub 示例的项目中实现了 Embed in cell of tableView。由于出现错误原因,我刚刚创建了新的空项目并在其中做了同样的事情,我感到震惊它在其中工作正常(相同的设备,相同的 xcode)。我已经研究了很多,但我找不到我的项目中的实际问题。请帮助我!
我用thisurl来播放视频。它在我的新项目中运行良好,但在我的实际项目中运行不正常。
下面是 VGEmbedTableViewController
文件中的 addPlayer
方法。其他代码与 gitHub 存储库的示例相同。我只改变了 url.
func addPlayer(_ cell: UITableViewCell) {
if player != nil {
player.cleanPlayer()
}
configurePlayer()
cell.contentView.addSubview(player.displayView)
player.displayView.snp.makeConstraints {
[=10=].edges.equalTo(cell)
}
player.replaceVideo(URL(string:"https://s3.eu-west-2.amazonaws.com/gymstar-app/7A77786B4870594D7165625046614E74/post_videos/postVideo1582781434.005436.mp4")!)
player.play()
}
我使用了简单的 swift 代码来播放内容。
func play() {
// Get AVPlayerItem
let url = URL(string:"https://s3.eu-west-2.amazonaws.com/gymstar-app/7A77786B4870594D7165625046614E74/post_videos/postVideo1582781434.005436.mp4")
//let url = URL(string:"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8")
self.asset = AVURLAsset(url: url!)
self.playerItem = AVPlayerItem(asset: asset!)
// Get AVPlayer
self.player = AVPlayer(playerItem: self.playerItem)
self.playerViewController = AVPlayerViewController()
self.playerViewController!.player = self.player;
self.present(self.playerViewController!, animated: true) {
self.player?.play()
}
}
我的问题已通过清除由 VGPlayer
库保存的缓存解决。他们提供了清除缓存和删除文件的方法,
open func cleanAllCache() {
ioQueue.sync {
do {
let cacheDirectory = VGPlayerCacheManager.cacheDirectory()
try fileManager.removeItem(atPath: cacheDirectory)
} catch { }
}
}
open func cleanOldFiles(completion handler: (()->())? = nil) {
ioQueue.sync {
let cacheDirectory = VGPlayerCacheManager.cacheDirectory()
var (URLsToDelete, diskCacheSize, cachedFiles) = self.cachedFiles(atPath: cacheDirectory, onlyForCacheSize: false)
for fileURL in URLsToDelete {
do {
try fileManager.removeItem(at: fileURL)
} catch _ { }
}
if cacheConfig.maxCacheSize > 0 && diskCacheSize > cacheConfig.maxCacheSize {
let targetSize = cacheConfig.maxCacheSize / 2
let sortedFiles = cachedFiles.keysSortedByValue {
resourceValue1, resourceValue2 -> Bool in
if let date1 = resourceValue1.contentAccessDate,
let date2 = resourceValue2.contentAccessDate
{
return date1.compare(date2) == .orderedAscending
}
return true
}
for fileURL in sortedFiles {
let (_, cacheSize, _) = self.cachedFiles(atPath: fileURL.path, onlyForCacheSize: true)
diskCacheSize -= cacheSize
do {
try fileManager.removeItem(at: fileURL)
} catch { }
URLsToDelete.append(fileURL)
if diskCacheSize < targetSize {
break
}
}
}
DispatchQueue.main.async {
if URLsToDelete.count != 0 {
let cleanedHashes = URLsToDelete.map { [=10=].lastPathComponent }
NotificationCenter.default.post(name: .VGPlayerCacheManagerDidCleanCache, object: self, userInfo: [VGPlayerCacheManager.VGPlayerCleanCacheKey: cleanedHashes])
}
handler?()
}
}
}