在IOS中重复点击按钮播放声音时的噪音(物镜-C)

时间:2016-07-13 01:15:52

标签: ios objective-c audio noise

我有一个按钮(或子视图)的视图,您可以点按以播放声音。听起来不错但是如果你反复快速点击按钮,那么在正在播放的声音开始时会发出砰砰声/噼啪声。这发生在设备和模拟器上。

以下是点击按钮时执行的示例代码:

if([self.playerTest isPlaying]) [self.playerTest stop];//I tried with this line and without it. No difference.
NSError *error;
self.playerTest = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
[self.playerTest prepareToPlay];
[self.playerTest play];

我尝试过这样做同样的结果:

//This is executed on the init
self.player = [[AVAudioPlayerNode alloc] init];
[self.audioPlayerEngine attachNode:self.player];

//This is called when the button is tapped
if([self.player isPlaying]) [self.player stop];//I tried with this line and without it. No difference.
NSError *error;
AVAudioFile *file = [[AVAudioFile alloc] initForReading:soundFile error:&error];
AVAudioMixerNode *mainMixer = [self.audioPlayerEngine mainMixerNode];
[self.audioPlayerEngine connect:self.player to:mainMixer format:file.processingFormat];
[self.player scheduleFile:file atTime:nil completionHandler:nil];
[self.audioPlayerEngine startAndReturnError:nil];
[self.player play];

我尝试在播放下一个

之前停止声音

我试过不同的声音文件和不同的格式(wav,caf,mp3)。我也尝试过使用OpenAL。 OpenAL在IOS 8中运行良好,但在IOS 9中你也能得到噪音。

有什么想法吗?

0 个答案:

没有答案