2012-08-09 3 views
0

내 xcode 프로젝트의 Supporting Files 폴더에 넣은 crashSound.wav 파일에 어떻게 액세스합니까? 다음 코드가 작동하지 않기 때문에 :initWithContentsOfURL : error :?를 사용하여 사운드 파일을로드하는 방법

@interface Game() 
{ 
    // code... 

    AVAudioPlayer *crashSound; 

    // code... 
} 
@end 

@implementation Game 

- (id) init 
{ 
    // code... 

    NSURL *crashSoundFile = [[NSURL alloc] initWithString:@"crashSound" ]; // <--PROBLEM 

    crashSound = [[AVAudioPlayer alloc] initWithContentsOfURL:crashSoundFile error:NULL]; 

    // code... 
} 
-(void) play // this is my "main" method that will be called once the playButton is pressed 
{ 
    [crashSound play];[crashSound play];[crashSound play]; 

} 
@end 

답변

1

@"crashSound"은 유효한 URL이 아닙니다. 실제 URL을 제공해야합니다.

프로젝트의 이미지, 소리 등 (자원)에 대한 URL은 컴파일 타임에 예측할 수 없으므로 런타임에 생성해야합니다. NSBundle을 사용하면 간단합니다.

NSBundle *bundle = [NSBundle mainBundle]; 
NSURL *crashSoundFile = [bundle URLForResource: @"crashSound" withExtension: @"wav"]; 

Ta da.

+0

조언 해 주셔서 감사합니다. 바로 할 수 있습니다. –

0

NSURL * url = [NSURL fileURLWithPath : [NSString stringWithFormat : @ "% @/soundfile.extension", [[NSBundle mainBundle] resourcePath]]]];

관련 문제