当我运行我的应用程序并单击保存按钮以保存混合在一起的两个文件时,我的应用程序崩溃说无效文件输出。我不明白为什么会出现这个错误,因为混合的两个文件是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")
}
答案 0 :(得分:1)
输出文件不是 mp3。你可以说 mp3,但这不会成为一个。我不认为Apple框架代码可以保存为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
)。
答案 1 :(得分:-2)
您可以将其导出到" AVFileTypeQuickTimeMovie"并将其重命名为* .mp3。
初始化导出,在这里你必须设置" presentName"参数" AVAssetExportPresetPassthrough"。如果没有,你将无法正确导出mp3。
dynamic me = fb.Get("/me?fields=email");
设置输出类型:
AVAssetExportSession *export = [[AVAssetExportSession alloc] initWithAsset:sset presetName:AVAssetExportPresetPassthrough];
导出它并将文件扩展名设置为" mp3"。