音声オーバーレイは、ユーザーの音声をテキストに変換するのに役立ち、必要な権限を処理しながら洗練された UX を提供します。
音声からテキストへの変換を実行するために、内部でネイティブSFSpeechRecognizer
使用します。
pod install
を実行してからプロジェクトを実行することで、デモ プロジェクトのクローンを作成して実行できます。
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 をインストールするには、次の行をカートファイルに追加します。
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 ライセンスに基づいて利用できます。詳細については、LICENSE ファイルを参照してください。