触发AVAudioPlayer以停止所有声音并播放选定的内容,但单击“播放"(选定)按钮时除外 [英] Trigger AVAudioPlayer to stop all sounds and play selected except when playing(selected) button is clicked

查看:73
本文介绍了触发AVAudioPlayer以停止所有声音并播放选定的内容,但单击“播放"(选定)按钮时除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个声音板应用程序,一次播放一个声音.我希望UIButton能够停止所有其他声音的播放并在单击时开始播放其自己的声音.我设置了一个AVAudioPlayer来播放所有声音(单击时会更改声音文件的URL,并且由于我一次只想播放一个声音,所以这不是问题).我想知道如何停止所有其他声音并根据按下的按钮播放声音,但是如果单击的按钮是当前播放声音的按钮,则该按钮只会停止声音.我知道这可以通过转到单独的音频播放器并触发stop事件来实现,但是我想避免复制和粘贴代码并尽可能保持效率,再加上我只希望/需要一个音频播放器.这是我的代码:

I am creating a sound board app that plays one sound at a time. I would like a UIButton to be able to stop all other sounds playing and start playing its own sound when clicked. I have a single AVAudioPlayer set up to play all sounds (I change the sound file url when clicked and since I only want one sound at a time playing this is not an issue). I would like to know how to stop all other sounds and play the sound according to the button pressed, but if the button clicked is the button with the sound currently playing, the button only stops the sound. I know this is achievable through going to separate audioplayers and triggering the stop event but I want to avoid copy and pasting code and to stay efficient as possible, plus I only want / need a single audioplayer. Here is my code:

- (IBAction)play {
 if (audioPlayer.playing == NO) {
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/k.mp3", [[NSBundle mainBundle] resourcePath]]];
    NSError *error;
     [audioPlayer release];
     audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = 0;

    AVAudioSession *audiosession = [AVAudioSession sharedInstance];
    [audiosession setCategory:AVAudioSessionCategoryAmbient error:nil];

    [audioPlayer play];
    [start setTitle:@"Stop" forState:UIControlStateNormal];
}
 else
     if (audioPlayer.playing == YES) {
         [audioPlayer stop];
     [start setTitle:@"Start" forState:UIControlStateNormal];
    }
    }


- (IBAction)play2 {
if (audioPlayer.playing == NO) {
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/chicken.mp3", [[NSBundle mainBundle] resourcePath]]];
    NSError *error;
    [audioPlayer release];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = 0;

    AVAudioSession *audiosession = [AVAudioSession sharedInstance];
    [audiosession setCategory:AVAudioSessionCategoryAmbient error:nil];

    [audioPlayer play];
    [start2 setTitle:@"Stop" forState:UIControlStateNormal];
} else
    if (audioPlayer.playing == YES) {
        [audioPlayer stop];
        [start2 setTitle:@"Start" forState:UIControlStateNormal];
    }
}

感谢您的帮助.预先谢谢你!

Any help is appreciated. Thank you in advance!

推荐答案

只需执行此操作

-(void)stopAudio{

if(audioPlayer && [audioPlayer isPlaying]){
    [audioPlayer stop];
    audioPlayer=nil;
}

}

并在每次单击时调用此函数.

And call this function on every click..

- (IBAction)play2 {
    [self stopAudio];

    //Do your Stuffs
}

这篇关于触发AVAudioPlayer以停止所有声音并播放选定的内容,但单击“播放"(选定)按钮时除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆