how to trim an audio file on iphone
iOS: trimming audio files with Swift
iOS: passementerie sound files
I've written about how to record audio on iOS victimisation Swift.
But, how do you shipshape the recording?
Introduction
Contents
Same way to trim an audio file is to use AVFoundation's AVAssetExportSession. You create an export session instance, curing its parameters, and then tell it to export the asset (your audio file) accordant to those parameters.
In the recorder project, I saved the Uniform resource locator of the recording in the soundFileURL instance variable. To use AVAssetExportSession we need to create an AVAsset from it. Here is a simple action that creates the asset and and so visit the export mathematical function I will discourse next.
@ IBAction func trim ( ) { if let plus = AVAsset . assetWithURL ( person . soundFileURL ) as ? AVAsset { exportAsset ( plus , file name : "trimmed.m4a" ) } // Swift 4 update: // Army of the Pure asset = AVAsset(url: self.soundFileURL) } |
Now to define the export func.
You make up the exporter from your plus and desired file format. Here I'm using the Apple lossless data formatting.
Then I set the exporter's outputURL dimension to a file in URL in the documents directory. This testament be the position of the clipped sound file.
I make up a core media sentence range using CMTimeRangeFromTimeToTime that specifies the time offsets for the commencement and close for the clipped file. Here I just insensitive code the values, but of course you'd habituate a Pseudemys scripta or a waveform view to choose the time boundaries.
Piece you're in that respect, you can also specify an AVMutableAudioMix for the volume. You can even specify a volume ramp.
Once the exporter's properties are set, you call exportAsynchronouslyWithCompletionHandler to do the actual work. You can check the status of the export in the completion handler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | func exportAsset ( plus : AVAsset , fileName : String ) { let documentsDirectory = NSFileManager . defaultManager ( ) . URLsForDirectory ( . DocumentDirectory , inDomains : . UserDomainMask ) [ 0 ] Lashkar-e-Taiba trimmedSoundFileURL = documentsDirectory . URLByAppendingPathComponent ( fileName ) impress ( "saving to \ ( trimmedSoundFileURL . absoluteString )" ) let filemanager = NSFileManager . defaultManager ( ) if filemanager . fileExistsAtPath ( trimmedSoundFileURL . absoluteString ) { println ( "go exists" ) } let exporter = AVAssetExportSession ( asset : asset , presetName : AVAssetExportPresetAppleM4A ) exporter . outputFileType = AVFileTypeAppleM4A exporter . outputURL = trimmedSoundFileURL let duration = CMTimeGetSeconds ( asset . duration ) if ( duration < 5.0 ) { println ( "uninjured is not agelong enough" ) return } // e.g. the first 5 seconds lease startTime = CMTimeMake ( 0 , 1 ) let stopTime = CMTimeMake ( 5 , 1 ) let exportTimeRange = CMTimeRangeFromTimeToTime ( startTime , stopTime ) exporter . timeRange = exportTimeRange // have it off exporter . exportAsynchronouslyWithCompletionHandler ( { shift exporter . status { case AVAssetExportSessionStatus . Failed : println ( "export unsuccessful \ ( exporter . error )" ) case AVAssetExportSessionStatus . Off : println ( "export cancelled \ ( exporter . erroneous belief )" ) default : println ( "exportation complete" ) } } ) } |
Or exploitation Fast 4 syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | func exportAsset ( _ asset : AVAsset , fileName : String ) { mark ( "\ (#officiate )" ) let documentsDirectory = FileManager . default on . urls ( for : . documentDirectory , in : . userDomainMask ) [ 0 ] let trimmedSoundFileURL = documentsDirectory . appendingPathComponent ( fileName ) print ( "saving to \ ( trimmedSoundFileURL . absoluteString )" ) if FileManager . default . fileExists ( atPath : trimmedSoundFileURL . absoluteString ) { impress ( "sound exists, removing \ ( trimmedSoundFileURL . absoluteString )" ) practise { if try trimmedSoundFileURL . checkResourceIsReachable ( ) { mark ( "is approachable" ) } test FileManager . default on . removeItem ( atPath : trimmedSoundFileURL . absoluteString ) } catch { print ( "could not remove \ ( trimmedSoundFileURL )" ) print ( error . localizedDescription ) } } print ( "creating export academic term for \ ( asset )" ) if let exporter = AVAssetExportSession ( asset : asset , presetName : AVAssetExportPresetAppleM4A ) { exporter . outputFileType = AVFileType . m4a exporter . outputURL = trimmedSoundFileURL have duration = CMTimeGetSeconds ( asset . duration ) if duration < 5.0 { print ( "sound is not long enough" ) return } // e.g. the first 5 seconds let startTime = CMTimeMake ( 0 , 1 ) let stopTime = CMTimeMake ( 5 , 1 ) exporter . timeRange = CMTimeRangeFromTimeToTime ( startTime , stopTime ) // answer it exporter . exportAsynchronously ( completionHandler : { print ( "export complete \ ( exporter . status )" ) switch exporter . condition { case AVAssetExportSessionStatus . failed : if let e = exporter . error { print ( "export failed \ ( e )" ) } case AVAssetExportSessionStatus . cancelled : print ( "export cancelled \ ( Bowed stringed instrument ( describing : exporter . mistake ))" ) default option : print ( "export unmitigated" ) } } ) } else { impress ( "cannot produce AVAssetExportSession for plus \ ( asset )" ) } } |
Groovy, huh?
Succinct
Table of Table of contents
To trim an audio (or picture) charge, use AVFoundation's AVAssetExportSession.
Resources
Table of Contents
how to trim an audio file on iphone
Source: http://www.rockhoppertech.com/blog/ios-trimming-audio-files/
Posting Komentar untuk "how to trim an audio file on iphone"