AVAudioPlayer有时会打断应用程序 [英] AVAudioPlayer hiccups the app sometimes

查看:100
本文介绍了AVAudioPlayer有时会打断应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的游戏,每次触摸屏幕后,动画就会在一个小的UIImageView上发生,并且这种情况发生得很好,并且应用程序以CADisplayLink作为计时器运行得很顺利.

I am working on a simple game and after every screen touch, an animation is happening on a small UIImageView and this happen very well and the app is running smoothly with CADisplayLink as the timer.

我添加了一个mp3文件,每次触摸后持续1秒​​钟就可以播放,因为AVAudioPlayer可以想象这样的声音:Bip

I added an mp3 file to play after every touch with 1 second lengh as AVAudioPlayer imagine a sound like : Bip

因此,我第一次碰到屏幕第一次打h时,应用程序冻结了不到一秒钟,我可以说这没问题,因为这是声音第一次分配内存.

So the first time that I touch the screen first hiccup happens an the app freeze for less than a second that I can say it is ok coz it's the first time that the sound allocate memory.

如果您在3秒钟之前再次触摸屏幕,则稍后再发生问题,该应用不会打seconds,但是如果我等待4秒或更长时间,则每次触摸后该应用都会开始打h.

The problem happens later when u touch the screen again if I touch it earlier than 3 seconds, the app doesn't hiccups but if I wait 4 seconds and more, the app starts to hiccup after every touch.

每次我两次触摸之间的间隔时间都早于3秒时,该应用程序不会打h,但是两次触摸之间的4秒后,该应用程序就会打ic.

Every time if I touch again and again earlier than 3 seconds between touches, the app doesn't hiccups, but after 4 seconds between touches, the app hiccups.

有解决打h的主意吗?

这是一些需要的代码

@property (nonatomic, strong) AVAudioPlayer *mySound;

- (void)viewDidLoad {

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"bip" ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];

self.mySound = newPlayer;

[mySound prepareToPlay];
[mySound setDelegate:self];
}

触摸发生后

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (location.x < viewWidth/2) {
   [mySound play];
} else {
   [mySound play];
}
}

推荐答案

发出非常短的静音声音.使用您的计时器大约每秒(与另一个声音播放器)一起播放一次. —你看这里的主意;我们正在尝试使媒体服务器保持活动状态,以便在需要声音效果时可以使用它.既然您说3秒钟之前没有问题,那让我认为3秒钟之后媒体服务器便重新进入睡眠状态.我们的目标是保持发痒",以免发生.

Make a very short silent sound. Use your timer to play that (with another sound player) every second or so. — You see what the idea is here; we are try to keep the media server alive so that it is ready to go when we want our sound effect. Since you say there's no problem before 3 seconds, that makes me think that after 3 seconds the media server has gone back to sleep. Our goal is to keep "tickling" it so that can't happen.

但是,总的来说,我的印象是AVAudioPlayer不适用于此类事情.您可能应该使用AVAudioEngine,它可以保持运行并播放声音而没有延迟.

In general, however, my impression is that AVAudioPlayer is not intended for this sort of thing. You should probably be using AVAudioEngine, which can be kept running and made to play sounds without latency.

这篇关于AVAudioPlayer有时会打断应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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