【发布时间】:2016-06-25 18:28:32
【问题描述】:
我正在开发一个简单的游戏,每次触摸屏幕后,都会在一个小的 UIImageView 上发生动画,这发生得非常好,并且应用程序在 CADisplayLink 作为计时器的情况下运行顺利。
我添加了一个 mp3 文件,以便在每次触摸后播放 1 秒长度,因为 AVAudioPlayer 想象这样的声音:Bip
所以当我第一次触摸屏幕第一次打嗝时,应用程序冻结了不到一秒钟,我可以说没关系,因为这是声音第一次分配内存。
如果我在 3 秒之前再次触摸屏幕,应用程序不会打嗝,但如果我等待 4 秒或更长时间,应用程序会在每次触摸后开始打嗝。
如果我在触摸间隔 3 秒之前一次又一次触摸,应用程序不会打嗝,但在触摸间隔 4 秒后,应用就会打嗝。
有解决打嗝的办法吗?
如果需要,这是一些代码
@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];
}
}
【问题讨论】:
-
您能显示按钮中的任何其他代码吗?
-
我没有使用任何按钮,只是触摸屏幕上的任何位置。我为你编辑了我的问题。
标签: objective-c xcode avfoundation avaudioplayer