首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从请求中检索UNNotificationRequest声音名称

从请求中检索UNNotificationRequest声音名称
EN

Stack Overflow用户
提问于 2020-10-28 23:33:10
回答 1查看 46关注 0票数 0

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

代码语言:javascript
复制
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检索特定通知请求时使用的自定义声音的名称。

代码语言:javascript
复制
 var notification: UNNotificationRequest

我尝试这样获取声音名称:

代码语言:javascript
复制
 let sound = notification.content.sound!.debugDescription

但这会返回:

代码语言:javascript
复制
 <UNNotificationSound: 0x2822419d0>

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

EN

回答 1

Stack Overflow用户

发布于 2020-10-29 04:07:03

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

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

下面是如何设置它的:

代码语言:javascript
复制
let utterance = AVSpeechUtterance(string: "Hello world")
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
utterance.rate = 0.1 

let synthesizer = AVSpeechSynthesizer()

下面是语音操作:

代码语言:javascript
复制
synthesizer.speak(utterance)
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64576165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档