如何获取 iPhone 上资源的文件系统路径?

发布于 2024-07-27 17:41:47 字数 1333 浏览 2 评论 0原文

在 iPhone 上,我需要获取资源的路径。 好的,完成了,但是当涉及到 CFURLCreateFromFileSystemRepresentation 的时候,我只是不知道如何解决这个问题。 为什么会出现这个错误? 任何解决方案或解决方法将受到高度赞赏。 先感谢您。

为了在 iPhone 上使用 AudioQueue 播放音频,我查看了以下示例: SpeakHere、AudioQueueTools(来自 SimpleSDK 目录)和 AudioQueueTest。 我试着做这做那,试图把这些谜题拼在一起。 现在,我被困在这个问题上。 由于上面的 sndFile 抛出异常,程序崩溃了。

我正在使用 AVAudioPlayer 来播放 iPhone 游戏中的所有声音。 在真正的 iPhone 设备上,当播放声音时,结果非常滞后,所以我决定需要使用 AudioQueue。

- (id) initWithFile: (NSString*) argv{

    if (self = [super init]){
        NSString *soundFilePath = [[NSBundle mainBundle]
                                    pathForResource:argv
                                             ofType:@"mp3"];
        int len = [soundFilePath length];
        char* fpath = new char[len];

        //this is for changing NSString into char* to match
        //CFURLCreateFromFileSystemRepresentation function's requirement.
        for (int i = 0; i < [soundFilePath length]; i++){
            fpath[i] = [soundFilePath characterAtIndex:i];
        }

        CFURLRef sndFile = CFURLCreateFromFileSystemRepresentation
                           (NULL, (const UInt8 *)fpath, strlen(fpath), false);
        if (!sndFile) {
            NSLog(@"sndFile error");
            XThrowIfError (!sndFile, "can't parse file path");
        }
}

On the iPhone I need to get the path for the resource. OK, done that, but when it comes to CFURLCreateFromFileSystemRepresentation thing, I just don't know how to solve this. Why does this error occur? Any solution or workaround would be highly appreciated. Thank you in advance.

I have taken a look at the following examples in order to play audio using AudioQueue on the iPhone:
SpeakHere, AudioQueueTools (from the SimpleSDK directory) and AudioQueueTest. I tried do this and that, trying to put the puzzles together. Right now, I am stuck at this. The program crashed because of the exception thrown from the sndFile above.

I am using AVAudioPlayer to play every sound on my iPhone games. On the real iPhone device, it turned out to be very laggy when a sound is to be played, so I decided I need to use AudioQueue.

- (id) initWithFile: (NSString*) argv{

    if (self = [super init]){
        NSString *soundFilePath = [[NSBundle mainBundle]
                                    pathForResource:argv
                                             ofType:@"mp3"];
        int len = [soundFilePath length];
        char* fpath = new char[len];

        //this is for changing NSString into char* to match
        //CFURLCreateFromFileSystemRepresentation function's requirement.
        for (int i = 0; i < [soundFilePath length]; i++){
            fpath[i] = [soundFilePath characterAtIndex:i];
        }

        CFURLRef sndFile = CFURLCreateFromFileSystemRepresentation
                           (NULL, (const UInt8 *)fpath, strlen(fpath), false);
        if (!sndFile) {
            NSLog(@"sndFile error");
            XThrowIfError (!sndFile, "can't parse file path");
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲欢浪云 2024-08-03 17:41:47

为什么需要 CFURL?

如果您在其他地方有需要 CFURL 的方法,那么借助免费桥接,您可以简单地使用 NSURL。 因此,要创建 NSURL,您只需执行以下操作:

  NSString * soundFilePath = [[NSBundle mainBundle]
                                 pathForResource:argv
                                          ofType:@"mp3"];

  NSURL *soundURL = [NSURL fileURLWithPath:soundFilePath];

一般来说,如果您发现自己使用 CF 对象,那么您可能做错了什么。

Why do you need a CFURL?

If you have a method elsewhere that requires a CFURL, you can simply use an NSURL thanks to toll-free bridging. So to create the NSURL, you'd just do:

  NSString * soundFilePath = [[NSBundle mainBundle]
                                 pathForResource:argv
                                          ofType:@"mp3"];

  NSURL *soundURL = [NSURL fileURLWithPath:soundFilePath];

In general, if you find yourself using CF objects you are probably doing something wrong.

↘紸啶 2024-08-03 17:41:47

我不确定这是否会消除您的异常,但有一种更简单的方法可以将 NSString 转换为 char 数组。 以下是我编写此方法的方法:

- (id) initWithFile:(NSString*) argv
{
    if ((self = [super init]) == nil) { return nil; }

    NSString * soundFilePath = [[NSBundle mainBundle]
                                 pathForResource:argv
                                          ofType:@"mp3"];
    CFURLRef sndFile = CFURLCreateFromFileSystemRepresentation
                       (NULL, [soundFilePath UTF8String],
                        [soundFilePath length], NO);

    if (!sndFile) { NSLog(@"sndFile error"); }
    XThrowIfError (!sndFile, "can't parse file path");

    ...
}

或者,由于 CFURLNSURL 是“免费桥接”,因此您可以简单地执行以下操作:

- (id) initWithFile:(NSString*) argv
{
    if ((self = [super init]) == nil) { return nil; }

    NSString * soundFilePath = [[NSBundle mainBundle]
                                 pathForResource:argv
                                          ofType:@"mp3"];
    NSURL * sndFile = [NSURL URLWithString:[soundFilePath
                       stringByAddingPercentEscapesUsingEncoding:
                         NSUTF8StringEncoding]];
    if (!sndFile) { NSLog(@"sndFile error"); }
    XThrowIfError (!sndFile, "can't parse file path");

    ...
}

I'm not sure if this will get rid of your exception, but there is a simpler way to convert an NSString to an array of char. Here is how I would write this method:

- (id) initWithFile:(NSString*) argv
{
    if ((self = [super init]) == nil) { return nil; }

    NSString * soundFilePath = [[NSBundle mainBundle]
                                 pathForResource:argv
                                          ofType:@"mp3"];
    CFURLRef sndFile = CFURLCreateFromFileSystemRepresentation
                       (NULL, [soundFilePath UTF8String],
                        [soundFilePath length], NO);

    if (!sndFile) { NSLog(@"sndFile error"); }
    XThrowIfError (!sndFile, "can't parse file path");

    ...
}

Or, since CFURL is "toll-free bridged" with NSURL, you can simply do:

- (id) initWithFile:(NSString*) argv
{
    if ((self = [super init]) == nil) { return nil; }

    NSString * soundFilePath = [[NSBundle mainBundle]
                                 pathForResource:argv
                                          ofType:@"mp3"];
    NSURL * sndFile = [NSURL URLWithString:[soundFilePath
                       stringByAddingPercentEscapesUsingEncoding:
                         NSUTF8StringEncoding]];
    if (!sndFile) { NSLog(@"sndFile error"); }
    XThrowIfError (!sndFile, "can't parse file path");

    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文