gpt4 book ai didi

objective-c - 播放 NSSound 两次

转载 作者:行者123 更新时间:2023-12-03 17:58:47 25 4
gpt4 key购买 nike

我想播放 NSSound 两次,除非它的时间短于 10 秒,然后我想等到 10 秒完成后再开始第二次。

但是我在播放同一个 NSSound 两次时遇到了问题。

NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"MessageArrived" ofType:@"wav"];
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:resourcePath byReference:YES];
[sound play] //It plays the fist time
[sound play] //I get the following Error and it doesn't play a secont time
/*malloc: *** auto malloc[498]: error: GC operation on unregistered thread. Thread registered implicitly. Break on auto_zone_thread_registration_error() to debug.*/

有人可以告诉我这个错误的原因是什么吗?我该如何处理这个问题?

还有其他方法可以做到这一点吗?

最佳答案

因此,经过大量搜索并尝试修复它,我得到了问题的解决方案......

NSSound *sound = [NSSound soundNamed:@"MessageArrived"];
BOOL res = [sound play];
NSLog(@"%d", res);
[NSThread detachNewThreadSelector:@selector(playSecondSound:) toTarget:self withObject:sound];

-(void)playSecondSound:(NSSound*)sound
{
[NSThread sleepForTimeInterval:10-[sound duration]];
[sound stop];
BOOL res = [sound play];
NSLog(@"%d", res);
}

我发现即使声音已经结束,我也必须在开始第二个声音之前调用[声音停止]。

关于objective-c - 播放 NSSound 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9716198/

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