0超越界限空NSArray
我试图合并一个音频和一个视频,这将来自NSFileManager。当我尝试时,我得到了一个错误。然后我创建了另一个项目,它完全没问题。但是,当我尝试在我的主项目中执行此操作时,出现错误。0超越界限空NSArray
终止应用程序由于未捕获的异常 'NSRangeException',原因: '*** - [__ NSArray0 objectAtIndex:]:索引0超越边界空的NSArray'
以下是我尝试合并:
要从Document目录中获取数据,我写了一个名为getData
的函数。
func getData(){
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
// Get the directory contents urls (including subfolders urls)
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: [])
print(directoryContents)
// if you want to filter the directory contents you can do like this:
videoUrlforMarge = directoryContents.filter{ $0.pathExtension == "mov" } as [AnyObject]
//videoUrlforMarge.append(directoryContents[1] as AnyObject)
print("this video \(videoUrlforMarge[0])")
audioUrl = directoryContents.filter{ $0.pathExtension == "caf" } as [AnyObject]
} catch let error as NSError {
print(error.localizedDescription)
}
}
这里是我的合并功能:
func mergeFilesWithUrl(videoUrl:NSURL, audioUrl:NSURL)
{
let mixComposition : AVMutableComposition = AVMutableComposition()
var mutableCompositionVideoTrack : [AVMutableCompositionTrack] = []
var mutableCompositionAudioTrack : [AVMutableCompositionTrack] = []
let totalVideoCompositionInstruction : AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
//start merge
let aVideoAsset : AVAsset = AVAsset(url: videoUrl as URL)
let aAudioAsset : AVAsset = AVAsset(url: audioUrl as URL)
mutableCompositionVideoTrack.append(mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid))
mutableCompositionAudioTrack.append(mixComposition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid))
let aVideoAssetTrack : AVAssetTrack = aVideoAsset.tracks(withMediaType: AVMediaTypeVideo)[0]
let aAudioAssetTrack : AVAssetTrack = aAudioAsset.tracks(withMediaType: AVMediaTypeAudio)[0]
do{
try mutableCompositionVideoTrack[0].insertTimeRange(CMTimeRangeMake(kCMTimeZero, aVideoAssetTrack.timeRange.duration), of: aVideoAssetTrack, at: kCMTimeZero)
try mutableCompositionAudioTrack[0].insertTimeRange(CMTimeRangeMake(kCMTimeZero, aVideoAssetTrack.timeRange.duration), of: aAudioAssetTrack, at: kCMTimeZero)
}catch{
}
totalVideoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero,aVideoAssetTrack.timeRange.duration)
let mutableVideoComposition : AVMutableVideoComposition = AVMutableVideoComposition()
mutableVideoComposition.frameDuration = CMTimeMake(1, 30)
mutableVideoComposition.renderSize = CGSize(width: 1280, height: 720)
// playerItem = AVPlayerItem(asset: mixComposition)
// player = AVPlayer(playerItem: playerItem!)
//
//
// AVPlayerVC.player = player
// let fm = FileManager.default
// let docsurl = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
// let savePathUrl = docsurl.appendingPathComponent("temp.mp4")
//find your video on this URl
//let savePathUrl : NSURL = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/newVideo.mp4")
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let savePathUrl = NSURL(string: ("\(documentsURL.appendingPathComponent("temp"))" + ".mp4"))
let VideoFilePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("mergeVideo\(arc4random()%1000)d")!.appendingPathExtension("mp4").absoluteString
if FileManager.default.fileExists(atPath: VideoFilePath)
{
do
{
try FileManager.default.removeItem(atPath: VideoFilePath)
}
catch { }
}
let assetExport: AVAssetExportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!
assetExport.outputFileType = AVFileTypeMPEG4
assetExport.outputURL = NSURL(fileURLWithPath: VideoFilePath) as URL
assetExport.shouldOptimizeForNetworkUse = true
assetExport.exportAsynchronously {() -> Void in
switch assetExport.status {
case AVAssetExportSessionStatus.completed:
//Uncomment this if u want to store your video in asset
let assetsLib = ALAssetsLibrary()
assetsLib.writeVideoAtPath(toSavedPhotosAlbum: savePathUrl! as URL, completionBlock: nil)
print("success")
case AVAssetExportSessionStatus.failed:
print("failed \(String(describing: assetExport.error))")
case AVAssetExportSessionStatus.cancelled:
print("cancelled \(String(describing: assetExport.error))")
default:
print("complete")
}
}
}
最后我试图通过诉讼合并,这里是它。
@IBAction func margeTest(_ sender: Any) {
let videoUrl = videoUrlforMarge[0]
let url = NSURL(fileURLWithPath: videoUrl.absoluteString!!)
let audio = audioUrl[0]
let urla = NSURL(fileURLWithPath: audio.absoluteString!!)
self.mergeFilesWithUrl(videoUrl: url , audioUrl: urla)
}
我在栈溢出一些代码,但得到了这个错误。
确保您aVideoAsset.tracks(withMediaType: AVMediaTypeVideo)
阵列和aAudioAsset.tracks(withMediaType: AVMediaTypeAudio)
数组访问位置由索引数组中之前的一些价值,附加保护可以帮你这个
guard aVideoAsset.tracks(withMediaType: AVMediaTypeVideo).count > 0 && aAudioAsset.tracks(withMediaType: AVMediaTypeAudio) > 0 else{
return
}
let aVideoAssetTrack : AVAssetTrack = aVideoAsset.tracks(withMediaType: AVMediaTypeVideo)[0]
let aAudioAssetTrack : AVAssetTrack = aAudioAsset.tracks(withMediaType: AVMediaTypeAudio)[0]
另外,还要确保您videoUrlforMarge
阵列和audioUrl
阵列存取位置由索引阵列中之前有一定的价值,使用相同的程序
由这一个
取代您mergeTest动作代码@IBAction func margeTest(_ sender: Any) {
guard videoUrlforMarge.count > 0 && audioUrl.count > 0 else{
return
}
let videoUrl = videoUrlforMarge[0]
let url = NSURL(fileURLWithPath: videoUrl.absoluteString!!)
let audio = audioUrl[0]
let urla = NSURL(fileURLWithPath: audio.absoluteString!!)
self.mergeFilesWithUrl(videoUrl: url , audioUrl: urla)
}
我的回答会不会帮你为什么是你的阵列EMPTY问题但可以帮助你不死机这一点,也许这个问题是由用于初始化您aVideoAsset
和aAudioAsset
造成的网址
更新
试试这个办法让你savePathUrl
工作
let filename = "temp.mp4"
let documentsURL = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!)
let savePath = documents.appendingPathComponent(fileName).path
let savePathUrl = URL(fileURLWithPath: savePath)
它不适合我。同样的错误。 –
@FaizulKarim再次检查我知道哪一行崩溃后再次更新了我的答案,请再试一次,让我知道 –
谢谢Reiner melian。错误消失了。但是我的视频并没有保存在照片中。但我是把视频保存这样 –
在哪一行发生此错误? – 3stud1ant3
其中哪行停止应用程序执行并崩溃? –
我正在尝试使用中断指针进行调试。在转到此行后,让aAudioAssetTrack:AVAssetTrack = aAudioAsset.tracks(withMediaType:AVMediaTypeAudio)[0]应用程序崩溃。 –