AVAudioPlayer UIButton - 第二次触摸停止当前音频播放

发布于 2024-10-21 07:36:57 字数 683 浏览 1 评论 0原文

我有一个 UIButton,触摸时会播放音频文件。目前,连续触摸按钮两次会播放音频文件两次,而不是停止播放。

我希望第二次单击停止音频文件并且不播放第二个实例。任何建议将不胜感激。谢谢

-(IBAction) buttonNoisePlay {

        NSString *pathsoundFile = [[NSBundle mainBundle] pathForResource:@"noise" ofType:@"mp4"];
        sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathsoundFile] error:NULL];
        sound.delegate = self;
        sound.numberOfLoops = -1;
        sound.volume = 1;   

            if(sound == nil || sound.playing == NO)  //if not playing
            {

                    [sound play];
            }
            else
            {
                    [sound stop];
            }
}

I have a UIButton that when touched plays an audio file. At the moment touching the button twice in succession plays the audio file twice instead of stopping it.

I'm looking to have the second click stop the audio file and not play a second instance. any suggestions would be very much appreciated. thanks

-(IBAction) buttonNoisePlay {

        NSString *pathsoundFile = [[NSBundle mainBundle] pathForResource:@"noise" ofType:@"mp4"];
        sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathsoundFile] error:NULL];
        sound.delegate = self;
        sound.numberOfLoops = -1;
        sound.volume = 1;   

            if(sound == nil || sound.playing == NO)  //if not playing
            {

                    [sound play];
            }
            else
            {
                    [sound stop];
            }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

傲世九天 2024-10-28 07:36:58

你可以这样做:

-(IBAction) buttonNoisePlay {

    if(sound == nil || sound.playing == NO)  //if not playing
    {
        ...
        //if nil initialize

        [sound play];
    }
    else
    {
        [sound stop];
    }
}

You could do something like this:

-(IBAction) buttonNoisePlay {

    if(sound == nil || sound.playing == NO)  //if not playing
    {
        ...
        //if nil initialize

        [sound play];
    }
    else
    {
        [sound stop];
    }
}
以可爱出名 2024-10-28 07:36:57

嗯,这就是我所做的&它只播放一次:

-(void) playSound: (NSString *) strFileName
{       
    if(audioPlayer == nil || audioPlayer.playing == NO)  //check if it's already playing in case they dbl-tapped (or more) b/c we only want to play it once per found item
    {
        //Get the filename of the sound file:
        NSString *urlAddress = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], strFileName];

        //Get a URL for the sound file
        NSURL *url = [NSURL fileURLWithPath:urlAddress];
        NSError *error;

        //Use AVAudioPlayer to play the sound
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        audioPlayer.numberOfLoops = 0;

        if (audioPlayer == nil)
            NSLog(@"audioPlayer nil Error:%@", [error description]);
        else
            [audioPlayer play];
    }
}

并且在标题中:

AVAudioPlayer *audioPlayer;

我可能还应该在初始化audioPlayer之前检查nil,所以它只完成一次......但现在 - 它有效。

hmm, this is what I do & it only plays once:

-(void) playSound: (NSString *) strFileName
{       
    if(audioPlayer == nil || audioPlayer.playing == NO)  //check if it's already playing in case they dbl-tapped (or more) b/c we only want to play it once per found item
    {
        //Get the filename of the sound file:
        NSString *urlAddress = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], strFileName];

        //Get a URL for the sound file
        NSURL *url = [NSURL fileURLWithPath:urlAddress];
        NSError *error;

        //Use AVAudioPlayer to play the sound
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        audioPlayer.numberOfLoops = 0;

        if (audioPlayer == nil)
            NSLog(@"audioPlayer nil Error:%@", [error description]);
        else
            [audioPlayer play];
    }
}

And in the header:

AVAudioPlayer *audioPlayer;

I should probably also be checking for nil before initializing the audioPlayer, so it's only done once... but for now - it works.

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