mac 上的 UNUserNotificationCenter 无法播放自定义音频文件

问题描述

我正在 BigSur 上构建菜单栏应用。应用程序经常发送 UserNotifications。我想对显示.default 声音应用不同的声音用户通知。我在播放声音时遇到了一些麻烦。我已经尽我所能,但仍然无法让它发挥作用。

到目前为止我所做的(音频文件one.caf

  1. 检查音频文件是否在 copy Bundle resources 中,并且在那里

enter image description here

  1. 检查多种音频格式 (.mp3,.wav,.caf)
  2. 将位置从 project root 更改为 controllers 目录

enter image description here

  1. 清理构建文件夹,清除派生数据,然后重新启动
  2. 使用 Bundle.main.url() 获取路径,然后调用 .lastPathComponent

enter image description here

  1. 我已检查是否为文件选择了目标

enter image description here

  1. 我已经检查了该文件是否也在 finder 中的同一目录中。

enter image description here

我不确定这是否与我调用通知的方式有关,但我对此表示怀疑。无论哪种方式,这是我在需要显示通知时运行的代码。仅供参考,我正在使用 singleton 类来管理通知

    private func showNotification(title: String,subtitle: String) {
        let content = UNMutableNotificationContent()
        content.title = title
        content.subtitle = subtitle
   
        // Getting asset name using URL
        let stringPath = Bundle.main.url(forResource: "one",withExtension: "caf")
        print(stringPath?.lastPathComponent)


//        content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "one.caf"))
        UNNotificationSound(named: UNNotificationSoundName(stringPath?.lastPathComponent ?? ""))
        content.sound = .defaultCritical
        let request = UNNotificationRequest(identifier: UUID().uuidString,content: content,trigger: nil)
        UNUserNotificationCenter.current().add(request)
    }

包装内的内容

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)