我想从didSelectRowAtIndexPath实例中的UITableView项目控制AVAudioPlayer的实例。didSelectRowAtIndexPath - 访问tapCount或类似
首先触摸行项触发AVAudioPlayer的“播放”。第二次触摸行项会触发AVAudioPlayer的“停止”。
我可以让'游戏'工作,但不能'停止'工作。此外,后续触摸行项目会在后台启动另一个音频线程。
确保1次敲击开始音频和2次敲击停止音频的最佳方式是什么?
代码样本 - 这种方法PREPS音频文件和AVAudioPlayer使用:
- (void)makeReadyAudio {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Murderers" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[musicPlayer prepareToPlay];
}
此块将开始&在didSelectRowAtIndexPath方法部分的情况下,语句中停止AVAudioPlayer:
case 7: {
//touch to start audio sound playing, touch again to stop playing
[self makeReadyAudio];
if ([musicPlayer isPlaying]) {
[musicPlayer stop];
NSLog(@"musicPlayer tested to be playing, so stop it.");
} else {
[musicPlayer play];
NSLog(@"musicPlayer tested to be *not* playing, so play it.");
}
break;
}
如果([玩家IsPlaying模块]){ \t [播放停止]。 \t NSLog(@“玩家测试要玩,所以停下来。”); } else { \t [player play]; (@“玩家测试*不*玩*,所以玩它。”); } 看似合理,但没有运气。同样的行为,'真正'的测试案例似乎永远不会运行。重复触摸选定的行打印“播放器测试*不*播放,所以播放它”一遍又一遍地在日志中...一边再次播放音频。 嗯。 – pteeter 2010-08-30 22:21:21
然后显示更多代码。听起来像错误是在别的地方。比如你的AVAudioPlayer代码。 – 2010-08-31 00:25:51
代码添加到上面的问题陈述...对不起支持标记标签不是最直观的@ StackOverflow。 – pteeter 2010-08-31 01:59:34