首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >播放NSSound两次

播放NSSound两次
EN

Stack Overflow用户
提问于 2012-03-15 16:32:50
回答 2查看 902关注 0票数 0

我想播放两次NSSound,除了它不到10秒,然后我想等到10秒完成,然后开始第二次。

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

代码语言:javascript
复制
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.*/

谁能告诉我这个错误的原因是什么?我该怎么处理呢?

有没有其他方法可以让我这样做呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-19 16:36:00

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

代码语言:javascript
复制
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);
}

我发现,即使在声音结束时,我也必须在启动第二个声音之前调用sound stop。

票数 1
EN

Stack Overflow用户

发布于 2012-03-15 16:48:30

当你调用第二个play方法时,声音已经在播放了,显然是垃圾收集中的一个bug,因为第二个play应该被忽略,但这不是你想要实现的问题。

代码语言:javascript
复制
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"MessageArrived" ofType:@"wav"];
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:resourcePath byReference:YES];
[sound play];

//Will play the second time after 10.0 seconds have passed.
NSInteger wait = ([sound duration] < 10.0) ? 10.0 - [sound duration] : 0.0; 
[sound performSelector:@selector(play) withObject:nil afterDelay:wait];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9716198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档