【问题标题】:0 beyond bounds for empty NSArray0 超出范围为空 NSArray
【发布时间】:2017-08-27 03:10:03
【问题描述】:

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

由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty 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 )
}

我在堆栈溢出中跟踪了一些代码,但得到了这个错误。

【问题讨论】:

  • 这个错误发生在哪一行?
  • 哪一行因崩溃而停止您的应用程序执行?
  • 我尝试使用断点进行调试。转到此行后让 aAudioAssetTrack : AVAssetTrack = aAudioAsset.tracks(withMediaType: AVMediaTypeAudio)[0] app crash.
  • 错误表示 aAudioAsset.tracks(withMediaType: AVMediaTypeAudio) 返回的数组是空数组
  • 怎么解决??

标签: ios swift


【解决方案1】:

在通过索引访问数组中的位置之前,确保您的 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 )
    }

我的回答不会帮助您为什么您的阵列是空的问题,但可以帮助您不要在此问题上崩溃,也许该问题是由您的 aVideoAssetaAudioAsset 用于初始化的网址

更新

试试这个方法让你的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。错误消失了。但是我的视频没有保存在照片中。但我是这样保存视频的
  • 我不知道,不是我,我总是解释否决票@FaizulKarim
  • 请提供其他帮助 .. assetsLib.writeVideoAtPath(toSavedPhotosAlbum: VideoFilePath, completionBlock: nil) .. 根据我的代码,此 videoFilepaht 是字符串格式。那么我怎样才能将它转换为 AssetsLib 需要的 url
  • @FaizulKarim 我已经更新了我的答案,如果适合你请告诉我
猜你喜欢
  • 2018-04-11
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 2012-10-26
  • 2020-12-10
  • 2011-07-10
相关资源
最近更新 更多