1

下面是我无法播放 SnareHitSample.wav 文件的代码。我不知道我哪里错了。

-(void)playAudioFiles
{
    NSString * path;
    AVAudioPlayer * snd;
    NSError * err;
    NSString *name;
    name = @"SnareHitSample.wav";
    path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name];

    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        NSURL * url = [NSURL fileURLWithPath:path];
        snd = [[AVAudioPlayer alloc] initWithContentsOfURL:url
                                                      error:&err] ;
        if (! snd) {
            NSLog(@"Sound named '%@' had error %@", name, [err localizedDescription]);
        } else {
            [snd prepareToPlay];
        }
    } else {
        NSLog(@"Sound file '%@' doesn't exist at '%@'", name, path);
    }
}
4

3 回答 3

7

谢谢大家,我在 .h 文件中声明了 avaudioplayer 对象,我的问题得到了解决。

  {
 AVAudioPlayer *player1;
  }
于 2013-02-19T07:42:24.250 回答
5

Try this code.

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:@"SnareHitSample"
                                     ofType:@"wav"]];
NSError *error = nil;
AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc]
               initWithContentsOfURL:url
               error:&error];
if (error)
{
  NSLog(@"Error in audioPlayer: %@",[error localizedDescription]);
}
else
{
    audioPlayer.delegate = self;
    [audioPlayer play];
    [audioPlayer setNumberOfLoops:INT32_MAX]; // for continuous play
}
于 2013-02-19T06:59:48.357 回答
1

试试下面的代码:

AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] 
                                                                                      pathForResource:@"SnareHitSample" ofType:@"wav"]] error:NULL];
       [theAudio play];

你会摆脱这个..任何查询都会回复我..

于 2013-02-19T07:06:04.507 回答