Ios 0超出空NSArray的界限

Ios 0超出空NSArray的界限,ios,swift,Ios,Swift,我试图合并一个音频和一个视频,这将来自NSFileManager。当我尝试时,我犯了一个错误。然后我创建了另一个项目,它是完全好的。但是当我试图在我的主要项目中执行这个时,我得到了一个错误 由于未捕获异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArray0 objectAtIndex:]:索引0超出空NSArray的界限” 以下是我尝试合并的方式: 为了从文档目录中获取数据,我编写了一个名为getData的函数 func getData(){ l

我试图合并一个音频和一个视频,这将来自NSFileManager。当我尝试时,我犯了一个错误。然后我创建了另一个项目,它是完全好的。但是当我试图在我的主要项目中执行这个时,我得到了一个错误

由于未捕获异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArray0 objectAtIndex:]:索引0超出空NSArray的界限”

以下是我尝试合并的方式:

为了从文档目录中获取数据,我编写了一个名为
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)
数组和
aaAudioAsset.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]
使用相同的过程,在通过索引访问数组中的位置之前,还要确保您的
videourlformage
数组和
audioUrl
数组具有一些值

用此代码替换合并测试操作代码

@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 )
    }
我的回答不会帮助您解决为什么您的数组为空的问题,但会帮助您避免在这个问题上崩溃,也许这个问题是由您的
aVideoAsset
aAudioAsset
用于初始化的URL造成的

更新 试着用这种方法让您的
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)

在哪一行发生此错误?在哪一行停止应用程序执行并导致崩溃?我试图使用break poin进行调试。转到此行后,让aaudioasetrack:AVAssetTrack=aaudioaset.tracks(withMediaType:AVMediaTypeAudio)[0]应用程序崩溃。错误说明aaudioaset.tracks(withMediaType:AVMediaTypeAudio)返回的数组为空数组如何解决此问题???@FaizulKarim再次检查我在知道哪一行崩溃后再次更新了答案,请再试一次,让我知道谢谢雷纳·梅利安。错误消失了。但是我的视频没有保存在照片中。但我是用这种方式保存视频的我不知道,不是我,我总是解释一个否决票@FaizulKarimAnother help拜托。。assetsLib.writeVideoAtPath(toSavedPhotosAlbum:VideoFilePath,completionBlock:nil)。。根据我的代码,这个videoFilepaht是字符串格式的。那么我如何将其转换为url AssetLib呢need@FaizulKarim我已经更新了我的答案,请让我知道是否适合你