我想播放两次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.*/谁能告诉我这个错误的原因是什么?我该怎么处理呢?
有没有其他方法可以让我这样做呢?
发布于 2012-03-19 16:36:00
因此,经过大量的搜索和尝试修复它,我找到了我问题的解决方案……
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。
发布于 2012-03-15 16:48:30
当你调用第二个play方法时,声音已经在播放了,显然是垃圾收集中的一个bug,因为第二个play应该被忽略,但这不是你想要实现的问题。
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];https://stackoverflow.com/questions/9716198
复制相似问题