語音疊加可協助您將使用者的語音轉換為文字,提供精美的使用者體驗,同時為您處理必要的權限。
它在內部使用本機SFSpeechRecognizer
來執行語音到文字的轉換。
您可以透過執行pod install
然後運行該項目來克隆並運行 Demo 項目
Swift Package Manager 是一個用於管理 Swift 程式碼分發的工具。它與 Swift 建置系統集成,可自動執行下載、編譯和連結依賴項的過程。
要使用 SwiftPM,您應該使用 Xcode 11+ 開啟您的專案。點選File
-> Swift Packages
-> Add Package Dependency
,輸入 InstantSearch VoiceOverlay 儲存庫的 URL。
如果您是框架作者並使用 VoiceOverlay 作為依賴項,請更新您的Package.swift
檔案:
let package = Package (
// 1.1.0 ..< 2.0.0
dependencies : [
. package ( url : " https://github.com/algolia/voice-overlay-ios " , from : " 1.1.0 " )
] ,
// ...
)
InstantSearchVoiceOverlay
可透過 CocoaPods 取得。要安裝它,請將以下行新增到您的 Podfile 中:
pod 'InstantSearchVoiceOverlay' , '~> 1.1.0'
Carthage 是 Cocoa 的一個簡單的、去中心化的依賴管理器。
若要安裝 InstantSearchVoiceOverlay,請將以下行新增至您的 Cartfile:
github "algolia/voice-overlay-ios" ~> 1.1 . 0
Info.plist
中,加入這 2 個字串屬性以及描述Privacy - Microphone Usage Description
,其描述如下: Need the mic for audio to text
Privacy - Speech Recognition Usage Description
一些描述,例如: Need the speech recognition capabilities for searching tags
import InstantSearchVoiceOverlay
class ViewController : UIViewController {
let voiceOverlayController = VoiceOverlayController ( )
@ objc func voiceButtonTapped ( ) {
voiceOverlayController . start ( on : self , textHandler : { ( text , final ) in
print ( " voice output: ( String ( describing : text ) ) " )
print ( " voice output: is it final? ( String ( describing : final ) ) " )
} , errorHandler : { ( error ) in
print ( " voice output: error ( String ( describing : error ) ) " )
} )
}
您可以透過修改 voiceOverlayController 的settings
屬性來自訂語音覆蓋:
/// Specifies whether the overlay directly starts recording (true),
/// or if it requires the user to click the mic (false). Defaults to true.
voiceOverlayController . settings . autoStart = true
/// Specifies whether the overlay stops recording after the user stops talking for `autoStopTimeout`
/// seconds (true), or if it requires the user to click the mic (false). Defaults to true.
voiceOverlayController . settings . autoStop = true
/// When autoStop is set to true, autoStopTimeout determines the amount of
/// silence time of the user that causes the recording to stop. Defaults to 2.
voiceOverlayController . settings . autoStopTimeout = 2
/// The layout and style of all screens of the voice overlay.
voiceOverlayController . settings . layout . < someScreen > . < someConstant >
// Use XCode autocomplete to see all possible screens and constants that are customisable.
// Examples:
/// The voice suggestions that appear in bullet points
voiceOverlayController . settings . layout . inputScreen . subtitleBulletList = [ " Suggestion1 " , " Sug2 " ]
/// Change the title of the input screen when the recording is ongoing.
voiceOverlayController . settings . layout . inputScreen . titleListening = " my custom title "
/// Change the background color of the permission screen.
voiceOverlayController . settings . layout . permissionScreen . backgroundColor = UIColor . red
/// And many more...
您可以在初始化 voiceOverlayController 時變更區域設定或 SpeechController,如下所示:
lazy var voiceOverlayController : VoiceOverlayController = {
let recordableHandler = {
return SpeechController ( locale : Locale ( identifier : " en_US " ) )
}
return VoiceOverlayController ( speechControllerHandler : recordableHandler )
} ( )
您可以透過實作Recordable
協定來建立自己的自訂 SpeechController 類別。
請注意,在 Swift 4 中,您可以使用Locale.current.languageCode
來取得當前語言環境。
或者,要監聽文字和錯誤事件,您可以遵循VoiceOverlayDelegate
協定的方法。
// Second way to listen to recording through delegate
func recording ( text : String ? , final : Bool ? , error : Error ? ) {
if let error = error {
print ( " delegate: error ( error ) " )
}
if error == nil {
print ( " delegate: text ( text ) " )
}
}
當缺少權限時,語音疊加將引導使用者前往設定應用程式的正確部分。
當showResultScreen
設定為 true 時,將顯示結果畫面。
/// Whether or not to show a result screen after the recording is finished.
voiceOverlayController . settings . showResultScreen = true
/// Timeout for showing the result screen in case no resultScreenText is provided on time.
voiceOverlayController . settings . showResultScreenTimeout = 2
/// Time for showing the result screen with the provided resultScreenText.
voiceOverlayController . settings . showResultScreenTime = 4
/// The processed result screen text that should be appear in the result screen.
voiceOverlayController . settings . resultScreenText = NSAttributedString ( string : myString , attributes : myAttributes )
該小工具在關閉結果畫面時提供resultScreenHandler
(假設未按一下「重新開始」按鈕)。此處理程序提供預先在resultScreenText
中設定的文字。
voiceOverlayController . start ( on : self , textHandler : { ( text , final ) in
print ( " getting ( String ( describing : text ) ) " )
print ( " is it final? ( String ( describing : final ) ) " )
if final {
// Process the result to post in the result screen.
// The timer here simulates a network processing call that took 1.5 seconds.
Timer . scheduledTimer ( withTimeInterval : 1.5 , repeats : false , block : { ( _ ) in
let myString = text
let myAttribute = [ NSAttributedString . Key . foregroundColor : UIColor . red ]
let myAttrString = NSAttributedString ( string : myString , attributes : myAttribute )
self . voiceOverlayController . settings . resultScreenText = myAttrString
} )
}
} , errorHandler : { ( error ) in
print ( " error ( String ( describing : error ) ) " )
} , resultScreenHandler : { ( text ) in
print ( " Result Screen: ( text ) " )
} )
InstantSearchVoiceOverlay 在 MIT 許可證下可用。有關詳細信息,請參閱許可證文件。