如何解决在Swift中串联两个音频文件并播放它们
我通过更改两件事使您的代码工作:
assetExport
像这样修改您的声明:
var assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
assetExport.outputFileType = AVFileTypeAppleM4A
那么它将正确合并文件。
看起来AVAssetExportSession
仅导出M4A格式,而忽略其他预设。尽管我还没有探讨过这种可能性,但也许有一种方法可以使其导出其他格式(通过对它进行子类化?)。
解决方法
我尝试快速连接.wav音频文件。
这是我的代码:
func merge(audio1: NSURL,audio2: NSURL) {
var error:NSError?
var ok1 = false
var ok2 = false
var documentsDirectory:String = paths[0] as! String
//Create AVMutableComposition Object.This object will hold our multiple AVMutableCompositionTrack.
var composition = AVMutableComposition()
var compositionAudioTrack1:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio,preferredTrackID: CMPersistentTrackID())
var compositionAudioTrack2:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio,preferredTrackID: CMPersistentTrackID())
//create new file to receive data
var documentDirectoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory,inDomains: .UserDomainMask).first! as! NSURL
var fileDestinationUrl = documentDirectoryURL.URLByAppendingPathComponent("resultmerge.wav")
println(fileDestinationUrl)
var url1 = audio1
var url2 = audio2
var avAsset1 = AVURLAsset(URL: url1,options: nil)
var avAsset2 = AVURLAsset(URL: url2,options: nil)
var tracks1 = avAsset1.tracksWithMediaType(AVMediaTypeAudio)
var tracks2 = avAsset2.tracksWithMediaType(AVMediaTypeAudio)
var assetTrack1:AVAssetTrack = tracks1[0] as! AVAssetTrack
var assetTrack2:AVAssetTrack = tracks2[0] as! AVAssetTrack
var duration1: CMTime = assetTrack1.timeRange.duration
var duration2: CMTime = assetTrack2.timeRange.duration
var timeRange1 = CMTimeRangeMake(kCMTimeZero,duration1)
var timeRange2 = CMTimeRangeMake(duration1,duration2)
ok1 = compositionAudioTrack1.insertTimeRange(timeRange1,ofTrack: assetTrack1,atTime: kCMTimeZero,error: nil)
if ok1 {
ok2 = compositionAudioTrack2.insertTimeRange(timeRange2,ofTrack: assetTrack2,atTime: duration1,error: nil)
if ok2 {
println("success")
}
}
//AVAssetExportPresetPassthrough => concatenation
var assetExport = AVAssetExportSession(asset: composition,presetName: AVAssetExportPresetPassthrough)
assetExport.outputFileType = AVFileTypeWAVE
assetExport.outputURL = fileDestinationUrl
assetExport.exportAsynchronouslyWithCompletionHandler({
switch assetExport.status{
case AVAssetExportSessionStatus.Failed:
println("failed \(assetExport.error)")
case AVAssetExportSessionStatus.Cancelled:
println("cancelled \(assetExport.error)")
default:
println("complete")
var audioPlayer = AVAudioPlayer()
audioPlayer = AVAudioPlayer(contentsOfURL: fileDestinationUrl,error: nil)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
})
}
并在终端中运行此错误(在iPhone上运行):
文件:///var/mobile/Containers/Data/Application/3F49D360-B363-4600-B3BB-
EE0810501910/Documents/resultmerge.wav
成功
失败,错误域= AVFoundationErrorDomain代码= -11838“操作中断” UserInfo = 0x174269ac0
{NSLocalizedDescription =操作中断,NSLocalizedFailureReason =希望操作的内容可以连续多次获得。}
但我不知道为什么会收到此错误。我将不胜感激您可以给我的任何帮助:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。