在我的应用程序中,我修剪了音频文件。
但是它不能每次都显示我失败
这是我的代码
@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)"}})
答案 0 :(得分:1)
该行
exportSession!.outputURL = NSURL.fileURLWithPath(filePath)
错了。 filePath
不是一条路;它已经是file://
网址。
快速解决方法是将outputURL
更改为:
exportSession!.outputURL = NSURL(string: filePath)!
此代码中的文件路径和文件URL之间存在混淆。例如filepath
是文件NSURL
,但strOutputFilePath123
是String
,其中包含file://
方案URI。
要将文件NSURL
转换为String
路径,您应该使用url.path
。