无效的文件输出AVAssetExport

当我运行我的应用程序,然后单击保存button来保存混合在一起的两个文件我的应用程序崩溃说无效的文件输出。 我不明白为什么这个错误出现,因为两个文件混合是MP3和输出文件是MP3。

码:

@IBAction func mixButton (sender:AnyObject!) { let oldAsset = self.player.currentItem.asset let type = AVMediaTypeAudio let audioFile = NSBundle.mainBundle().URLForResource("file1", withExtension: "mp3") let asset1 = AVURLAsset(URL: audioFile, options: nil) let arr2 = asset1.tracksWithMediaType(type) let track2 = arr2.last as AVAssetTrack let duration : CMTime = track2.timeRange.duration let comp = AVMutableComposition() let comptrack = comp.addMutableTrackWithMediaType(type, preferredTrackID: Int32(kCMPersistentTrackID_Invalid)) comptrack.insertTimeRange(CMTimeRangeMake(CMTimeMakeWithSeconds(0,600), CMTimeMakeWithSeconds(5,600)), ofTrack:track2, atTime:CMTimeMakeWithSeconds(0,600), error:nil) comptrack.insertTimeRange(CMTimeRangeMake(CMTimeSubtract(duration, CMTimeMakeWithSeconds(5,600)), CMTimeMakeWithSeconds(5,600)), ofTrack:track2, atTime:CMTimeMakeWithSeconds(5,600), error:nil) let type3 = AVMediaTypeAudio let s = NSBundle.mainBundle().URLForResource("file2", withExtension:"mp3") let asset = AVURLAsset(URL:s, options:nil) let arr3 = asset.tracksWithMediaType(type3) let track3 = arr3.last as AVAssetTrack let comptrack3 = comp.addMutableTrackWithMediaType(type3, preferredTrackID:Int32(kCMPersistentTrackID_Invalid)) comptrack3.insertTimeRange(CMTimeRangeMake(CMTimeMakeWithSeconds(0,600), CMTimeMakeWithSeconds(10,600)), ofTrack:track3, atTime:CMTimeMakeWithSeconds(0,600), error:nil) let params = AVMutableAudioMixInputParameters(track:comptrack3) params.setVolume(1, atTime:CMTimeMakeWithSeconds(0,600)) params.setVolumeRampFromStartVolume(1, toEndVolume:0, timeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(7,600), CMTimeMakeWithSeconds(3,600))) let mix = AVMutableAudioMix() mix.inputParameters = [params] let item = AVPlayerItem(asset:comp) item.audioMix = mix mixedFile = comp //global variable for mixed file 

}}

 @IBAction func saveButton(sender: AnyObject) { let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String let savedFileTest = documentsPath + "/myfile.mp3" if (NSFileManager.defaultManager().fileExistsAtPath(savedFileTest)) { NSFileManager.defaultManager().removeItemAtPath(savedFileTest, error: nil) } let url = NSURL.fileURLWithPath(savedFileTest) let exporter = AVAssetExportSession(asset: mixedFile, presetName: AVAssetExportPresetHighestQuality) exporter.outputURL = url exporter.outputFileType = AVFileTypeMPEGLayer3 exporter.exportAsynchronouslyWithCompletionHandler({ switch exporter.status{ case AVAssetExportSessionStatus.Failed: println("failed \(exporter.error)") case AVAssetExportSessionStatus.Cancelled: println("cancelled \(exporter.error)") default: println("complete") } 

输出文件不是 mp3。 你可以说,但是,这并不是一个。 我不认为苹果框架代码可以保存为MP3。 它可以阅读,但由于各种许可问题,它不能写。

像m4a一样,像这样(我已经从你原来的代码中改变了行):

 let savedFileTest = documentsPath + "/myfile.m4a" // * if (NSFileManager.defaultManager().fileExistsAtPath(savedFileTest)) { NSFileManager.defaultManager().removeItemAtPath(savedFileTest, error: nil) } let url = NSURL.fileURLWithPath(savedFileTest) let exporter = AVAssetExportSession( asset: mixedFile, presetName: AVAssetExportPresetAppleM4A) // * exporter.outputURL = url exporter.outputFileType = AVFileTypeAppleM4A // * 

顺便说一下,你保存错误的东西(混合的comp而不是混合的item )。

您可以将其导出到“AVFileTypeQuickTimeMovie”并将其重命名为* .mp3。

初始化导出,在这里你必须设置“presentName”参数为“AVAssetExportPresetPassthrough”。 如果没有,你将无法导出MP3正确。

 AVAssetExportSession *export = [[AVAssetExportSession alloc] initWithAsset:sset presetName:AVAssetExportPresetPassthrough]; 

设置输出types:

 exportSession.outputFileType = AVFileTypeQuickTimeMovie; exportSession.shouldOptimizeForNetworkUse = true; 

导出它并将文件扩展名设置为“mp3”。