NSUserNotification与自定义soundName

时间:2012-09-04 16:05:33

标签: objective-c macos osx-mountain-lion nsusernotification

是否有人设法使用NSUserNotification soundName来处理自定义声音? 我尝试使用aif和caf格式44100KHz 16bit 2秒的持续时间。通知会在适当的时间显示,并带有正确的标题和文字,但会播放默认声音而不是我的自定义声音。

声音文件已在应用程序包中正确复制。 如果我试试这个声音可以正常工作:

NSSound* sound = [NSSound soundNamed:@"morse.aif"];
[sound play];

但是当我在通知中使用相同的声音时,会播放默认通知声音:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    NSUserNotification* notification = [[NSUserNotification alloc]init];
    notification.title = @"Titolo";
    notification.deliveryDate = [NSDate dateWithTimeIntervalSinceNow:10];
    notification.soundName = @"morse.aif";
    [[NSUserNotificationCenter defaultUserNotificationCenter]scheduleNotification:notification];
}

我尝试了无延伸,但没有成功。

notification.soundName = @"morse.aif";
notification.soundName = @"morse2.caf";
notification.soundName = @"morse";     

这些都不起作用。

我的应用程序没有签名,也没有沙盒,但我不认为这对用户通知是必要的,除了声音问题之外,通知效果也很好。

2 个答案:

答案 0 :(得分:8)

在我看来,这个问题是区分大小写的。使用notification.soundName = @"Morse";对我来说非常有效。正如您在soundNamed ~/Library/Sounds /Library/Sounds /Network/Library/Sounds /System/Library/Sounds 中看到的那样,声音的搜索文件夹依次为:

{{1}}

如果您查看最后一个,这可能是您尝试拉出的地方,因为它们是“系统偏好设置”中的声音,您可以看到它们的名称

Sound names

因此请保留文件的大小写,并省略扩展名,它应该按预期工作。

答案 1 :(得分:1)

如果您有多个版本的App二进制通知中心可能正在搜索错误的二进制文件中的声音文件。

如果您确保删除二进制文件的任何旧副本,则应该解决问题。

来自Apple Dev论坛:https://devforums.apple.com/message/708511

  

如果您的应用程序二进制文件的多个版本浮动,就会发生这种情况。 NotificationCenter仅对其中一个进行罚款。如果是没有声音的那个,那么它将无法工作。