微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我现在正在尝试导出一个播放器使用AVPlayer(使用网址)的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仅支持使用AVAssetExportPresetPassthrough预设的com.apple.quicktime-movie(.mov)和com.apple.coreaudio-format(.caf)进行mp3转码的文件类型.在编写输出文件时,您还必须确保使用其中一个文件扩展名,否则将无法保存.

支持输出文件类型和mp3输入文件的扩展名以粗体显示(在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)

如果您不介意将音频数据转换为其他格式,则您不必使用AVAssetExportPresetPassthrough预设.还有AVAssetExportPresetLowQuality,AVAssetExportPresetMediumQuality和AVAssetExportPresetHighestQuality.在随后的示例代码中,输出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);
    }
}];

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐