在我的应用程序中,我修剪音频文件。

但它每次都无法正常工作,因此无法显示

这是我的代码

 @IBAction func trimAudioPressed(sender: AnyObject) {

    isTrim = true

    let audioFileInput = filepath

    let date = NSDate()
    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy_MMM_dd_HH_mm_ss"
    let strdate = formatter.stringFromDate(date)

    print(NSUserDefaults.standardUserDefaults().valueForKey("fileURL"))

    let extensionofFile = filepath.pathExtension!

    let strOutputFilePath123 : String = String(filepath.URLByDeletingLastPathComponent!)

    strOutputFilePath = "\(strOutputFilePath123)\(strdate).\(extensionofFile)"

    let fileManger = NSFileManager.defaultManager()
    if fileManger.fileExistsAtPath(strOutputFilePath) {
        do {
            (try fileManger.removeItemAtPath(strOutputFilePath))
        }
        catch let error { print(error)
        }
    }

    let audioFileOutput : NSURL = NSURL(string: strOutputFilePath)!

    let asset = AVAsset(URL: audioFileInput)

    let succcess = self.exportAsset(asset, toFilePath: strOutputFilePath)
    print(succcess)

    self.dismissView()

}



func exportAsset(avAsset: AVAsset, toFilePath filePath: String) -> Bool {
    var tracks = avAsset.tracksWithMediaType(AVMediaTypeAudio)
    if tracks.count == 0 {
        return false
    }
    let track = tracks[0]

    let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetAppleM4A)

    let supportedTypeArray = exportSession!.supportedFileTypes
    for str: String in supportedTypeArray {

        if nil == exportSession {
            return false
        }
    }
    print(leftV)
    print(rightV)
    let startTime = CMTimeMake(Int64(leftV), 1)
    let stopTime = CMTimeMake(Int64(rightV), 1)

    let exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime)
    let exportAudioMix = AVMutableAudioMix()

     let exportAudioMixInputParameters = AVMutableAudioMixInputParameters(track: track)

    exportAudioMix.inputParameters = [exportAudioMixInputParameters]
    exportSession!.outputURL = NSURL.fileURLWithPath(filePath)

    exportSession!.outputFileType = AVFileTypeAppleM4A

    exportSession!.timeRange = exportTimeRange

    exportSession!.audioMix = exportAudioMix

    exportSession!.exportAsynchronouslyWithCompletionHandler({() -> Void in
        if .Completed == exportSession!.status {
            print("Success")

        }
        else if .Failed == exportSession!.status {
            print("Failed")
            print("Export Session Status: \(exportSession!.status)")

        }
        else {

            print("Export Session Status: \(exportSession!.status)")
        }

    })
    return true
}

我不知道哪里出了问题

这是我的字符串输入文件路径
file:///private/var/mobile/Containers/Data/Application/07166600-AAEA-436F-BE6B-93839C180F19/Documents/Default/recording-2016-08-31-18-28-12.m4a

这是我的输出文件路径
file:///private/var/mobile/Containers/Data/Application/07166600-AAEA-436F-BE6B-93839C180F19/Documents/Default/2016_Aug_31_18_38_31.m4a

甚至开始时间和停止时间也很完美。所以我该怎么解决。如果有其他办法,请让我知道。

这是我得到的错误
export failed Optional(Error Domain=NSURLErrorDomain Code=-3000 "Cannot create file" UserInfo={NSLocalizedDescription=Cannot create file, NSUnderlyingError=0x7fc9415142b0 {Error Domain=NSOSStatusErrorDomain Code=-12115 "(null)"}})

最佳答案

线

exportSession!.outputURL = NSURL.fileURLWithPath(filePath)

是错的。 filePath不是路径;它已经是一个file:// URL。

快速解决方案是将outputURL更改为此:
exportSession!.outputURL = NSURL(string: filePath)!

在此代码中,文件路径和文件URL之间存在混淆。例如filepath是文件NSURL,但是strOutputFilePath123是包含String方案URI的file://

要将文件NSURL转换为String路径,应使用url.path

关于ios - AudioFile修剪不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39250765/

10-13 04:07