我必须开发一个应用程序,在用户吹蜡烛后播放声音,而不是声音应该播放。对于吹蜡烛,我使用AVAudioRecorder捕捉用户吹,然后它开始停止蜡烛的动画,然后它应该开始播放歌曲,但它没有播放歌曲。我在本地存储了歌曲。(在我的申请中)
为AVAudioRecorder NSURL *url1 = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error1;
recorder = [[AVAudioRecorder alloc] initWithURL:url1 settings:settings error:&error1];
动画图像开始,之后应该播放歌曲。
imageView1.animationImages= [NSArray arrayWithObjects:
[UIImage imageNamed:@"can6.png"],
[UIImage imageNamed:@"can7.png"],
[UIImage imageNamed:@"can8.png"],
[UIImage imageNamed:@"can9.png"],
[UIImage imageNamed:@"can10.png"],nil];
[imageView1 setAnimationRepeatCount:1];
imageView1.animationDuration=0.7;
[imageView1 startAnimating];
[recorder stop];
player = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:xyz song ofType:nil]]
error:nil];
NSLog(@"music play-%@",xyz song);
[player prepareToPlay];
[player play];
所有代码都工作正常,只有声音没有播放。我得到蜡烛停止动画开始,但我不能在这里的歌曲。虽然上面的声音播放代码在另一个视图中工作得很好,它播放了歌曲,但在这里却没有播放歌曲。
检查[[NSBundle mainBundle] pathForResource:xyz song ofType:nil]的结果是否为nil。我认为问题可能是传递nil for type。例如,如果你的歌曲是HappyBirthday.mp3,你应该调用
[[NSBundle mainBundle] pathForResource:@"HappyBirthday" ofType:@"mp3"];