iOS音效播放

当开始播放时压低其他app的声音,播放结束后恢复正常

//MARK: 开始播放音效
            [[soundBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton * value) {
                NSError * setCategoryError = nil;
                if(![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionDuckOthers error:&setCategoryError]){
                    [SVProgressHUD showErrorWithStatus:@"音效文件已损坏,请重新上传"];
                }else{
                    [[AVAudioSession sharedInstance] setActive:YES error:nil];
                    [_audioPlayer play];
                    [value.imageView startAnimating];
                }
            }];
            
            //MARK: 停止播放音效
            [[self rac_signalForSelector:@selector(audioPlayerDidFinishPlaying:successfully:) fromProtocol:@protocol(AVAudioPlayerDelegate)] subscribeNext:^(id x) {
                [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
                [_audioPlayer stop];
                [soundBtn.imageView stopAnimating];
            }];

更详细的文章请看http://www.jianshu.com/p/3e0a399380df

你可能感兴趣的:(iOS音效播放)