当解包一个Optional值时,Swift无法将AVAsset音频文件导出到其他名称,并且意外发现错误为nil

时间:2016-11-27 09:48:18

标签: ios swift avaudiorecorder

我正在尝试将录制的音频中的音频文件导出到其他音频名称,然后尝试将录制的音频转换为不同的音高,但我最终无法导出音频文件,它一直给我提供可选的nil错误可以任意你们这些人帮我解决了这个问题吗?

let asset = AVURLAsset(url: audioFile.url)
        print(audioFile.url)

        let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A)

        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let trimmedSoundFileURL = documentsDirectory.appendingPathComponent("trimmed10.m4a")

        try! FileManager.default.createDirectory(at: trimmedSoundFileURL, withIntermediateDirectories: true, attributes: nil)

        let fileManager = FileManager.default
        print(trimmedSoundFileURL.path)
        if fileManager.fileExists(atPath: trimmedSoundFileURL.path) {
            print("File exists")
            do {
                try fileManager.removeItem(at: trimmedSoundFileURL)
            }
            catch let error as NSError {
                print("Ooops! Something went wrong: \(error)")
            }
        } else {
            print("File doesn't exists")
        }

        exporter?.outputURL = trimmedSoundFileURL
        exporter?.outputFileType = AVFileTypeAppleM4A




        print(trimmedSoundFileURL)


        exporter?.exportAsynchronously(completionHandler: {
            switch exporter?.status {
            case AVAssetExportSessionStatus.completed?:
                self.url1! = trimmedSoundFileURL

                print("export complete")
            case  AVAssetExportSessionStatus.failed?:
                print("export failed \(exporter!.error)")
            case AVAssetExportSessionStatus.cancelled?:
                print("export cancelled \(exporter!.error)")
            default:
                print("export complete")
            }
        })


        do{
            print(self.url1!)
            let newaudioFile = try AVAudioFile(forWriting: self.url1!, settings:  [
                AVFormatIDKey: NSNumber(value:kAudioFormatMPEG4AAC),
                AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue,
                AVEncoderBitRateKey : 320000,
                AVNumberOfChannelsKey: 2,
                AVSampleRateKey : 44100.0
                ])

            audioEngine.mainMixerNode.installTap(onBus: 0, bufferSize: 2048, format: self.audioEngine.mainMixerNode.inputFormat(forBus: 0)) {
                (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in

                print( newaudioFile.length)
                print("=====================")
                print( newaudioFile.length)
                print("**************************")
                if ( newaudioFile.length) < ( newaudioFile.length){//Let us know when to stop saving the file, otherwise saving infinitely

                    do{
                        //print(buffer)
                        try  newaudioFile.write(from: buffer)
                    }catch _{
                        print("Problem Writing Buffer")
                    }
                }else{
                    self.audioEngine.mainMixerNode.removeTap(onBus: 0)//if we dont remove it, will keep on tapping infinitely

                }

            }
        }catch _{
            print("Problem")
        }

0 个答案:

没有答案