Advertisement
Liborek

Merging two wav files in Swift

Jul 16th, 2015
1,248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.30 KB | None | 0 0
  1. class ViewController: UIViewController {
  2.  
  3.     override func viewDidLoad() {
  4.         super.viewDidLoad()
  5.        
  6.         var audio1 = NSURL(fileURLWithPath:  NSBundle.mainBundle().pathForResource("Buildup1", ofType: "wav")!)!
  7.         var audio2 = NSURL(fileURLWithPath:  NSBundle.mainBundle().pathForResource("Verse1", ofType: "wav")!)!
  8.         var audio3 = NSURL(fileURLWithPath:  NSBundle.mainBundle().pathForResource("Drop1", ofType: "wav")!)!
  9.         self.concatenate(audio1, audio2Url: audio2, audio3Url: audio3)
  10.     }
  11.  
  12.     override func didReceiveMemoryWarning() {
  13.         super.didReceiveMemoryWarning()
  14.         // Dispose of any resources that can be recreated.
  15.     }
  16.  
  17.  
  18.     func concatenate(audio1Url: NSURL, audio2Url:  NSURL, audio3Url: NSURL) {
  19.        
  20.        
  21.         var error:NSError?
  22.        
  23.         var ok1 = false
  24.         var ok2 = false
  25.         var ok3 = false
  26.        
  27.        
  28.         //var documentsDirectory:String = paths[0] as! String
  29.        
  30.         //Create AVMutableComposition Object.This object will hold our multiple AVMutableCompositionTrack.
  31.         var composition = AVMutableComposition()
  32.         var compositionAudioTrack:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
  33.         //var compositionAudioTrack2:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
  34.        
  35.         //create new file to receive data
  36.         var documentDirectoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as! NSURL
  37.         var fileDestinationUrl = documentDirectoryURL.URLByAppendingPathComponent("resultmerge.wav")
  38.         println(fileDestinationUrl)
  39.        
  40.        
  41.         var avAsset1 = AVURLAsset(URL: audio1Url, options: nil)
  42.         var avAsset2 = AVURLAsset(URL: audio2Url, options: nil)
  43.         var avAsset3 = AVURLAsset(URL: audio3Url, options: nil)
  44.        
  45.         var tracks1 =  avAsset1.tracksWithMediaType(AVMediaTypeAudio)
  46.         var tracks2 =  avAsset2.tracksWithMediaType(AVMediaTypeAudio)
  47.         var tracks3 =  avAsset3.tracksWithMediaType(AVMediaTypeAudio)
  48.        
  49.         var assetTrack1:AVAssetTrack = tracks1[0] as! AVAssetTrack
  50.         var assetTrack2:AVAssetTrack = tracks2[0] as! AVAssetTrack
  51.         var assetTrack3:AVAssetTrack = tracks3[0] as! AVAssetTrack
  52.        
  53.        
  54.         var duration1: CMTime = assetTrack1.timeRange.duration
  55.         var duration2: CMTime = assetTrack2.timeRange.duration
  56.         var duration3: CMTime = assetTrack3.timeRange.duration
  57.        
  58.        
  59.        
  60.         var timeRange1 = CMTimeRangeMake(kCMTimeZero, duration1)
  61.         var timeRange2 = CMTimeRangeMake(kCMTimeZero, duration2)
  62.         var timeRange3 = CMTimeRangeMake(kCMTimeZero, duration3)
  63.        
  64.        
  65.         ok1 = compositionAudioTrack.insertTimeRange(timeRange1, ofTrack: assetTrack1, atTime: kCMTimeZero, error: nil)
  66.         if ok1 {
  67.            
  68.             ok2 = compositionAudioTrack.insertTimeRange(timeRange2, ofTrack: assetTrack2, atTime: duration1, error: nil)
  69.            
  70.             if ok2 {
  71.                 ok3 = compositionAudioTrack.insertTimeRange(timeRange3, ofTrack: assetTrack3, atTime: CMTimeAdd(duration1, duration2), error: nil)
  72.                
  73.             }
  74.         }
  75.        
  76.         //AVAssetExportPresetPassthrough => concatenation
  77.         var assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
  78.         assetExport.outputFileType = AVFileTypeAppleM4A
  79.         assetExport.outputURL = fileDestinationUrl
  80.         assetExport.exportAsynchronouslyWithCompletionHandler({
  81.             switch assetExport.status{
  82.             case  AVAssetExportSessionStatus.Failed:
  83.                 println("failed \(assetExport.error)")
  84.             case AVAssetExportSessionStatus.Cancelled:
  85.                 println("cancelled \(assetExport.error)")
  86.             default:
  87.                 println("complete")
  88.                 var audioPlayer = AVAudioPlayer()
  89.                 audioPlayer = AVAudioPlayer(contentsOfURL: fileDestinationUrl, error: nil)
  90.                 audioPlayer.prepareToPlay()
  91.                 audioPlayer.play()
  92.             }
  93.            
  94.         })
  95.        
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement