无效的文件输出AVAssetExport [英] Invalid File Output AVAssetExport

查看:256
本文介绍了无效的文件输出AVAssetExport的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

code:

  @IBAction FUNC mixButton(发件人:AnyObject){
        让oldAsset = self.player.currentItem.asset        让类型= AVMediaTypeAudio
        让AUDIOFILE = NSBundle.mainBundle()URLForResource(文件1,withExtension:MP3)。
        让asset1 = AVURLAsset(网址:的AudioFile,选择:无)
        让ARR2 = asset1.tracksWithMediaType(类型)
        让TRACK2 = arr2.last为AVAssetTrack        让时间:CMTime = track2.timeRange.duration        让补偿= AVMutableComposition()
        让comptrack = comp.addMutableTrackWithMediaType(类型,
            preferredTrackID:的Int32(kCMPersistentTrackID_Invalid))        comptrack.insertTimeRange(CMTimeRangeMake(CMTimeMakeWithSeconds(0600),CMTimeMakeWithSeconds(5600)),ofTrack:TRACK2,atTime:CMTimeMakeWithSeconds(0600),错误:无)
        comptrack.insertTimeRange(CMTimeRangeMake(CMTimeSubtract(持续时间,CMTimeMakeWithSeconds(5600)),CMTimeMakeWithSeconds(5600)),ofTrack:TRACK2,atTime:CMTimeMakeWithSeconds(5600),错误:无)        让3型= AVMediaTypeAudio
        让S = NSBundle.mainBundle()URLForResource。(文件2,withExtension:MP3)
        让资产= AVURLAsset(网址:S,选择:无)
        让ARR3 = asset.tracksWithMediaType(3型)
        让TRACK3 = arr3.last为AVAssetTrack        让comptrack3 = comp.addMutableTrackWithMediaType(类型3,preferredTrackID:的Int32(kCMPersistentTrackID_Invalid))
        comptrack3.insertTimeRange(CMTimeRangeMake(CMTimeMakeWithSeconds(0600),CMTimeMakeWithSeconds(10600)),ofTrack:TRACK3,atTime:CMTimeMakeWithSeconds(0600),错误:无)        让PARAMS = AVMutableAudioMixInputParameters(曲目:comptrack3)
        params.setVolume(1,atTime:CMTimeMakeWithSeconds(0600))
        params.setVolumeRampFromStartVolume(1,toEndVolume:0,TIMERANGE:CMTimeRangeMake(CMTimeMakeWithSeconds(7600),CMTimeMakeWithSeconds(3 600)))
        让混合= AVMutableAudioMix()
        mix.inputParameters = [PARAMS]        让项目= AVPlayerItem(资产:COMP)
        item.audioMix =混合
        mixedFile =补偿//全球混合文件变量

}
}

  @IBAction FUNC saveButton(发件人:AnyObject){
    让documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,真)[0]为String
    让savedFileTest = documentsPath +/myfile.mp3
    如果(NSFileManager.defaultManager()。fileExistsAtPath(savedFileTest)){
        。NSFileManager.defaultManager()removeItemAtPath(savedFileTest,错误:无)
    }    让URL = NSURL.fileURLWithPath(savedFileTest)    让出口= AVAssetExportSession(资产:mixedFile,presetName:AVAssetExport presetHighestQuality)
    exporter.outputURL =网址
    exporter.outputFileType = AVFileTypeMPEGLayer3    exporter.exportAsynchronouslyWithCompletionHandler({
        开关exporter.status {
        案例AVAssetExportSessionStatus.Failed:
            的println(失败\\(exporter.error))
        案例AVAssetExportSessionStatus.Cancelled:
            的println(取消\\(exporter.error))
        默认:
            的println(完整)
        }


解决方案

输出文件的的MP3。你可以的的MP3但这并不能使它之一。我不认为苹果框架code的可以的保存为MP3。它可以读取它,但由于各种许可证的问题也不能写。

做到这一点作为一个M4A,像这样(我出演,我从你原来的code更改的行):

 让savedFileTest = documentsPath +/myfile.m4a// *
如果(NSFileManager.defaultManager()。fileExistsAtPath(savedFileTest)){
    。NSFileManager.defaultManager()removeItemAtPath(savedFileTest,错误:无)
}
让URL = NSURL.fileURLWithPath(savedFileTest)
让出口= AVAssetExportSession(
    资产:mixedFile,presetName:AVAssetExport presetAppleM4A)// *
exporter.outputURL =网址
exporter.outputFileType = AVFileTypeAppleM4A // *

顺便说一句,你节省了错误的东西(非混合补偿,而不是混合项目

When i run my app and click the save button to save the two files mixed together my app crashes saying invalid file output. I dont see why this error shows up because the two files being mixed are mp3 and output file is mp3.

Code:

 @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")
        }

解决方案

The output file is not mp3. You can say mp3 but that doesn't make it one. I don't think Apple framework code can save as mp3. It can read it, but due to various licensing issues it can't write it.

Do it as an m4a, like this (I have starred the lines I changed from your original code):

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 // *

By the way, you're saving the wrong thing (the unmixed comp rather than the mixed item).

这篇关于无效的文件输出AVAssetExport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆