本质上,我希望连接 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/