首页 > 解决方案 > 同时播放2个音频objective-c

问题描述

我有一个 AVPlayer 与摄像机的实时流一起运行,当用户触摸按钮拍摄快照时,我想播放另一个音频(摄像机快照音频效果)

所以我添加了这段代码

- (void)screenFlash {
NSString *soundFilePath = [NSString stringWithFormat:@"%@/Snapshoot.m4a",[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = 1;

[player play];

UIView *flash = [[UIView alloc] initWithFrame:self.view.frame];
flash.backgroundColor = UIColor.whiteColor;
flash.alpha=1;

[self.view addSubview:flash];

[UIView animateWithDuration:0.3 animations:^{
    flash.alpha=0;
} completion:^(BOOL finished) {
    [flash removeFromSuperview];
}];
}

如果我在 [播放器播放] 之前放置一个断点,音频听起来很完美,但如果我禁用断点,音频就不会重现。

有人知道为什么吗?我可以同时播放两个音频(相机音频和快照效果)吗?

标签: iosobjective-caudioavplayer

解决方案


您可以使用这样的东西来播放相机快照音频效果。非常适合我。

 #import <AudioToolbox/AudioToolbox.h>
 ......................................
 AudioServicesPlaySystemSound(1108);

推荐阅读