如何播放 macOS“垃圾箱”系统声音?

问题描述

我在里面发现了几个 macOS 系统声音

/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds

包括dock/drag to trash.aif 中的查找器中删除文件时的垃圾声音。

但是我找不到播放这些声音的 API。我真的不想硬编码任何文件路径。

PS:一个选项可能是简单地将声音复制到我自己的应用程序包中,但如果可能的话,我也想避免这种情况(还有许可?)。

解决方法

导入AudioToolbox框架并使用AudioServicesPlaySystemSound函数。参数值未记录在案,并且可能会与硬编码路径一样发生变化。它应该通过应用商店审查,因为它不是私有 API。

//drag to trash.aif
AudioServicesPlaySystemSound(0x10);

//poof item of dock.aif
AudioServicesPlaySystemSound(0xf);

唯一记录的值是:

CF_ENUM(SystemSoundID)
{
    kSystemSoundID_UserPreferredAlert   = 0x00001000,kSystemSoundID_FlashScreen          = 0x00000FFE,// this has been renamed to be consistent
    kUserPreferredAlert     = kSystemSoundID_UserPreferredAlert
};

这是未记录的值列表

 1 "Volume Mount.aif"
 2 "Volume Unmount.aif"
 3 "Media Keys.aif"
 4 "Sticky Keys ON.aif"
 5 "Sticky Keys OFF.aif"
 6 "Sticky Keys MODIFER.aif"
 7 "Sticky Keys Locked.aif"
 8 "Sticky Keys Stuck.aif"
 9 "Mouse Keys ON.aif"
10 "Mouse Keys OFF.aif"
11 "Slow Keys,1st Key Pressed.aif"
12 "Slow Keys,Key Registered.aif"
13 "empty trash.aif"
14 "move to trash.aif"
15 "poof item off dock.aif"
16 "drag to trash.aif"
17 "InkSoundBecomeMouse.aif"
18 "InkSoundStroke1.aif"
19 "InkSoundStroke2.aif"
20 "InkSoundStroke3.aif"
21 "InkSoundStroke4.aif"
22 "burn complete.aif"
23 "burn failed.aif"
24 "Grab.aif"