Улучшение ответа @sealz, вот мой ответ:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:layout_alignTop="@+id/mail"
android:layout_alignBottom="@+id/mail"
>
<Button
android:id="@+id/leftMail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:background="@android:color/transparent"
/>
<Button
android:id="@+id/rightMail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:background="@android:color/transparent"
/>
</LinearLayout>
<ImageView
android:id="@+id/mail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/mail1600x600"
android:layout_centerInParent="true"
/>
</RelativeLayout>
Вы должны реализовать делегат из AVSpeechSynthesizer
и добавить следующий код:
var myText = "This is my text, which will be detect on text-to-speech operation"
override func viewDidLoad() {
super.viewDidLoad()
speechSynthesizer.delegate = self
}
Реализовано willSpeakRangeOfSpeechString
, которое будет называть каждое произнесенное слово системой в текстовой строке. Вы можете реализовать метод contains()
типа String
для обнаружения конкретной строки в вашем тексте.
extension TextToSpeechVC: AVSpeechSynthesizerDelegate {
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
let string = self.myText[characterRange.lowerBound..<characterRange.upperBound]
if string.trim == "operation" {
print("string = \(string)")
}
}
}
Добавить расширение String
для индекса диапазона.
extension String {
var trim: String {
return self.trimmingCharacters(in: .whitespacesAndNewlines)
}
subscript (bounds: CountableClosedRange<Int>) -> String {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return String(self[start...end])
}
subscript (bounds: CountableRange<Int>) -> String {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return String(self[start..<end])
}
}
Надеюсь, это поможет вам.