1

嗨,有谁知道如何向按钮添加音频我已经添加了框架创建了一个放置声音的动作,但是当我按下按钮时没有声音播放?

这是我到目前为止所做的

-(void)buttonSound{


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

    NSError *error;
    audioPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer2.numberOfLoops = -1;

    [audioPlayer2 play];

}

- (IBAction)backToMenu:(id)sender {
    [self buttonSound];
    [self.navigationController popViewControllerAnimated:YES];
}
4

2 回答 2

1

对于简短的系统声音,您应该使用以下内容:

SystemSoundID sound;
CFURLRef alertURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (CFStringRef)@"ButtonSound", CFSTR("mp3"), NULL);
OSStatus status = AudioServicesCreateSystemSoundID(alertURL, &sound);

AudioServicesPlayAlertSound(sound);
于 2012-05-22T13:14:52.110 回答
0

通过立即弹出导航控制器释放您的音频播放器。

如果删除此行:

[self.navigationController popViewControllerAnimated:YES];

您应该能够听到音频文件(如果路径正确)。

于 2012-05-22T13:13:40.943 回答