0

我正在使用SDAVAssetExportSession转换MPMedaiItem.m4a文件,它工作得很好。

链接:这里

这是我的代码,最终结果将是.m4a文件,fileName因为1.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格式的输出结果文件,我查看了类文件,但我无法弄清楚在“.aac”文件的代码中要更改什么。

有人有什么主意吗?

4

0 回答 0