ios - AVAsset 无法识别 NSURL - 崩溃 - iOS

标签 ios objective-c filepath nsurl avassetreader

我正在制作一个 iOS 应用。它的特点之一是可以保存录音和保存音频文件。

为了保存录制的音频文件的 URL,我将它们作为文件路径存储在 NSUserDefaults 中,如下所示:

file:///var/mobile/Containers/Data/Application/17F8E799-D7E1-4B3C-9DFE-7BA48E346314/Documents/recordName.aif

我正在使用名为“FVSoundWaveDemo”的库来尝试显示录制文件的声波。但是我的 URL 有问题......

“FVSoundWaveDemo”中的示例加载本地导入的文件,如下所示:

NSString* filename = [NSString stringWithFormat:@"song1.m4a"];
NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:filename ofType:nil]];
_soundWaveView.soundURL = url;

您看到带有“song1.m4a”的位,我想替换它并让应用程序改用文件路径 URL。就像我在上面的问题中显示的文件路径 URL。

我已经尝试过各种方法,例如尝试将 filePath“转换”为普通 URL 并尝试使用 AVAssets 等。但是我尝试的一切都出现了这个错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

这是因为我正在使用的库像这样使用 AVAssets:

AVAssetReader* reader = [[AVAssetReader alloc] initWithAsset:songAsset error:&error];

AVAssetTrack* songTrack = [songAsset.tracks objectAtIndex:0];

NSDictionary* outputSettingsDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                                    [NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
                                    [NSNumber numberWithInt:1],AVNumberOfChannelsKey,
                                    [NSNumber numberWithInt:8],AVLinearPCMBitDepthKey,
                                    [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
                                    [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                                    [NSNumber numberWithBool:NO],AVLinearPCMIsNonInterleaved,
                                    nil];

那么我如何才能让 AVAsset 只与文件路径 URL 一起工作呢?

请记住,我试图让它查看应用程序保存的录制音频文件,不是开发人员可以添加到 Xcode 项目中的本地导入音频文件。

我已经坚持了好几天了,如果你能帮助我,那将不胜感激,谢谢。

谢谢你抽出时间,丹。

最佳答案

最后想通了。我进行了以下更改以使其正常工作:

1) 我像这样使用 fileURLWithPath 而不是 URLWithString:

NSURL *assetURL = [NSURL fileURLWithPath:passURL];
AVURLAsset *asset = [AVURLAsset assetWithURL:assetURL];

2) 在保存音频文件 URL 之前,我将其保存为 NSString,如下所示:

file:///var/mobile/Containers/Data/Application/17F8E799-D7E1-4B3C-9DFE-7BA48E346314/Documents/recordName.aif

当您想使用 AVPlayer 之类的东西时,这不起作用。相反,您必须确保保存 URL,使其在字符串的开头包含“file:///”位。为此,请确保保存字符串路径值而不是字符串路径的 NSURL,如下所示:

    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath_ = [searchPaths objectAtIndex:0];
    NSString *pathToSave = [documentPath_ stringByAppendingPathComponent:@"audioName.aif"];

现在将“pathToSave”NSString 保存到您想要的任何位置,并在您想要加载文件时使用它。这样做会导致音频文件 URL 像这样被保存:

/var/mobile/Containers/Data/Application/45ABF4BD-FA0E-43F7-B936-7DE29D8F28ED/Documents/23Feb2015_074118pm.aif

您会看到它的开头没有“file:///”位。该字符串现在可用于此答案的第 1 部分。

希望这对遇到与我类似问题的人有帮助。

关于ios - AVAsset 无法识别 NSURL - 崩溃 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28659944/

相关文章:

ios - 检测在 arkit 上点击了哪个平面

ios - 使用闭包而不是 UIBarButtonItem 的选择器参数但不使用弱 self

objective-c - Objective-c 上的指针

objective-c - 关于ARC中只读@property的问题

python - 如何在运行时更改目录并将文件存储在不同位置

c#类库使用文件

javascript - sendFile 不是基于 Express 的 Node 应用程序中的函数?

ios - 如何将数据发送到 iBeacon ?

ios - 加载对象 x-apple-eventkit 的默认属性时出错

ios - Xcode 应用程序提交错误 : Client unable to connect to server