Browse Source

1.添加获取moca试题库module

王鹏鹏 2 years ago
parent
commit
23e3413c82

+ 16 - 0
baselib/src/main/java/com/yingyangfly/baselib/voice/PlayVoice.kt

@@ -161,6 +161,22 @@ class PlayVoice {
         checkResult(result, "speak")
     }
 
+    /**
+     * 播放语音
+     */
+    fun speakWithParagraph(word: List<String>) {
+        if (mSpeechSynthesizer == null) {
+            print("[ERROR], 初始化失败")
+            return
+        }
+        val bags: MutableList<SpeechSynthesizeBag> = ArrayList()
+        for (i in word.indices) {
+            bags.add(getSpeechSynthesizeBag(word[i], i.toString()))
+        }
+        val result: Int = mSpeechSynthesizer!!.batchSpeak(bags)
+        checkResult(result, "speak")
+    }
+
     private fun getSpeechSynthesizeBag(text: String, utteranceId: String): SpeechSynthesizeBag {
         val speechSynthesizeBag = SpeechSynthesizeBag()
         //需要合成的文本text的长度不能超过120个GBK字节。

+ 8 - 12
moca/src/main/java/com/yingyangfly/moca/record/ListenRecordFragment.kt

@@ -19,12 +19,7 @@ class ListenRecordFragment : BaseFragment<FragmentListenRecordBinding>() {
      */
     private var questionId = 0
     private var playVoice: PlayVoice? = null
-
-    private var firstWord = ""
-    private var secondWord = ""
-    private var thirdWord = ""
-    private var fourthWord = ""
-    private var fifthWord = ""
+    private val words = mutableListOf<String>()
 
     override fun onCreate(savedInstanceState: Bundle?) {
         questionId = arguments?.getInt("questionId") ?: 33
@@ -57,7 +52,7 @@ class ListenRecordFragment : BaseFragment<FragmentListenRecordBinding>() {
             }
 
             imagePlay.setOnSingleClickListener {
-                playVoice?.speakWithParagraph(firstWord, secondWord, thirdWord)
+                playVoice?.speakWithParagraph(words)
             }
         }
     }
@@ -75,30 +70,31 @@ class ListenRecordFragment : BaseFragment<FragmentListenRecordBinding>() {
      * 加载数据
      */
     private fun loadData() {
+        words.clear()
         if (dao != null) {
             val firstquestion = dao?.getQuestion(questionId)
             if (firstquestion != null) {
-                firstWord = firstquestion.reviewItem
+                words.add(firstquestion.reviewItem)
             }
             val secondQuestionnId = questionId + 1
             val secondQuestion = dao?.getQuestion(secondQuestionnId)
             if (secondQuestion != null) {
-                secondWord = secondQuestion.reviewItem
+                words.add(secondQuestion.reviewItem)
             }
             val thirdQuestionId = questionId + 2
             val thirdQuestion = dao?.getQuestion(thirdQuestionId)
             if (thirdQuestion != null) {
-                thirdWord = thirdQuestion.reviewItem
+                words.add(thirdQuestion.reviewItem)
             }
             val fourthQuestionnId = questionId + 3
             val fourthQuestion = dao?.getQuestion(fourthQuestionnId)
             if (fourthQuestion != null) {
-                fourthWord = fourthQuestion.reviewItem
+                words.add(fourthQuestion.reviewItem)
             }
             val fifthQuestionId = questionId + 4
             val fifthQuestion = dao?.getQuestion(fifthQuestionId)
             if (fifthQuestion != null) {
-                fifthWord = fifthQuestion.reviewItem
+                words.add(fifthQuestion.reviewItem)
             }
         }
     }