我正在开发一个应用程序来通知一些事件。它有可听通知和NSView/HUD通知。
在调试过程中,我已经运行了几个小时。我注意到,在8+几个小时后,NSSound停止发出声音。
我的代码很简单,在最初的几个小时内可以工作:
它的内容如下:
void onStateUpdate(void* context) {
NSSound* myAwesomeSound = [NSSound soundNamed:existingSoundFile];
if (myAwesomeSound == nil)
NSLog(@"\n\nERROR FINDING SOUND!!!?!?!\n\n");
else
NSLog(@"Code is being called!");
[myAwesomeSound play];
[myVisualHUD start];
}通过注册CFRunLoopSource调用:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
mRunLoopSource = (CFRunLoopSourceRef)IOPSNotificationCreateRunLoopSource(onStateUpdate, (__bridge void *)(self));
if(mRunLoopSource) {
CFRunLoopAddSource(CFRunLoopGetCurrent(), mRunLoopSource, kCFRunLoopDefaultMode);
}
}此代码与HUD代码和报告事件的NSLog一起出现。所以我知道它实际上是按预期运行的,但是操作系统只是“静音”或者忽略NSSound,因为它没有失败。
我做错了什么?我如何使它继续工作,因为这将最终成为一个托盘应用程序通知。
发布于 2017-02-21 06:42:59
因此,在阅读了一些线程之后,比如这。
我恐怕不能说NSSound是另一个来自苹果的混合效果API:
使用AudioToolbox是可行的方法。到目前为止,工作了几个小时后还在继续工作。
下面是一个代码片段(有点难看,但可以自我解释用法):
NSString *audioFileName = @"MyResourceFileName";
NSURL *audioURLRef = [[NSBundle mainBundle] URLForResource:audioFileName
withExtension:@"mp3"];
SystemSoundID audioFileId = 0;
AudioServicesCreateSystemSoundID(
(__bridge CFURLRef) audioURLRef,
&audioFileId
);
// Plays the audio file using AudioServices...
AudioServicesPlaySystemSound(audioFileId);https://stackoverflow.com/questions/42038928
复制相似问题