didSelectRowAtIndexPath - 访问tapCount或类似的

我想从didSelectRowAtIndexPath实例中的UITableView项控制AVAudioPlayer的实例。 首次触摸行项会触发AVAudioPlayer的“播放”。第二次触摸行项触发AVAudioPlayer的“停止”。 我可以让'玩'工作,但不能让'停止'工作。此外,行项的后续触摸在后台启动音频的另一个线程。 什么是确保1点击启动音频的最佳方法,第2点点击是否停止? 代码示例 - 此方法准备使用音频文件和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部分的case语句中停止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;
    }
    
已邀请:
怎么样:
- (void) didSelectRowAtIndexPath:
{
    if (player.isPlaying) {
        [player stop];
    } else {
        [player start];
    }
}
换句话说,让玩家保持身边,看看它在做什么。     
问题是每次敲击行都会调用
makeReadyAudio
。你需要检查一下你是否已经这样做了。例如,通过执行以下操作:
if (musicPlayer == nil) {
    [self makeReadyAudio];
}
    

要回复问题请先登录注册