Combine audioFiles iOS
Solution:
You can combine audio files in swift also.You can pass the audiofile paths to one function it can combine those files and save it as single file.
Code:
func combineAudioFiles(audioFileUrls: NSArray,place:MyLocations,isOnEntry:Bool) {
let composition = AVMutableComposition()
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let urls = documentsDirectory.appendingPathComponent(audioFileUrls[i] as! String)
let asset = AVURLAsset(url: urls)
let track = asset.tracks(withMediaType: AVMediaTypeAudio).first
let timeRange = CMTimeRange(start: CMTimeMake(0, 600), duration: (track?.timeRange.duration)!)
try! compositionAudioTrack.insertTimeRange(timeRange, of: track!, at: composition.duration)
}
let fileManager = FileManager.default
let documentDirectoryURL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first! as NSURL
var newUrl:NSURL!
let newDir = documentDirectoryURL.appendingPathComponent("Sounds")?.path
do{
try fileManager.createDirectory(atPath: newDir!,withIntermediateDirectories: true, attributes: nil)
newUrl = documentDirectoryURL.appendingPathComponent("Sounds") as! NSURL
} catch {
print("Error: \(error.localizedDescription)")
}
let mergeAudioFiles = isOnEntry == true ? newUrl!.appendingPathComponent("\(place.locationID)entry.m4a") as! URL as NSURL : newUrl!.appendingPathComponent("combinedAudio.m4a") as! URL as NSURL
if fileManager.fileExists(atPath: mergeAudioFiles.path!) {
do{
try fileManager.removeItem(atPath: mergeAudioFiles.path!)
} catch {
//Do nothing
}
}
let assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
assetExport?.outputFileType = AVFileTypeAppleM4A
assetExport?.outputURL = mergeAudioFiles 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")
}
})
}
You can combine audio files in swift also.You can pass the audiofile paths to one function it can combine those files and save it as single file.
Code:
func combineAudioFiles(audioFileUrls: NSArray,place:MyLocations,isOnEntry:Bool) {
let composition = AVMutableComposition()
for i in 0 ..< audioFileUrls.count {
let compositionAudioTrack :AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let urls = documentsDirectory.appendingPathComponent(audioFileUrls[i] as! String)
let asset = AVURLAsset(url: urls)
let track = asset.tracks(withMediaType: AVMediaTypeAudio).first
let timeRange = CMTimeRange(start: CMTimeMake(0, 600), duration: (track?.timeRange.duration)!)
try! compositionAudioTrack.insertTimeRange(timeRange, of: track!, at: composition.duration)
}
let fileManager = FileManager.default
let documentDirectoryURL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first! as NSURL
var newUrl:NSURL!
let newDir = documentDirectoryURL.appendingPathComponent("Sounds")?.path
do{
try fileManager.createDirectory(atPath: newDir!,withIntermediateDirectories: true, attributes: nil)
newUrl = documentDirectoryURL.appendingPathComponent("Sounds") as! NSURL
} catch {
print("Error: \(error.localizedDescription)")
}
let mergeAudioFiles = isOnEntry == true ? newUrl!.appendingPathComponent("\(place.locationID)entry.m4a") as! URL as NSURL : newUrl!.appendingPathComponent("combinedAudio.m4a") as! URL as NSURL
if fileManager.fileExists(atPath: mergeAudioFiles.path!) {
do{
try fileManager.removeItem(atPath: mergeAudioFiles.path!)
} catch {
//Do nothing
}
}
let assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
assetExport?.outputFileType = AVFileTypeAppleM4A
assetExport?.outputURL = mergeAudioFiles 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")
}
})
}
Comments
Post a Comment