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

Kar*_*arl 4 iphone core-audio audioqueueservices

在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");
        }
}
Run Code Online (Sandbox Code Playgroud)

Ken*_*ner 11

你为什么需要CFURL?

如果你在其他地方有一个需要CFURL的方法,你可以简单地使用NSURL,这要归功于免费桥接.所以要创建NSURL,你只需:

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

  NSURL *soundURL = [NSURL fileURLWithPath:soundFilePath];
Run Code Online (Sandbox Code Playgroud)

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