gpt4 book ai didi

iphone - Xcode 中的重复声音问题

转载 作者:太空宇宙 更新时间:2023-11-04 04:53:24 24 4
gpt4 key购买 nike

当一个帧碰到另一个帧时它会播放声音所以我在 did 加载器中使用这段代码所以声音被加载

我使用框架

#import <AVFoundation/AVFoundation.h>

- (void)viewDidLoad

 NSString *path1 = [[NSBundle mainBundle] pathForResource:@"ballbounce" ofType:@"mp3"];
theaudio1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path1] error: NULL];

然后用这个来播放声音

[audio1 play];

但有时不播放声音,声音片段只有 0.7 秒长,只是发出简单的噪音。

喜欢

enemy.center = CGPointMake(enemy.center.x+pos.x,enemy.center.y+pos.y);

if (enemy.center.x > 308 || enemy.center.x < 10)

{
[theaudio1 play];
pos.x = -pos.x;

}
if (enemy.center.y > 400 || enemy.center.y < 100)
{
[theaudio1 play];
pos.y = -pos.y ;
}

声音有时连续快速播放,有时声音不播放有没有更好的方法来编写这段代码?

最佳答案

在调用 play 之前,请在 AVAudioPlayer 实例上调用 prepareToPlay 方法。它预加载音频播放器的缓冲区。

另外,如果可以的话,让你的播放器根据内存播放,因为它只是一个非常短的声音。请参阅 AVAudioPlayer 类文档中的 initWithData:error 方法说明。

关于iphone - Xcode 中的重复声音问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12412687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com