gpt4 book ai didi

ios - 如何使用 AVAssetExportSession 导出 AVPlayer 音频 mp3 文件?

转载 作者:可可西里 更新时间:2023-11-01 05:19:44 24 4
gpt4 key购买 nike

我现在正在尝试导出一个使用 AVPlayer(使用 url)播放的 mp3 文件,因此不必下载两次。

这是我的示例代码:

我已经尝试了每个 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");
}

如果这不可能完成,是否有任何变通办法?

另外,我可以更改音频文件的格式。使用 AVAudioPlayer 的理想类型是什么?

最佳答案

AVAssetExportSession 似乎只支持使用 com.apple.quicktime-movie (.mov) 和 com.apple.coreaudio-format< 进行 mp3 转码的文件类型/em> (.caf) 使用 AVAssetExportPresetPassthrough 预设。您还必须确保在编写输出文件时使用这些文件扩展名之一,否则将无法保存。

mp3 输入文件支持的输出文件类型和扩展名以粗体显示(在 OS X 10.11.6 上测试):

  • com.apple.quicktime-movie (.mov)
  • com.apple.m4a-音频 (.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)

如果您不介意将音频数据正确转码为另一种格式,则不必使用 AVAssetExportPresetPassthrough 预设。还有 AVAssetExportPresetLowQualityAVAssetExportPresetMediumQualityAVAssetExportPresetHighestQuality。在下面的示例代码中,输出 URL 的扩展名为 .m4a,生成的转码在 iTunes 和其他媒体播放器中播放:

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);
}
}];

关于ios - 如何使用 AVAssetExportSession 导出 AVPlayer 音频 mp3 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37819867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com