繁体   English   中英

在Xcode中重复播放声音的问题

[英]Issue with repeating sound in Xcode

当一帧击中另一帧时,它会播放声音,因此我在did loader中使用了此代码,从而加载了声音

我使用框架

#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方法说明。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM