ios - AVAsset 轨道为空

标签 ios objective-c avfoundation

本质上,我希望连接 AVAsset 文件。我大致知道该怎么做,但我正在努力加载音频文件。

我可以使用 AVAudioPlayer 播放文件,我可以通过我的终端在目录中看到它们,但是当我尝试使用 AVAssetURL 加载它们时,它总是返回一个轨道的空数组。

我使用的网址:

NSURL *firstAudioFileLocation = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%@", workingDirectory , @"/temp.pcm"]];

结果是:

file:///Users/evolve/Library/Developer/CoreSimulator/Devices/8BF465E8-321C-47E6-BF2E-049C5E900F3C/data/Containers/Data/Application/4A2D29B2-E5B4-4D07-AE6B-1DD15F5E59A3/Documents/temp.pcm



正在加载的 Assets :

AVAsset *test = [AVURLAsset URLAssetWithURL:firstAudioFileLocation options:nil];

但是调用这个时:

NSLog(@" total tracks %@", test.tracks);

我的输出总是total tracks ()

我随后将它们添加到我的 AVMutableCompositionTrack 的调用最终导致应用程序崩溃,因为 AVAsset 似乎没有正确加载。



我玩过其他加载 Assets 的变体,包括:

NSURL *alternativeLocation = [[NSBundle mainBundle] URLForResource:@"temp" withExtension:@"pcm"];

以及尝试使用文档中的选项加载 AVAsset:

NSDictionary *assetOptions = @{AVURLAssetPreferPreciseDurationAndTimingKey: @YES};



如何从最近由 AVAudioRecorder 创建的本地资源 加载轨道?

编辑

我四处寻找,发现我可以录制和加载 .CAF 文件扩展名。

似乎 .PCM 不支持 AVAsset,这个页面也很有帮助。 https://developer.apple.com/documentation/avfoundation/avfiletype

最佳答案

AVAsset 加载不是瞬时的。您需要等待数据可用。示例:

AVAsset *test = [AVURLAsset URLAssetWithURL:firstAudioFileLocation options:nil];
[test loadValuesAsynchronouslyForKeys:@[@"playable",@"tracks"] completionHandler:^{

    // Now tracks is available
    NSLog(@" total tracks %@", test.tracks);
}];

可以找到更详细的例子in the documentation .

关于ios - AVAsset 轨道为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47816292/

相关文章:

ios - 是否可以编辑 UIAlertAction 标题字体大小和样式?

ios - 创建开发人员和生产证书问题?

ios - 删除尺寸类的 UICollectionView

avfoundation - 如何从具有正确方向的 AVCapturePhoto 生成 UIImage?

objective-c - UIViewReportBrokenSuperviewChain

objective-c - Xcode 6 - 按下按钮播放声音

ios - Codename one issue 为 iOS 开发二进制文件

iOS 9 - NSUserDefault 未设置

objective-c - 如何重新创建 iMessage 发送按钮和文本字段布局?

ios - NSPredicate 收集 NSManagedObject 请求失败