2016-08-31 2 views
1

In meiner App trimme ich die Audiodatei.AudioFile-Trimmen funktioniert nicht

aber es funktioniert nicht jede Zeit seine zeigt mir

gescheitert

dies ist mein Code

@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 
} 

ich weiß nicht, wo seine schief gehen

hier meinen String Eingabedatei Pfad ist

file:///private/var/mobile/Containers/Data/Application/07166600-AAEA-436F-BE6B-93839C180F19/Documents/Default/recording-2016-08-31-18-28-12.m4a 

Hier ist meine Ausgabedatei Pfad

file:///private/var/mobile/Containers/Data/Application/07166600-AAEA-436F-BE6B-93839C180F19/Documents/Default/2016_Aug_31_18_38_31.m4a 

oder sogar Startzeit und Stoppzeit ist auch perfekt. also wie kann ich das lösen? und lassen Sie mich wissen, ob es noch eine andere Annäherung gibt.

dies ist der Fehler, die ich

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

Was ist der Fehler, den Sie bekommen? –

+0

kein Fehler Kumpel es geht nur in dieser Schleife .Failed == exportSession! .status –

+0

Was ist der Status? –

Antwort

1

Die Linie

exportSession!.outputURL = NSURL.fileURLWithPath(filePath) 

falsch verstanden ist. filePath ist kein Pfad; Es ist bereits eine file:// URL.

Die schnelle Lösung ist es, die outputURL zu, dies zu ändern:

exportSession!.outputURL = NSURL(string: filePath)! 

Es gibt Verwirrung zwischen Dateipfade und Datei URLs in diesem Code. z.B. filepath ist eine Datei NSURL, aber strOutputFilePath123 ist eine String, die eine file:// Schema-URI enthält.

Um eine Datei NSURL in einen String Pfad zu konvertieren, sollten Sie url.path verwenden.

Verwandte Themen