• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift 将录制的多个aac音频合并为m4a格式音频

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
//合并音频
    func mergeAudio() {
        let composition = AVMutableComposition()
        
        for i in 0 ..< audioLocalUrls.count {
            
            let compositionAudioTrack :AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
            
            let asset = AVURLAsset(url: URL.init(fileURLWithPath: audioLocalUrls[i]))
            
            let track = asset.tracks(withMediaType: AVMediaTypeAudio)[0]
            
            var timeRange:CMTimeRange
            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
        let stringDate = BaseController().getCurrentTime()
        self.mergeAudioURL = (documentDirectoryURL.appendingPathComponent(BaseController().randomMD5(str: stringDate) + ".m4a")! as URL as NSURL) as URL!
        
        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("success\(assetExport?.error)")
                        
                    
                    for i in 0..<self.audioLocalUrls.count{
                        let fileManager = FileManager.default
                        try! fileManager.removeItem(at: URL.init(fileURLWithPath: self.audioLocalUrls[i]))
                    }
                }
        })
    }

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift 是猴还是猿?发布时间:2022-07-13
下一篇:
Swift中的闭包(Closure)浅析发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap