gpt4 book ai didi

ios - AVAudioPlayer 不播放长音?

转载 作者:行者123 更新时间:2023-11-29 01:58:09 25 4
gpt4 key购买 nike

我有一系列从 0.3s 到 3.0s 的声音文件。

尝试使用 AVAudioPlayer 播放时,我什么也没得到,除非我在之后添加一个 sleep(4) 调用以确保可以播放声音。真奇怪。

除此之外,传入的错误参数没有错误,[player play] 每次都返回 YES。但是,奇怪的是,委托(delegate)方法没有被调用。

这是我的代码。

(.m 文件)

@interface MyClass ()
@property (nonatomic, strong) AVAudioPlayer *soundPlayer;
@end

@implementation SLInspireKit
//view did load here

- (void)playRandomSound {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];

NSError *error;

NSString *musicFileName = [NSString stringWithFormat:@"my-sound-%d.aiff", arc4random_uniform(8)];
NSString *path = [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], musicFileName];
NSURL *soundUrl = [NSURL fileURLWithPath:path];

self.soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&error];
self.soundPlayer.delegate = self;
[self.soundPlayer setVolume:1.0];
[self.soundPlayer prepareToPlay];

if ([self.soundPlayer play]){
NSLog(@"Played fine");
} else {
NSLog(@"Did not play fine");
}
sleep(1); //uncomment this out, and nothing. Set to 1, first second of each sound, 2, 2 seconds and so on.

}

有什么想法吗?我不认为这是 ARC 的事情,但它可能是:/我每次都必须启动,因为声音每次都在变化。

最佳答案

评论的快速总结:结果是需要 sleep(n),因为包含 AVAudioPlayer 的整个类都被释放了。

保留类(class)可以解决问题。

干杯!

关于ios - AVAudioPlayer 不播放长音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30619110/

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