4

iPhoneでは、リソースのパスを取得する必要があります。OK、それはできましたが、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");
        }
}
4

2 に答える 2

11

なぜCFURLが必要なのですか?

CFURLを必要とするメソッドが他の場所にある場合は、フリーダイヤルブリッジのおかげでNSURLを使用できます。したがって、NSURLを作成するには、次のようにします。

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

  NSURL *soundURL = [NSURL fileURLWithPath:soundFilePath];

一般に、CFオブジェクトを使用していることに気付いた場合は、おそらく何か間違ったことをしていることになります。

于 2009-07-24T17:48:09.687 に答える
0

これで例外がなくなるかどうかはわかりませんが、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");

    ...
}

または、CFURLは「フリーダイヤルブリッジ」でNSURLあるため、次のように簡単に実行できます。

- (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");

    ...
}
于 2009-07-24T14:06:29.880 に答える