0

I'm using SDAVAssetExportSession to convert MPMedaiItem to .m4a file and it's working great.

Link : Here

This is my code, end result will be .m4a file, when in pass fileName as 1.aac this code not working.

I don't know what to pass in outputFileType for .aac type.

//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)
            }
        })
    }

If i pass .aac in file name this will give me error : Error: Cannot write output file

What i want is, the output result file in .aac format, i looked into class file but i can't get figure it out what to change in code for '.aac' file.

Does anyone have any idea?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Pratik Prajapati
  • 1,137
  • 1
  • 13
  • 26

0 Answers0