Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate two audio files in Swift and play them

I try to concatenate .wav audio files in swift.

Here is my code :

func merge(audio1: NSURL, audio2:  NSURL) {


    var error:NSError?

    var ok1 = false
    var ok2 = false


    var documentsDirectory:String = paths[0] as! String

    //Create AVMutableComposition Object.This object will hold our multiple AVMutableCompositionTrack.
    var composition = AVMutableComposition()
    var compositionAudioTrack1: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 url1 = audio1
    var url2 = audio2


    var avAsset1 = AVURLAsset(URL: url1, options: nil)
    var avAsset2 = AVURLAsset(URL: url2, options: nil)

    var tracks1 =  avAsset1.tracksWithMediaType(AVMediaTypeAudio)
    var tracks2 =  avAsset2.tracksWithMediaType(AVMediaTypeAudio)

    var assetTrack1:AVAssetTrack = tracks1[0] as! AVAssetTrack
    var assetTrack2:AVAssetTrack = tracks2[0] as! AVAssetTrack


    var duration1: CMTime = assetTrack1.timeRange.duration
    var duration2: CMTime = assetTrack2.timeRange.duration

    var timeRange1 = CMTimeRangeMake(kCMTimeZero, duration1)
    var timeRange2 = CMTimeRangeMake(duration1, duration2)


    ok1 = compositionAudioTrack1.insertTimeRange(timeRange1, ofTrack: assetTrack1, atTime: kCMTimeZero, error: nil)
    if ok1 {

        ok2 = compositionAudioTrack2.insertTimeRange(timeRange2, ofTrack: assetTrack2, atTime: duration1, error: nil)

        if ok2 {
            println("success")
        }
    }

    //AVAssetExportPresetPassthrough => concatenation
    var assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetPassthrough)
    assetExport.outputFileType = AVFileTypeWAVE
    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()
        }

    })

}

And get this error in the terminal (running on a iPhone) :

file:///var/mobile/Containers/Data/Application/3F49D360-B363-4600-B3BB-EE0810501910/Documents/resultmerge.wav

success

failed Error Domain=AVFoundationErrorDomain Code=-11838 "Opération interrompue" UserInfo=0x174269ac0 {NSLocalizedDescription=Opération interrompue, NSLocalizedFailureReason=L’opération n’est pas prise en charge pour ce contenu multimédia.}

But I don't know why I'm getting this error. I would greatly appreciate any help you can give me :)

like image 344
Pierre Louis Bresson Avatar asked May 27 '15 10:05

Pierre Louis Bresson


3 Answers

for swift 3.0 - credit goes to @Peyman (with light modification)

var mergeAudioURL = NSURL()

func mergeAudioFiles(audioFileUrls: NSArray) {
    let composition = AVMutableComposition()

    for i in 0 ..< audioFileUrls.count {

        let compositionAudioTrack :AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())

        let asset = AVURLAsset(url: (audioFileUrls[i] as! NSURL) as URL)

        let track = asset.tracks(withMediaType: AVMediaTypeAudio)[0]

        let timeRange = CMTimeRange(start: CMTimeMake(0, 600), duration: track.timeRange.duration)

        try! compositionAudioTrack.insertTimeRange(timeRange, of: track, at: composition.duration)
    }

    let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
    self.mergeAudioURL = documentDirectoryURL.appendingPathComponent("FinalAudio.m4a")! as URL as NSURL

    let assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
    assetExport?.outputFileType = AVFileTypeAppleM4A
    assetExport?.outputURL = mergeAudioURL as URL
    assetExport?.exportAsynchronously(completionHandler:
        {
            switch assetExport!.status
            {
            case AVAssetExportSessionStatus.failed:
                print("failed \(assetExport?.error)")
            case AVAssetExportSessionStatus.cancelled:
                print("cancelled \(assetExport?.error)")
            case AVAssetExportSessionStatus.unknown:
                print("unknown\(assetExport?.error)")
            case AVAssetExportSessionStatus.waiting:
                print("waiting\(assetExport?.error)")
            case AVAssetExportSessionStatus.exporting:
                print("exporting\(assetExport?.error)")
            default:
                print("Audio Concatenation Complete")
            }
    })
}
like image 68
pigeon_39 Avatar answered Oct 06 '22 06:10

pigeon_39


I needed to merge multiple audio files so I rewrote the function to accept an array of NSURLs. Thought I'd share it here.

I'm new to Swift, so please leave feedback.

Give credit where credit is due: @Eric D. @Pierre Louis Bresson

Here is the code:

func mergeAudioFiles(audioFileUrls: NSArray, callback: (url: NSURL?, error: NSError?)->()) {

// Create the audio composition
let composition = AVMutableComposition()

// Merge
for (var i = 0; i < audioFileUrls.count; i++) {

    let compositionAudioTrack :AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())

    let asset = AVURLAsset(URL: audioFileUrls[i] as! NSURL)

    let track = asset.tracksWithMediaType(AVMediaTypeAudio)[0]

    let timeRange = CMTimeRange(start: CMTimeMake(0, 600), duration: track.timeRange.duration)

    try! compositionAudioTrack.insertTimeRange(timeRange, ofTrack: track, atTime: composition.duration)
}

// Create output url
let format = NSDateFormatter()
format.dateFormat="yyyy-MM-dd-HH-mm-ss"
let currentFileName = "recording-\(format.stringFromDate(NSDate()))-merge.m4a"
print(currentFileName)

let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let outputUrl = documentsDirectory.URLByAppendingPathComponent(currentFileName)

// Export it
let assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
assetExport?.outputFileType = AVFileTypeAppleM4A
assetExport?.outputURL = outputUrl

assetExport?.exportAsynchronouslyWithCompletionHandler({ () -> Void in
    switch assetExport!.status {
        case AVAssetExportSessionStatus.Failed:
            callback(url: nil, error: assetExport?.error)
        default:
            callback(url: assetExport?.outputURL, error: nil)
    }
})

}
like image 6
Peyman Avatar answered Oct 06 '22 07:10

Peyman


I got your code working by changing two things:

  • the preset name: from AVAssetExportPresetPassthrough to AVAssetExportPresetAppleM4A

  • the output file type: from AVFileTypeWAVE to AVFileTypeAppleM4A

Modify your assetExport declaration like this:

var assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
assetExport.outputFileType = AVFileTypeAppleM4A

then it will properly merge the files.

It looks like AVAssetExportSession only exports M4A format and ignores other presets. There may be a way to make it export other formats (by subclassing it?), though I haven't explored this possibility yet.

like image 5
Eric Aya Avatar answered Oct 06 '22 07:10

Eric Aya