0超出空NSArray的界限

时间:2017-08-27 03:10:03

标签: ios swift

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

  

由于未捕获的异常终止应用程序' 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 )
}

我正在关注堆栈溢出中的一些代码,但是遇到了这个错误。

1 个答案:

答案 0 :(得分:2)

确保您的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 )
    }

我的回答无法帮助您解决为什么您的阵营存在问题,但可以帮助您避免崩溃,也许这个问题是由您{{1}造成的}和aVideoAsset用于初始化的网址

更新

尝试这种方式让aAudioAsset正常工作

savePathUrl