【问题标题】:AVAsset not recognising NSURL - Crashing - iOSAVAsset 无法识别 NSURL - 崩溃 - iOS
【发布时间】:2015-04-23 23:02:02
【问题描述】:

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

为了保存录制的音频文件的 URL,我将它们作为 filePaths 存储在 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”的位,我想替换它并让应用程序改用 filePath URL。就像我在上面的问题中显示的 filePath URL 一样。

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

由于未捕获的异常“NSRangeException”而终止应用,原因:“*** -[__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 只与 filePath URL 一起工作呢?

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

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

感谢您的宝贵时间,丹。

【问题讨论】:

    标签: ios objective-c filepath nsurl avassetreader


    【解决方案1】:

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

    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 部分一起使用。

    希望这可以帮助任何与我有类似问题的人。

    【讨论】:

    • 哇,这真的救了我。我很惊讶这个答案很难找到。谢谢!!
    • 否则删除 "file://' 只需执行 [yourURL path] 即可。
    【解决方案2】:

    这样的?

    NSURL* assetURL = [NSURL URLWithString:@"file://your-file.aif"];
    AVURLAsset* asset  = [AVURLAsset URLAssetWithURL:assetURL options:nil];
    

    【讨论】:

    • 刚刚试了一下,还是不行:(还有什么想法吗?
    • 顺便感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    相关资源
    最近更新 更多