我在Swift2.3中合并了两个音频文件。
工作正常。文件大小为7MB。所以我无法发送到服务器。
如何减小音频文件大小?这样我就可以轻松发送到服务器了。 我附上了以下代码。对于那些试图合并的人来说,它会很有用:
func mixAudio()
{
let currentTime = CFAbsoluteTimeGetCurrent()
let composition = AVMutableComposition()
let compositionAudioTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)
compositionAudioTrack.preferredVolume = 0.8
let avAsset = AVURLAsset.init(URL: soundFileURL, options: nil)
print("\(avAsset)")
var tracks = avAsset.tracksWithMediaType(AVMediaTypeAudio)
let clipAudioTrack = tracks[0]
do {
try compositionAudioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, avAsset.duration), ofTrack: clipAudioTrack, atTime: kCMTimeZero)
}
catch _ {
}
let compositionAudioTrack1 = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)
compositionAudioTrack.preferredVolume = 0.8
let avAsset1 = AVURLAsset.init(URL: soundFileURL1)
print(avAsset1)
var tracks1 = avAsset1.tracksWithMediaType(AVMediaTypeAudio)
let clipAudioTrack1 = tracks1[0]
do {
try compositionAudioTrack1.insertTimeRange(CMTimeRangeMake(kCMTimeZero, avAsset1.duration), ofTrack: clipAudioTrack1, atTime: kCMTimeZero)
}
catch _ {
}
var paths = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)
let CachesDirectory = paths[0]
let strOutputFilePath = CachesDirectory.stringByAppendingString("/newone.mov")
print(" strOutputFilePath is \n \(strOutputFilePath)")
let requiredOutputPath = CachesDirectory.stringByAppendingString("/newone.m4a")
print(" requiredOutputPath is \n \(requiredOutputPath)")
let audioFileOutput = NSURL.fileURLWithPath(requiredOutputPath)
print(" OUtput path is \n \(audioFileOutput)")
do {
try NSFileManager.defaultManager().removeItemAtURL(audioFileOutput)
}
catch _ {
}
let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
exporter!.outputURL = audioFileOutput
exporter!.outputFileType = AVFileTypeAppleM4A
exporter!.exportAsynchronouslyWithCompletionHandler
{() -> Void in
print(" OUtput path is \n \(requiredOutputPath)")
print("export complete: \(CFAbsoluteTimeGetCurrent() - currentTime)")
do
{
print(audioFileOutput)
print(" OUtput path is \n \(requiredOutputPath)")
self.wasteplayer = try AVAudioPlayer(contentsOfURL: audioFileOutput)
self.wasteplayer.numberOfLoops = 0
self.wasteplayer.play()
}
}
catch _
{
}
}
答案 0 :(得分:0)
您必须为作家提供音频设置词典。 以下是您可以查看的以下格式
kAudioFormatMPEG4AAC:164,(最低)
kAudioFormatAppleLossless:430,
kAudioFormatAppleIMA4:475,
kAudioFormatULaw:889,
kAudioFormatALaw:889
使用kAudioFormatMPEG4AAC它是最小的。
let audioOutputSettings = [
AVFormatIDKey : Int(kAudioFormatMPEG4AAC),
AVSampleRateKey : //[ NSNumber numberWithInt: 16 ], AVEncoderBitDepthHintKey,
Int(44100.0),
AVNumberOfChannelsKey : Int(1),
AVChannelLayoutKey: NSData(bytes:&channelLayout, length:sizeof(AudioChannelLayout)),
]
这是Swift中的上述设置。
在这一行
let exporter = AVAssetExportSession(asset:composition,presetName:AVAssetExportPresetAppleM4A)
可以试试吗
let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetLowQuality)