我的AVAudioPlayer不工作?

时间:2014-03-15 19:30:32

标签: ios objective-c avaudioplayer

我是第一次编程AVAudioPlayer并且它无法工作:(,我的代码:

@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];
     }

}

我称之为:

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

感谢您的帮助,感激不尽!

1 个答案:

答案 0 :(得分:0)

您的文件存储在哪里?我假设您刚刚为项目添加了sound1.caf。您需要指定完整路径:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"caf"];
self.player1 = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:filePath] error:&error];