How can I export an AVPlayer audio mp3 file using AVAssetExportSession?

Now I'm trying to export an mp3 file that was a player using AVPlayer (using the url), so it does not need to be downloaded twice.

This is my sample code:

I tried every outputFileType ...

self.exporter = [[AVAssetExportSession alloc] initWithAsset:self.asset presetName:AVAssetExportPresetPassthrough]; } NSError *error; NSLog(@"export.supportedFileTypes : %@",self.exporter.supportedFileTypes); // "com.apple.quicktime-movie", // "com.apple.m4a-audio", // "public.mpeg-4", // "com.apple.m4v-video", // "public.3gpp", // "org.3gpp.adaptive-multi-rate-audio", // "com.microsoft.waveform-audio", // "public.aiff-audio", // "public.aifc-audio", // "com.apple.coreaudio-format" self.exporter.outputFileType = @"public.aiff-audio"; self.exporter.shouldOptimizeForNetworkUse = YES; NSURL *a = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error]; NSURL *url = [a URLByAppendingPathComponent:@"filename.mp3"]; NSString *filePath = [url absoluteString]; self.exporter.outputURL = url; if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){ [self.exporter exportAsynchronouslyWithCompletionHandler:^{ if (self.exporter.status == AVAssetExportSessionStatusCompleted) { if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){ NSLog(@"File doesn't exist at path"); }else { NSLog@"File saved!"); } } else if(self.exporter.status == AVAssetExportSessionStatusFailed){ NSLog(@"Failed"); }else if(self.exporter.status == AVAssetExportSessionStatusUnknown){ NSLog(@"Unknown"); }else if(self.exporter.status == AVAssetExportSessionStatusCancelled){ NSLog(@"Cancelled"); }else if(self.exporter.status == AVAssetExportSessionStatusWaiting){ NSLog(@"Waiting"); }else if(self.exporter.status == AVAssetExportSessionStatusExporting){ NSLog(@"Exporting"); } NSLog(@"Exporter error! : %@",self.exporter.error); }]; }}else{ NSLog(@"File already exists at path"); } 

If this is not possible, is there any work?

Also, since I can change the format of the audio file. What is the ideal type to work with AVAudioPlayer?

+4
ios audio mp3 avplayer avassetexportsession
source share
2 answers

It seems that AVAssetExportSession only supports file types for transcoding mp3 using com.apple.quicktime-movie (.mov) and com.apple.coreaudio-format (.caf) using the AVAssetExportPresetPassthrough preset. You must also be sure to use one of these file extensions when writing your output file, otherwise it will not be saved.

The supported output file type and extensions for the mp3 input file are shown in bold (tested on OS X 10.11.6):

  • com.apple.quicktime-movie (.mov)
  • com.apple.m4a-audio (.m4a)
  • public.mpeg-4 (.mp4)
  • com.apple.m4v-video (.m4v)
  • org.3gpp.adaptive-multi-rate-audio (.amr)
  • com.microsoft.waveform-audio (.wav)
  • public.aiff-audio (.aiff)
  • public.aifc-audio (.aifc)
  • com.apple.coreaudio-format (.caf)

If you are not opposed to properly transcoding the audio data to another format, you do not need to use the AVAssetExportPresetPassthrough preset. There are also AVAssetExportPresetLowQuality , AVAssetExportPresetMediumQuality and AVAssetExportPresetHighestQuality . In the following example, the output URL code has the extension .m4a , and the resulting transcode is played in iTunes and other media players:

 AVAsset * asset = [AVAsset assetWithURL:inputURL]; AVAssetExportSession * exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality]; exportSession.outputFileType = AVFileTypeMPEG4; exportSession.outputURL = outputURL; exportSession.metadata = asset.metadata; [exportSession exportAsynchronouslyWithCompletionHandler:^{ if (exportSession.status == AVAssetExportSessionStatusCompleted) { NSLog(@"AV export succeeded."); } else if (exportSession.status == AVAssetExportSessionStatusCancelled) { NSLog(@"AV export cancelled."); } else { NSLog(@"AV export failed with error: %@ (%ld)", exportSession.error.localizedDescription, (long)exportSession.error.code); } }]; 
+7
source share

I tried to export an mp3 audio file from ipod library. and this is my decision below.

 extension DirectoryListViewController: MPMediaPickerControllerDelegate { public func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) { guard let mediaItem = mediaItemCollection.items.first else { ImportExternalFileService.shared.alertImportError(); return } guard let url = mediaItem.assetURL else { ImportExternalFileService.shared.alertImportError(); return } guard let songTitle = mediaItem.title else { ImportExternalFileService.shared.alertImportError(); return } guard let exportSession = AVAssetExportSession(asset: AVURLAsset(url: url), presetName: AVAssetExportPresetAppleM4A) else { ImportExternalFileService.shared.alertImportError(); return } exportSession.outputFileType = .m4a exportSession.metadata = AVURLAsset(url: url).metadata exportSession.shouldOptimizeForNetworkUse = true guard let fileExtension = UTTypeCopyPreferredTagWithClass(exportSession.outputFileType!.rawValue as CFString, kUTTagClassFilenameExtension) else { ImportExternalFileService.shared.alertImportError(); return } let documentURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) let outputURL = documentURL.appendingPathComponent("\(songTitle).\(fileExtension.takeUnretainedValue())") /* Dont't forget to remove the existing url, or exportSession will throw error: can't save file */ do { try FileManager.default.removeItem(at: outputURL) } catch let error as NSError { print(error.debugDescription) } exportSession.outputURL = outputURL exportSession.exportAsynchronously(completionHandler: { if exportSession.status == .completed { DispatchQueue.main.async { ImportExternalFileService.shared.importRecordFile(url: exportSession.outputURL!) } } else { print("AV export failed with error:- ", exportSession.error!.localizedDescription) } }) } public func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) { dismiss(animated: true, completion: nil) } 

}

0
source share

All Articles