xcode 触发 AVAudioPlayer 停止所有声音并播放选定的除非单击播放(选定)按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15357126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:57:09  来源:igfitidea点击:

Trigger AVAudioPlayer to stop all sounds and play selected except when playing(selected) button is clicked

iosobjective-cxcodeavaudioplayer

提问by Alex

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:

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

- (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!

任何帮助表示赞赏。先感谢您!

回答by iphonic

Just do this

就这样做

-(void)stopAudio{

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

}

And call this function on every click..

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

- (IBAction)play2 {
    [self stopAudio];

    //Do your Stuffs
}

回答by Shah Paneri

when your button is clicked stop your audio player and assign your new song url and then play it.Write this code when you click on button to play song.

当您的按钮被点击时,停止您的音频播放器并分配您的新歌曲网址,然后播放它。当您点击按钮播放歌曲时编写此代码。

if(audioPlayer.isPlaying)
      [audioPlayer stop];    

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
NSURL *url = [NSURL fileURLWithPath:YourSoundURL];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.delegate=self;
[audioPlayer play];
[start2 setTitle:@"Stop" forState:UIControlStateNormal];