Hamparan suara membantu Anda mengubah suara pengguna menjadi teks , memberikan UX yang disempurnakan sambil menangani izin yang diperlukan untuk Anda.
Ia menggunakan SFSpeechRecognizer
asli secara internal untuk melakukan konversi ucapan ke teks.
Anda dapat mengkloning dan menjalankan proyek Demo dengan melakukan pod install
dan kemudian menjalankan proyek tersebut
Swift Package Manager adalah alat untuk mengelola distribusi kode Swift. Ini terintegrasi dengan sistem build Swift untuk mengotomatiskan proses pengunduhan, kompilasi, dan menghubungkan dependensi.
Untuk menggunakan SwiftPM, Anda harus menggunakan Xcode 11+ untuk membuka proyek Anda. Klik File
-> Swift Packages
-> Add Package Dependency
, masukkan URL repo InstantSearch VoiceOverlay.
Jika Anda adalah pembuat kerangka kerja dan menggunakan VoiceOverlay sebagai dependensi, perbarui file Package.swift
Anda:
let package = Package (
// 1.1.0 ..< 2.0.0
dependencies : [
. package ( url : " https://github.com/algolia/voice-overlay-ios " , from : " 1.1.0 " )
] ,
// ...
)
InstantSearchVoiceOverlay
tersedia melalui CocoaPods. Untuk menginstalnya, tambahkan baris berikut ke Podfile Anda:
pod 'InstantSearchVoiceOverlay' , '~> 1.1.0'
Carthage adalah manajer ketergantungan yang sederhana dan terdesentralisasi untuk Kakao.
Untuk menginstal InstantSearchVoiceOverlay, tambahkan baris berikut ke Cartfile Anda:
github "algolia/voice-overlay-ios" ~> 1.1 . 0
Info.plist
, tambahkan 2 properti string ini beserta deskripsinyaPrivacy - Microphone Usage Description
dengan deskripsi seperti: Need the mic for audio to text
Privacy - Speech Recognition Usage Description
beberapa deskripsi seperti: 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 ) ) " )
} )
}
Anda dapat menyesuaikan hamparan suara Anda dengan mengubah properti settings
voiceOverlayController:
/// 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...
Anda dapat mengubah lokal atau SpeechController saat menginisialisasi voiceOverlayController seperti:
lazy var voiceOverlayController : VoiceOverlayController = {
let recordableHandler = {
return SpeechController ( locale : Locale ( identifier : " en_US " ) )
}
return VoiceOverlayController ( speechControllerHandler : recordableHandler )
} ( )
Anda dapat membuat kelas SpeechController kustom Anda sendiri dengan mengimplementasikan protokol Recordable
.
Perhatikan bahwa di Swift 4, Anda dapat menggunakan Locale.current.languageCode
untuk mendapatkan lokal saat ini.
Secara opsional, untuk mendengarkan peristiwa teks dan kesalahan, Anda dapat menyesuaikan diri dengan metode protokol 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 ) " )
}
}
Jika ada izin yang hilang, hamparan suara akan memandu pengguna ke bagian aplikasi pengaturan yang benar.
Layar hasil muncul ketika showResultScreen
disetel ke 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 )
Widget menyediakan resultScreenHandler
ketika layar hasil ditutup (asalkan tombol "Mulai lagi" tidak diklik). Handler menyediakan teks yang telah diatur di resultScreenText
sebelumnya.
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 tersedia di bawah lisensi MIT. Lihat file LISENSI untuk info lebih lanjut.