2016-11-17 81 views
0

我合併了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 _ 
    { 

    } 

    } 
+2

編碼爲MP3或AAC合併後? – Moritz

+0

Iam不編碼任何東西..我只是合併兩個音頻文件(一個文件是背景歌曲()MP3和另一個是語音(M4A)我已經記錄)。我附上上面的代碼..我只是檢查合併的文件在文件夾..大小應該是7MB.soi想要減小尺寸。 – HariKarthick

+1

這就是爲什麼我建議你在合併後編碼你的音頻到MP3或AAC ** ... – Moritz

回答

0

您必須爲作者提供音頻設置字典。 這裏有下列格式,你可以看看

kAudioFormatMPEG4AAC:164(最低)

kAudioFormatAppleLossless:430,

kAudioFormatAppleIMA4:475,

kAudioFormatULaw:889,

kAudioFormatALaw :889

使用kAudioFormatMPEG4AAC它是最小的a蒙所有。

 let audioOutputSettings = [ 
      AVFormatIDKey : Int(kAudioFormatMPEG4AAC), 
      AVSampleRateKey :  //[ NSNumber numberWithInt: 16 ], AVEncoderBitDepthHintKey, 
       Int(44100.0), 
      AVNumberOfChannelsKey : Int(1), 
      AVChannelLayoutKey: NSData(bytes:&channelLayout, length:sizeof(AudioChannelLayout)), 
] 

這是在Swift中的上述設置。

在這條線

讓出口= AVAssetExportSession(資產:組成,presetName:AVAssetExportPresetAppleM4A)

u能試試這個,而不是

let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetLowQuality) 
+0

我的不好,你的代碼沒有加載到我的瀏覽器中,現在它有任何方法是的,我正在嘗試 –

+1

我編輯了答案,你可以嘗試做我在最後一行提到的改變。 –

+0

http://chat.stackoverflow.com/rooms/37266/discussion-between-grzegorz-krukowski-and-newbie 可能是一個參考 –