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;
}
I'd like to control an instance of AVAudioPlayer from a UITableView item in a didSelectRowAtIndexPath instance.
First touch of the row item triggers 'play' of the AVAudioPlayer. Second touch of the row item triggers 'stop' of the AVAudioPlayer.
I can make the 'play' work but can't get the 'stop' to work. Also, subsequent touches of the row item starts another thread of the audio in the background.
What's the best way to ensure 1 tap starts the audio and a 2nd tap stops it?
Code samples - this method preps audio file and AVAudioPlayer for use:
- (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];
}
This block will start & stop the AVAudioPlayer within a case statement in the didSelectRowAtIndexPath section:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样:
换句话说,让玩家留在身边,看看它在做什么。
How about:
In other words, keep the player around and see what it is doing.
问题是每次点击该行时您都会调用
makeReadyAudio
。您需要检查您是否已经这样做了。例如,通过执行以下操作:The problem is that you call
makeReadyAudio
every single time the row is tapped. You need to check if you have already done that. For example by doing something like: