การซ้อนทับด้วยเสียง ช่วยให้คุณเปลี่ยน เสียง ของผู้ใช้เป็น ข้อความ โดยมอบ UX ที่สวยงาม พร้อมทั้งจัดการ สิทธิ์ที่จำเป็น ให้กับคุณ
โดยจะใช้ SFSpeechRecognizer
ดั้งเดิมภายในเพื่อทำการแปลงคำพูดเป็นข้อความ
คุณสามารถโคลนและรันโปรเจ็กต์สาธิตได้โดยทำการ pod install
จากนั้นรันโปรเจ็กต์
Swift Package Manager เป็นเครื่องมือสำหรับจัดการการแจกจ่ายโค้ด Swift มันถูกรวมเข้ากับระบบ Swift build เพื่อทำให้กระบวนการดาวน์โหลด คอมไพล์ และลิงก์การอ้างอิงเป็นไปโดยอัตโนมัติ
หากต้องการใช้ SwiftPM คุณควรใช้ Xcode 11+ เพื่อเปิดโครงการของคุณ คลิก File
-> Swift Packages
-> Add Package Dependency
ป้อน URL ของ repo ของ InstantSearch VoiceOverlay
หากคุณเป็นผู้เขียนเฟรมเวิร์กและใช้ 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 ) ) " )
} )
}
คุณสามารถปรับแต่งการซ้อนทับเสียงของคุณได้โดยแก้ไขคุณสมบัติ 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...
คุณสามารถเปลี่ยนภาษาหรือ SpeechController ได้เมื่อเริ่มต้น voiceOverlayController ของคุณดังนี้:
lazy var voiceOverlayController : VoiceOverlayController = {
let recordableHandler = {
return SpeechController ( locale : Locale ( identifier : " en_US " ) )
}
return VoiceOverlayController ( speechControllerHandler : recordableHandler )
} ( )
คุณสามารถสร้างคลาส SpeechController ที่คุณกำหนดเองได้โดยใช้โปรโตคอล Recordable
โปรดทราบว่าใน 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
เป็นจริง
/// 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 ดูไฟล์ใบอนุญาตสำหรับข้อมูลเพิ่มเติม