Create a single audio file from multiple MP3 files (concatenate audio)

Recently (like today), I had the need to concatenate multiple MP3s into a single sound file for playback in a notification. There is a bit of Objective-C code available for demonstrating this process, but nothing in Swift. So, here’s my Swift:


func createSound(soundFiles: [String], outputFile: String) {
var startTime: CMTime = kCMTimeZero
let composition: AVMutableComposition = AVMutableComposition()
let compositionAudioTrack: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)

for fileName in soundFiles {
let sound: String = Bundle.main.path(forResource: fileName, ofType: "mp3")!
let url: URL = URL(fileURLWithPath: sound)
let avAsset: AVURLAsset = AVURLAsset(url: url)
let timeRange: CMTimeRange = CMTimeRangeMake(kCMTimeZero, avAsset.duration)
let audioTrack: AVAssetTrack = avAsset.tracks(withMediaType: AVMediaTypeAudio)[0]

try! compositionAudioTrack.insertTimeRange(timeRange, of: audioTrack, at: startTime)
startTime = CMTimeAdd(startTime, timeRange.duration)
}

let exportPath: String = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].path+"/"+outputFile+".m4a"

let export: AVAssetExportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)!

export.outputURL = URL(fileURLWithPath: exportPath)
export.outputFileType = AVFileTypeAppleM4A

export.exportAsynchronously {
if export.status == AVAssetExportSessionStatus.completed {
NSLog("All done");
}
}

}

No error checking in the “try” lines, but that is left up to you. The function (createSound) takes an Array of MP3 files (found in the application bundle) and creates a single named M4A file from the concatenated result.

Filenames provided to the function do not include the suffix (.mp3, etc.).

Posted in Uncategorized

iOS in 24 Hours

Hello all,

There isn’t going to be an updated book this year, but I’ve just posted updated projects and am working on updates to the text that I will hopefully be able to release. I’ll be posting errata as best I can, so watch the site/Twitter for updates.

Happy Holidays,
John

Posted in Errata, iOS 9 Errata, Notes

iOS 9 Hour 1: Welcome project

If you’re experiencing the error “An App ID with Identifier ‘com.teachyourselfios.Welcome’ is not available” when trying to install the Welcome app on your device, the fix should be simple (and wasn’t required until recently).

fixwelcome

  1. Click the Welcome project group in Xcode.
  2. Make sure that the Welcome target is selected.
  3. Change “com.teachyourselfios.Welcome” to another string of your choosing.

The problem is that Apple knows that I (a registered developer) have used that string already to compile the application. When you attempt to use it, it sees it as a conflict – even though the app in question has never been submitted to Apple for App Store processing. Sigh.

Posted in Errata, iOS 9 Errata

iOS 9 Hour 6: Swift classes are in a single file

Hour 6, page 215 – there is text remaining from the Objective-C edition that mentions a “interface file.” Swift classes are contained within a single file. The text should state that actions are defined after global variable properties in the .swift file.

Posted in Errata, iOS 9 Errata

iOS 9 Hour 4: Downcasting unnecessary in Using Quick Help

Hour 4, page 147, in the section “Using Quick Help” – Apple has changed the NSDate class so that it properly returns an NSDate object, instead of AnyObject. Unfortunately, my example was not updated to reflect this.

Posted in Errata, iOS 9 Errata

iOS 9 Hour 3: MyObject, not NSDate

Hour 3, page 92, in the second paragraph of the “Downcasting” section – I state that we need to tell Xcode that an object is an NSDate. This should really be “MyObject.” I had originally written the text using specific object examples, but shifted it to generics (“MyObject“) and missed one of the original object classes.

Posted in Errata, iOS 9 Errata

iOS 9 Hour 3: Incorrect letter in Answers section

In Hour 3, page 122, answer 10, the correct answer is spelled out, but the wrong letter is provided. It should be ‘A’, not ‘B’.

Posted in Errata, iOS 9 Errata

iOS 8 Hour 3: String Replacement OOPS!

In Hour 3, I use this code to demonstrate how to replace my last name (Ray) with “is awesome!”:

var myFullName: String = "John Ray"
var myDescription: String = myFullName.stringByReplacingOccurrencesOfString(myFullName,
withString: "is awesome!")

This is incorrect. As written, this would replace “John Ray” (myFullName) with “is awesome!” – not just “Ray”. The proper code should read:

var myFullName: String = "John Ray"
var myDescription: String = myFullName.stringByReplacingOccurrencesOfString("Ray",
withString: "is awesome!")

Posted in Errata, iOS 8 Errata

iOS 8 Hour 5: Change to guide menus

The horizontal and vertical guides are now accessed by choosing Editor, Canvas, Add Guide.

Posted in Errata, iOS 8 Errata

iOS 8 Hour 3: myInstanceMethod reference

In the last paragraph on page 86 (Hour 3), I reference “myInstanceMethod” being defined on line 14. This is incorrect – it is actually defined on line 17.

Posted in Errata, iOS 8 Errata