-6

我为孩子们开发了一个应用程序,它包含一些练习测试,所以当答案正确时,我编写了代码来显示 alert "correct"。我想要显示警报时的代码"correct"。它也应该与声音一起出现。

4

1 回答 1

2

获取音频的路径,然后使用警报视图启动音频,然后在按下按钮时可以停止它。像这样的东西..

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

   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 1;
[audioPlayer play];
[alert show]; 

在这里停止音频。

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
 if (buttonIndex==0) {

 [audioPlayer stop];

}

注意:添加 AVAudio 框架,如果您不想在 UIAlertView 的按钮被单击之前播放音频,则设置 audioPlayer.numberOfLoops = -1;

于 2013-09-17T07:11:01.737 回答