如何使用AVAssetReader导出.aac文件?

时间:2018-04-14 06:37:09

标签: ios swift mpmediaitem avassetreader

我使用SDAVAssetExportSessionMPMedaiItem转换为.m4a文件并且效果很好。

链接:Here

这是我的代码,最终结果将是.m4a文件,当传递fileName1.aac此代码无效。

我不知道outputFileType类型的.aac传递的内容。

//MARK:- Export Function
    func exportAssetNew(_ asset: AVAsset, fileName: String,startPost:Int64,endPost:Int64) {
        //print("\(#function)")
        //print("startPost : \(startPost)  :: endPost : \(endPost)")
        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let trimmedSoundFileURL = documentsDirectory.appendingPathComponent(fileName)
        //print("creating export session for \(asset)")

        newSession = SDAVAssetExportSession(asset: loadedAssets)
        newSession.outputFileType = AVFileTypeAppleM4A
        newSession.outputURL = trimmedSoundFileURL
        newSession.audioSettings = [AVFormatIDKey : kAudioFormatMPEG4AAC,AVNumberOfChannelsKey : 2,AVSampleRateKey:44100,AVEncoderBitRateKey:128000]

        let startTime = CMTimeMake(startPost, 1)
        let stopTime = CMTimeMake(endPost, 1)
        newSession.timeRange = CMTimeRangeFromTimeToTime(startTime, stopTime)
        newSession.exportAsynchronously(completionHandler: {
            if self.newSession.status == AVAssetExportSessionStatus.completed {
                //print("New Export Complete : fileName: ",fileName)
                if self.songPartsUrls.count == 0 {
                    self.songPartsUrls.append(trimmedSoundFileURL)
                    self.childUploadingIndex = 0
                  //  print("Stime : ",Date().timeIntervalSince1970)
                    self.createRequestWithAWS(songNamePrefix: self.regionPrefix)
                }else{
                    self.songPartsUrls.append(trimmedSoundFileURL)
                    if self.isGoUploadFromChild {
                       self.isGoUploadFromChild = false
                       self.createRequestWithAWS(songNamePrefix: self.regionPrefix)
                    }
                }
                self.startSplitingSongs()
            } else if self.newSession.status == AVAssetExportSessionStatus.cancelled {
                print("New Export Cancel")
            } else {
                print("Error: ",self.newSession.error.localizedDescription)
            }
        })
    }

如果我在文件名中传递.aac,则会给出错误:错误:无法写入输出文件

我想要的是,.aac格式的输出结果文件,我查看了类文件,但我无法弄清楚在#39; .aac&#39的代码中要更改的内容;文件。

有没有人有任何想法?

0 个答案:

没有答案