Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- var audio1 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Buildup1", ofType: "wav")!)!
- var audio2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Verse1", ofType: "wav")!)!
- var audio3 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Drop1", ofType: "wav")!)!
- self.concatenate(audio1, audio2Url: audio2, audio3Url: audio3)
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- func concatenate(audio1Url: NSURL, audio2Url: NSURL, audio3Url: NSURL) {
- var error:NSError?
- var ok1 = false
- var ok2 = false
- var ok3 = false
- //var documentsDirectory:String = paths[0] as! String
- //Create AVMutableComposition Object.This object will hold our multiple AVMutableCompositionTrack.
- var composition = AVMutableComposition()
- var compositionAudioTrack:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
- //var compositionAudioTrack2:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
- //create new file to receive data
- var documentDirectoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as! NSURL
- var fileDestinationUrl = documentDirectoryURL.URLByAppendingPathComponent("resultmerge.wav")
- println(fileDestinationUrl)
- var avAsset1 = AVURLAsset(URL: audio1Url, options: nil)
- var avAsset2 = AVURLAsset(URL: audio2Url, options: nil)
- var avAsset3 = AVURLAsset(URL: audio3Url, options: nil)
- var tracks1 = avAsset1.tracksWithMediaType(AVMediaTypeAudio)
- var tracks2 = avAsset2.tracksWithMediaType(AVMediaTypeAudio)
- var tracks3 = avAsset3.tracksWithMediaType(AVMediaTypeAudio)
- var assetTrack1:AVAssetTrack = tracks1[0] as! AVAssetTrack
- var assetTrack2:AVAssetTrack = tracks2[0] as! AVAssetTrack
- var assetTrack3:AVAssetTrack = tracks3[0] as! AVAssetTrack
- var duration1: CMTime = assetTrack1.timeRange.duration
- var duration2: CMTime = assetTrack2.timeRange.duration
- var duration3: CMTime = assetTrack3.timeRange.duration
- var timeRange1 = CMTimeRangeMake(kCMTimeZero, duration1)
- var timeRange2 = CMTimeRangeMake(kCMTimeZero, duration2)
- var timeRange3 = CMTimeRangeMake(kCMTimeZero, duration3)
- ok1 = compositionAudioTrack.insertTimeRange(timeRange1, ofTrack: assetTrack1, atTime: kCMTimeZero, error: nil)
- if ok1 {
- ok2 = compositionAudioTrack.insertTimeRange(timeRange2, ofTrack: assetTrack2, atTime: duration1, error: nil)
- if ok2 {
- ok3 = compositionAudioTrack.insertTimeRange(timeRange3, ofTrack: assetTrack3, atTime: CMTimeAdd(duration1, duration2), error: nil)
- }
- }
- //AVAssetExportPresetPassthrough => concatenation
- var assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
- assetExport.outputFileType = AVFileTypeAppleM4A
- assetExport.outputURL = fileDestinationUrl
- assetExport.exportAsynchronouslyWithCompletionHandler({
- switch assetExport.status{
- case AVAssetExportSessionStatus.Failed:
- println("failed \(assetExport.error)")
- case AVAssetExportSessionStatus.Cancelled:
- println("cancelled \(assetExport.error)")
- default:
- println("complete")
- var audioPlayer = AVAudioPlayer()
- audioPlayer = AVAudioPlayer(contentsOfURL: fileDestinationUrl, error: nil)
- audioPlayer.prepareToPlay()
- audioPlayer.play()
- }
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement