سؤال

Im programming an AVAudioPlayer for the first time ever and its not working :(, heres my code:

@property (nonatomic, strong) AVAudioPlayer *player1;

- (void)viewDidLoad
{
    [super viewDidLoad];

     NSError *error = nil;
     if (!self.player1) {
    self.player1 = [[AVAudioPlayer alloc]initWithData:[NSData dataWithContentsOfFile:@"sound 1.caf"] error:&error];
    [self.player1 prepareToPlay];
     }

}

And I call it:

- (IBAction)play1:(id)sender {
    [self.player1 play];
}

Thanks your help its appreciated!

هل كانت مفيدة؟

المحلول

Where is your file stored? I assume you just added sound1.caf to your project. You need to specify the full path:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"caf"];
self.player1 = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:filePath] error:&error];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top