Voice Assistant SDK IOS
Overview
The Voice Assistant SDK provides an easy-to-use features for integrating Voice Assistant capabilities into your IOS applications. With this SDK, you can initialize voice data, play voice messages, and handle playback callbacks effortlessly.
Build up
- Initialization: Load TTS voice configurations from a remote source.
- Voice URL: A URL that contains the voices from remote data source
- Play Voice: Play voice messages with customizable delegate for play, stop, and error events.
- Stop Voice: Stop the playback whenever required.
AmaniVoiceAssistant integration with SPM
You can easily add AmaniVoiceAssistantSDK to your project as Swift Package Manager.
In Xcode project, File > Add Package Dependencies > paste it to search bar under the link.
source "https://github.com/AMANI-AI-ORG/AmaniVoiceAssistantSDK"
AmaniVoiceAsistant SDK Initialization
- Getting from config
import AmaniSDK
#if canImport(AmaniVoiceAssistantSDK)
import AmaniVoiceAssistantSDK
#endif
class ViewController: UIViewController {
var voiceAsistant: AmaniVoiceAssistant?
var config: AppConfig?
override func viewWillAppear() {
super.viewWillAppear()
Task { @MainActor in
do {
try await initAmaniVoiceAssistant()
} catch(let err) {
debugPrint(err)
}
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
#if canImport(AmaniVoiceAssistantSDK)
Task { @MainActor in
do {
//MARK: You must need call stop func where did you set to a property AmaniVoiceAssistant Class.
try? await AmaniUI.sharedInstance.voiceAssistant?.stop()
}catch(let err) {
debugPrint(err)
}
}
#endif
}
func initAmaniVoiceAssistant() async {
self.config = try Amani.sharedInstance.appConfig().getApplicationConfig()
if let ttsURL = self.config?.generalconfigs.ttsVoice {
do {
self.voiceAssistant = try await AmaniVoiceAssistant.init(url: ttsURL)
self.voiceAsistant?.setDelegate(delegate: self)
}catch(let err) {
debugPrint(err)
}
}
}
func playAmaniVoiceAssistant() async {
#if canImport(AmaniVoiceAssistantSDK)
if let docID = self.docID {
Task { @MainActor in
do {
//It should be set according to the key value defined in the JSON file.
try? await AmaniUI.sharedInstance.voiceAssistant?.play(key: "VOICE_\(docID)")
}catch(let error) {
debugPrint("\(error)")
}
}
}
#endif
}
}
//MARK: Set AmaniVoiceAssistant SDK delegate
extension ViewController: AmaniVADelegate {
func onPlay(with text: String) {
//The voice has been started
debugPrint(text)
}
func onStop(with text: String) {
//The voice has been stopped
debugPrint(text)
}
}
Voice URL
Voice URL is a string URL given in the SDK init phase to fetch the sounds from the remote data source. You can set this string URL in two ways.
- Getting Voice URL From Amani Remote Config: In order to use this feature, AmaniSDK must be integrated in your application. If it is not integrated, you can reach the link to see how to integrate it.
import AmaniSDK
/**
* This function is a function of AmaniSDK. It is used to retrieve remote data to get VoiceURL.
*/
private func getVoiceURL() {
do {
self.config: AppConfigModel? = try sharedSDKInstance.appConfig().getApplicationConfig()
if let ttsURL = self.config?.generalconfigs.ttsVoice {
debugPrint(ttsURL)
}
}
}
- Creating Manual URL: In order to use this feature, you must create a URL link in accordance with the JSON format below and the URL must be a URL that directly downloads the JSON.
The audio files in the JSON must be in the "voice":"data:audio/mp3;base64 format.
- Voice URL JSON Format:
[
{
"key":"VOICE_ID0",
"text":"Lütfen kimlik belgenizin ön yüzünü tarayın",
"voice":"data:audio/mp3;base64,long_base64_string"
},
{
"key":"VOICE_ID1",
"text":"Lütfen kimlik belgenizin arka yüzünü tarayın",
"voice":"data:audio/mp3;base64,long_base64_string"
},
{
"key":"VOICE_NFC",
"text":"Lütfen kimlik belgenizi yaklaştırın, böylece cihazınızın arka kısmına değsin",
"voice":"data:audio/mp3;base64,long_base64_string"
},
{
"key":"VOICE_SUCCESS",
"text":"Teşekkür ederiz! Kimlik doğrulama tamamlandı",
"voice":"data:audio/mp3;base64,long_base64_string"
},
{
"key":"VOICE_SE0",
"text":"Kaliteli bir selfie çekmek için yüzünüzü kameraya doğru hizalayın, iyi bir ışık kaynağı kullanın, doğal bir ifade takının, cihazınızı sabit tutun ve lensinizin temiz olduğundan emin olun.",
"voice":"data:audio/mp3;base64,long_base64_string"
}
]
NOTE
If you want to get the voice file from Amani Config and customize, you can use the keyword "ttsVoices_ios". If you encounter any problems, please contact Amani.
- Set Messagges
class ViewController: UIViewController {
var voiceAsistant: AmaniVoiceAssistant?
override func viewWillAppear() {
super.viewWillAppear()
Task { @MainActor in
do {
try await initAmaniVoiceAssistant()
} catch(let err) {
debugPrint(err)
}
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
#if canImport(AmaniVoiceAssistantSDK)
Task { @MainActor in
do {
try? await AmaniUI.sharedInstance.voiceAssistant?.stop()
debugPrint("\(err)")
}
}
#endif
}
func initAmaniVoiceAssistant() async {
voiceAsistant = AmaniVoiceAssistant(lang: "en-EN")
voiceAsistant?.setDelegate(delegate: self)
voiceAsistant?.setMessages(voiceMessage: ["voice_id_1": "Please scan front of your id card."])
await playAmaniVoiceAssistant()
}
func playAmaniVoiceAssistant() async {
do {
try await voiceAsistant?.play(key: "voice_id_1")
}catch(let error) {
debugPrint(error)
}
}
}
//MARK: Set AmaniVoiceAssistant SDK delegate
extension ViewController: AmaniVADelegate {
func onPlay(with text: String) {
//The voice has been started
debugPrint(text)
}
func onStop(with text: String) {
//The voice has been stopped
debugPrint(text)
}
}