微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从请求中检索UNNotificationRequest声音名称

如何解决从请求中检索UNNotificationRequest声音名称

我设置了具有自定义声音的通知,如下所示:

var soundNames = ["Default","Alert1","Alert2","Alert3","Alert4","Alert5","Alert6","Alert7","Alert8","Alert9","Alert10"]

func addNotification(title: String,timeInt: TimeInterval,image: Data?,soundName: String?) {

let content = UNMutableNotificationContent()
content.title = title

    content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "\(soundName!).caf"))

    var trigger: UNTimeIntervalNotificationTrigger

    trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInt,repeats: true)

// choose a random identifier
let request = UNNotificationRequest(identifier: UUID().uuidString,content: content,trigger: trigger)

// add our notification request
UNUserNotificationCenter.current().add(request)

saveConceptCard(id: request.identifier,title: title,image: image)

}

现在,我想获取从ID检索特定通知请求时使用的自定义声音的名称

 var notification: UNNotificationRequest

我尝试这样获得声音名称

 let sound = notification.content.sound!.debugDescription

但这返回:

 <UNNotificationSound: 0x2822419d0>

有没有办法从该代码获取文件名?

解决方法

您必须使用AVFoundation框架将文本转换为语音

当您推送通知时,您还必须推送AVSpeechSynthesizer

设置方法如下:

let utterance = AVSpeechUtterance(string: "Hello world")
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
utterance.rate = 0.1 

let synthesizer = AVSpeechSynthesizer()

以下是语音操作:

synthesizer.speak(utterance)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。