播放NSSound两次 [英] Play NSSound two times

查看:88
本文介绍了播放NSSound两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了要短于10秒,我想播放NSSound两次,然后我要等到10秒结束后再开始第二个。

I want to play a NSSound two times except its shorter than 10sec then i want to wait until the 10sec are done and then start the second one.

但是我

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.*/

有人可以告诉我此错误的原因是什么?
我该如何处理?

Can someone tell me whats the reason for this error? How can I handle this?

还有另一种方法吗?

推荐答案

因此,在搜寻并尝试修复它之后,我为我的问题找到了解决方案...

So after a loot of searching and trying to fix it I got the solution for my problem...

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);
}

我发现,即使声音结束,我也必须打电话[声音停止],然后再开始第二次。

I found out that even when the sound has finished I have to call [sound stop] before starting the second.

这篇关于播放NSSound两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆