didSelectRowAtIndexPath - 访问 tapCount 或类似的

发布于 2024-09-16 18:16:41 字数 1114 浏览 3 评论 0原文

我想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以酷 2024-09-23 18:16:41

怎么样:

- (void) didSelectRowAtIndexPath:
{
    if (player.isPlaying) {
        [player stop];
    } else {
        [player start];
    }
}

换句话说,让玩家留在身边,看看它在做什么。

How about:

- (void) didSelectRowAtIndexPath:
{
    if (player.isPlaying) {
        [player stop];
    } else {
        [player start];
    }
}

In other words, keep the player around and see what it is doing.

撩心不撩汉 2024-09-23 18:16:41

问题是每次点击该行时您都会调用 makeReadyAudio 。您需要检查您是否已经这样做了。例如,通过执行以下操作:

if (musicPlayer == nil) {
    [self 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:

if (musicPlayer == nil) {
    [self makeReadyAudio];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文