Browse Source

1.替换百度语音合成sdk

王鹏鹏 2 years ago
parent
commit
9d3c52c2f5

+ 3 - 0
baselib/src/main/java/com/yingyangfly/baselib/utils/RxBusCodes.kt

@@ -22,4 +22,7 @@ object RxBusCodes {
 
     //购买成功
     const val SuccessfulPurchase = 9005
+
+    //加载语音
+    const val LoadedVoice = 9006
 }

+ 66 - 0
mmse/src/main/java/com/yingyangfly/mmse/activity/QuestionsActivity.kt

@@ -7,10 +7,12 @@ import android.view.View
 import com.alibaba.android.arouter.facade.annotation.Route
 import com.yingyang.mmse.R
 import com.yingyang.mmse.databinding.ActivityQuestionsBinding
+import com.yingyangfly.baselib.db.VoicePlayerBean
 import com.yingyangfly.baselib.ext.getEndAnimation
 import com.yingyangfly.baselib.ext.getScaleAnimation
 import com.yingyangfly.baselib.ext.toast
 import com.yingyangfly.baselib.mvvm.BaseMVVMActivity
+import com.yingyangfly.baselib.player.VoicePlayer
 import com.yingyangfly.baselib.router.RouterUrlCommon
 import com.yingyangfly.baselib.utils.JumpUtil
 import com.yingyangfly.baselib.utils.RxBusCodes
@@ -30,8 +32,14 @@ class QuestionsActivity : BaseMVVMActivity<ActivityQuestionsBinding, QuestionsVi
     lateinit var rxTimer: RxTimer
     var time: Long = 0
 
+    /**
+     * 语音合成
+     */
+    private var voicePlayer: VoicePlayer? = null
+
     override fun initViews() {
         rxTimer = RxTimer()
+        voicePlayer = VoicePlayer.getInstance(mContext)
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -113,6 +121,64 @@ class QuestionsActivity : BaseMVVMActivity<ActivityQuestionsBinding, QuestionsVi
                 }
             }
         }
+        RxBusCodes
         return true
     }
+
+    @Subscribe(code = RxBusCodes.LoadedVoice, threadMode = ThreadMode.MAIN)
+    fun speak(str: String) {
+        if (db != null) {
+            val voicePlayerDao = db?.getVoicePlayerDao()
+            if (voicePlayerDao != null) {
+                val voicePlayerBean = voicePlayerDao.getVoicePlayerBean(str)
+                if (voicePlayerBean != null) {
+                    if (voicePlayer != null) {
+                        voicePlayer?.play(voicePlayerBean.url) {
+
+                        }
+                    }
+                } else {
+                    getVoiceUrl(str)
+                }
+            } else {
+                getVoiceUrl(str)
+            }
+        } else {
+            getVoiceUrl(str)
+        }
+    }
+
+    /**
+     * 获取声音url
+     */
+    private fun getVoiceUrl(taskDesn: String) {
+        viewModel.getVoiceUrl(taskDesn, fail = {
+            it.toast()
+        }, success = {
+            if (TextUtils.isEmpty(it).not()) {
+                if (db != null) {
+                    val voicePlayerDao = db?.getVoicePlayerDao()
+                    if (voicePlayerDao != null) {
+                        val voicePlayerBean = VoicePlayerBean().apply {
+                            url = it
+                            words = taskDesn
+                        }
+                        voicePlayerDao.insert(voicePlayerBean)
+                    }
+                }
+                if (voicePlayer != null) {
+                    voicePlayer?.play(it) {
+
+                    }
+                }
+            }
+        })
+    }
+
+    override fun onDestroy() {
+        if (voicePlayer != null && voicePlayer!!.isPlaying) {
+            voicePlayer?.stop()
+        }
+        super.onDestroy()
+    }
 }

+ 14 - 0
mmse/src/main/java/com/yingyangfly/mmse/activity/QuestionsViewModel.kt

@@ -22,4 +22,18 @@ class QuestionsViewModel : BaseViewModel() {
         success,
         fail
     )
+
+    /**
+     * 获取声音url
+     */
+    fun getVoiceUrl(
+        msg: String,
+        fail: ((msg: String) -> Unit)? = null,
+        success: ((success: String?) -> Unit)? = null,
+    ) = launchFlow(false) {
+        MMSE_API.getVoiceUrl(msg)
+    }.runUI(
+        success,
+        fail
+    )
 }

+ 7 - 5
mmse/src/main/java/com/yingyangfly/mmse/fragment/JudgmentRecordFragment.kt

@@ -14,7 +14,8 @@ import com.yingyangfly.baselib.db.QuestionsBean
 import com.yingyangfly.baselib.ext.getEndAnimation
 import com.yingyangfly.baselib.ext.getScaleAnimation
 import com.yingyangfly.baselib.ext.toast
-import com.yingyangfly.baselib.voice.PlayVoice
+import com.yingyangfly.baselib.utils.RxBusCodes
+import gorden.rxbus2.RxBus
 
 /**
  * 听录音重复
@@ -26,7 +27,7 @@ class JudgmentRecordFragment : BaseFragment<FragmentJudgmentRecordBinding>(), Vi
      */
     private var questionId = 0
     private var question: QuestionsBean? = null
-    private var playVoice: PlayVoice? = null
+//    private var playVoice: PlayVoice? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
         questionId = arguments?.getInt("questionId") ?: 24
@@ -34,8 +35,8 @@ class JudgmentRecordFragment : BaseFragment<FragmentJudgmentRecordBinding>(), Vi
     }
 
     override fun initViews() {
-        playVoice = PlayVoice()
-        playVoice?.setContext(mContext)
+//        playVoice = PlayVoice()
+//        playVoice?.setContext(mContext)
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -104,7 +105,8 @@ class JudgmentRecordFragment : BaseFragment<FragmentJudgmentRecordBinding>(), Vi
                     }
                     R.id.soundImage -> {
                         if (question != null) {
-                            playVoice?.speak(question?.reviewItem!!)
+                            RxBus.get().send(RxBusCodes.LoadedVoice, question?.reviewItem!!)
+//                            playVoice?.speak(question?.reviewItem!!)
                         }
                     }
                 }

+ 13 - 8
mmse/src/main/java/com/yingyangfly/mmse/fragment/RecordActionFragment.kt

@@ -11,7 +11,8 @@ import com.yingyang.mmse.databinding.FragmentRecordActionBinding
 import com.yingyangfly.baselib.base.BaseFragment
 import com.yingyangfly.baselib.ext.getEndAnimation
 import com.yingyangfly.baselib.ext.getScaleAnimation
-import com.yingyangfly.baselib.voice.PlayVoice
+import com.yingyangfly.baselib.utils.RxBusCodes
+import gorden.rxbus2.RxBus
 
 /**
  * 听录音做动作
@@ -26,7 +27,7 @@ class RecordActionFragment : BaseFragment<FragmentRecordActionBinding>(), View.O
     private var secondWord = ""
     private var thirdWord = ""
 
-    private var playVoice: PlayVoice? = null
+//    private var playVoice: PlayVoice? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
         questionId = arguments?.getInt("questionId") ?: 26
@@ -34,8 +35,8 @@ class RecordActionFragment : BaseFragment<FragmentRecordActionBinding>(), View.O
     }
 
     override fun initViews() {
-        playVoice = PlayVoice()
-        playVoice?.setContext(mContext)
+//        playVoice = PlayVoice()
+//        playVoice?.setContext(mContext)
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -78,9 +79,9 @@ class RecordActionFragment : BaseFragment<FragmentRecordActionBinding>(), View.O
 
     override fun onPause() {
         super.onPause()
-        if (playVoice != null) {
-            playVoice?.stop()
-        }
+//        if (playVoice != null) {
+//            playVoice?.stop()
+//        }
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -101,7 +102,11 @@ class RecordActionFragment : BaseFragment<FragmentRecordActionBinding>(), View.O
                         pageNext(v)
                     }
                     R.id.soundImage -> {
-                        playVoice?.speakWithParagraph(firstWord, secondWord, thirdWord)
+                        val stringBuffer = StringBuffer()
+                        stringBuffer.append(firstWord).append(",").append(secondWord).append(",")
+                            .append(thirdWord)
+                        RxBus.get().send(RxBusCodes.LoadedVoice, stringBuffer.toString())
+//                        playVoice?.speakWithParagraph(firstWord, secondWord, thirdWord)
                     }
                 }
             }

+ 13 - 8
mmse/src/main/java/com/yingyangfly/mmse/fragment/SoundRecordFragment.kt

@@ -11,7 +11,8 @@ import com.yingyang.mmse.databinding.FragmentSoundRecordBinding
 import com.yingyangfly.baselib.base.BaseFragment
 import com.yingyangfly.baselib.ext.getEndAnimation
 import com.yingyangfly.baselib.ext.getScaleAnimation
-import com.yingyangfly.baselib.voice.PlayVoice
+import com.yingyangfly.baselib.utils.RxBusCodes
+import gorden.rxbus2.RxBus
 
 /**
  * 听录音判断
@@ -26,7 +27,7 @@ class SoundRecordFragment : BaseFragment<FragmentSoundRecordBinding>(), View.OnT
     private var secondWord = ""
     private var thirdWord = ""
 
-    private var playVoice: PlayVoice? = null
+//    private var playVoice: PlayVoice? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
         questionId = arguments?.getInt("questionId") ?: 11
@@ -34,8 +35,8 @@ class SoundRecordFragment : BaseFragment<FragmentSoundRecordBinding>(), View.OnT
     }
 
     override fun initViews() {
-        playVoice = PlayVoice()
-        playVoice?.setContext(mContext)
+//        playVoice = PlayVoice()
+//        playVoice?.setContext(mContext)
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -80,9 +81,9 @@ class SoundRecordFragment : BaseFragment<FragmentSoundRecordBinding>(), View.OnT
 
     override fun onPause() {
         super.onPause()
-        if (playVoice != null) {
-            playVoice?.stop()
-        }
+//        if (playVoice != null) {
+//            playVoice?.stop()
+//        }
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -103,7 +104,11 @@ class SoundRecordFragment : BaseFragment<FragmentSoundRecordBinding>(), View.OnT
                         pageNext(v)
                     }
                     R.id.soundImage -> {
-                        playVoice?.speakWithParagraph(firstWord, secondWord, thirdWord)
+                        val stringBuffer = StringBuffer()
+                        stringBuffer.append(firstWord).append(",").append(secondWord).append(",")
+                            .append(thirdWord)
+                        RxBus.get().send(RxBusCodes.LoadedVoice, stringBuffer.toString())
+//                        playVoice?.speakWithParagraph(firstWord, secondWord, thirdWord)
                     }
                 }
             }

+ 9 - 0
mmse/src/main/java/com/yingyangfly/mmse/net/MmseApiService.kt

@@ -4,6 +4,7 @@ import com.yingyangfly.baselib.net.BaseResp
 import okhttp3.RequestBody
 import retrofit2.http.Body
 import retrofit2.http.POST
+import retrofit2.http.Query
 
 interface MmseApiService {
 
@@ -12,4 +13,12 @@ interface MmseApiService {
      */
     @POST("app/review/saveReviewRecord")
     suspend fun submitQuestions(@Body requestBody: RequestBody): BaseResp<Unit>
+
+    /**
+     * 获取声音url
+     */
+    @POST("app/video/getVoiceUrl")
+    suspend fun getVoiceUrl(
+        @Query("voiceMsg") voiceMsg: String
+    ): BaseResp<String>
 }

+ 61 - 0
moca/src/main/java/com/yingyangfly/moca/activity/MocaActivity.kt

@@ -7,10 +7,12 @@ import android.view.View
 import com.alibaba.android.arouter.facade.annotation.Route
 import com.yingyang.moca.R
 import com.yingyang.moca.databinding.ActivityMocaBinding
+import com.yingyangfly.baselib.db.VoicePlayerBean
 import com.yingyangfly.baselib.ext.getEndAnimation
 import com.yingyangfly.baselib.ext.getScaleAnimation
 import com.yingyangfly.baselib.ext.toast
 import com.yingyangfly.baselib.mvvm.BaseMVVMActivity
+import com.yingyangfly.baselib.player.VoicePlayer
 import com.yingyangfly.baselib.router.RouterUrlCommon
 import com.yingyangfly.baselib.utils.JumpUtil
 import com.yingyangfly.baselib.utils.RxBusCodes
@@ -29,9 +31,11 @@ class MocaActivity : BaseMVVMActivity<ActivityMocaBinding, MocaViewModel>(),
     private var reviewTaskId = ""
     lateinit var rxTimer: RxTimer
     var time: Long = 0
+    private var voicePlayer: VoicePlayer? = null
 
     override fun initViews() {
         rxTimer = RxTimer()
+        voicePlayer = VoicePlayer.getInstance(mContext)
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -116,4 +120,61 @@ class MocaActivity : BaseMVVMActivity<ActivityMocaBinding, MocaViewModel>(),
         return true
     }
 
+    @Subscribe(code = RxBusCodes.LoadedVoice, threadMode = ThreadMode.MAIN)
+    fun speak(str: String) {
+        if (db != null) {
+            val voicePlayerDao = db?.getVoicePlayerDao()
+            if (voicePlayerDao != null) {
+                val voicePlayerBean = voicePlayerDao.getVoicePlayerBean(str)
+                if (voicePlayerBean != null) {
+                    if (voicePlayer != null) {
+                        voicePlayer?.play(voicePlayerBean.url) {
+
+                        }
+                    }
+                } else {
+                    getVoiceUrl(str)
+                }
+            } else {
+                getVoiceUrl(str)
+            }
+        } else {
+            getVoiceUrl(str)
+        }
+    }
+
+    /**
+     * 获取声音url
+     */
+    private fun getVoiceUrl(taskDesn: String) {
+        viewModel.getVoiceUrl(taskDesn, fail = {
+            it.toast()
+        }, success = {
+            if (TextUtils.isEmpty(it).not()) {
+                if (db != null) {
+                    val voicePlayerDao = db?.getVoicePlayerDao()
+                    if (voicePlayerDao != null) {
+                        val voicePlayerBean = VoicePlayerBean().apply {
+                            url = it
+                            words = taskDesn
+                        }
+                        voicePlayerDao.insert(voicePlayerBean)
+                    }
+                }
+                if (voicePlayer != null) {
+                    voicePlayer?.play(it) {
+
+                    }
+                }
+            }
+        })
+    }
+
+    override fun onDestroy() {
+        if (voicePlayer != null && voicePlayer!!.isPlaying) {
+            voicePlayer?.stop()
+        }
+        super.onDestroy()
+    }
+
 }

+ 14 - 0
moca/src/main/java/com/yingyangfly/moca/activity/MocaViewModel.kt

@@ -24,4 +24,18 @@ class MocaViewModel : BaseViewModel() {
         success,
         fail
     )
+
+    /**
+     * 获取声音url
+     */
+    fun getVoiceUrl(
+        msg: String,
+        fail: ((msg: String) -> Unit)? = null,
+        success: ((success: String?) -> Unit)? = null,
+    ) = launchFlow(false) {
+        MOCA_API.getVoiceUrl(msg)
+    }.runUI(
+        success,
+        fail
+    )
 }

+ 10 - 8
moca/src/main/java/com/yingyangfly/moca/listenspeak/ListenSpeakFragment.kt

@@ -12,7 +12,8 @@ import com.yingyangfly.baselib.base.BaseFragment
 import com.yingyangfly.baselib.db.QuestionsBean
 import com.yingyangfly.baselib.ext.getEndAnimation
 import com.yingyangfly.baselib.ext.getScaleAnimation
-import com.yingyangfly.baselib.voice.PlayVoice
+import com.yingyangfly.baselib.utils.RxBusCodes
+import gorden.rxbus2.RxBus
 
 class ListenSpeakFragment : BaseFragment<FragmentListenSpeakBinding>(),
     View.OnTouchListener {
@@ -20,7 +21,7 @@ class ListenSpeakFragment : BaseFragment<FragmentListenSpeakBinding>(),
      * 问题id
      */
     private var questionId = 0
-    private var playVoice: PlayVoice? = null
+//    private var playVoice: PlayVoice? = null
     private var questionsBean: QuestionsBean? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -29,8 +30,8 @@ class ListenSpeakFragment : BaseFragment<FragmentListenSpeakBinding>(),
     }
 
     override fun initViews() {
-        playVoice = PlayVoice()
-        playVoice?.setContext(mContext)
+//        playVoice = PlayVoice()
+//        playVoice?.setContext(mContext)
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -67,9 +68,9 @@ class ListenSpeakFragment : BaseFragment<FragmentListenSpeakBinding>(),
 
     override fun onPause() {
         super.onPause()
-        if (playVoice != null) {
-            playVoice?.stop()
-        }
+//        if (playVoice != null) {
+//            playVoice?.stop()
+//        }
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -91,7 +92,8 @@ class ListenSpeakFragment : BaseFragment<FragmentListenSpeakBinding>(),
                     }
                     R.id.imagePlay -> {
                         if (questionsBean != null) {
-                            playVoice?.speak(questionsBean?.reviewDesc!!)
+                            RxBus.get().send(RxBusCodes.LoadedVoice, questionsBean?.reviewDesc!!)
+//                            playVoice?.speak(questionsBean?.reviewDesc!!)
                         }
                     }
                 }

+ 10 - 8
moca/src/main/java/com/yingyangfly/moca/memorizenumbers/MemorizeNumbersFragment.kt

@@ -12,7 +12,8 @@ import com.yingyangfly.baselib.base.BaseFragment
 import com.yingyangfly.baselib.db.QuestionsBean
 import com.yingyangfly.baselib.ext.getEndAnimation
 import com.yingyangfly.baselib.ext.getScaleAnimation
-import com.yingyangfly.baselib.voice.PlayVoice
+import com.yingyangfly.baselib.utils.RxBusCodes
+import gorden.rxbus2.RxBus
 
 /**
  * 背数字
@@ -24,7 +25,7 @@ class MemorizeNumbersFragment : BaseFragment<FragmentMemorizeNumbersBinding>(),
      * 问题id
      */
     private var questionId = 0
-    private var playVoice: PlayVoice? = null
+//    private var playVoice: PlayVoice? = null
     private var question: QuestionsBean? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -33,8 +34,8 @@ class MemorizeNumbersFragment : BaseFragment<FragmentMemorizeNumbersBinding>(),
     }
 
     override fun initViews() {
-        playVoice = PlayVoice()
-        playVoice?.setContext(mContext)
+//        playVoice = PlayVoice()
+//        playVoice?.setContext(mContext)
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -77,9 +78,9 @@ class MemorizeNumbersFragment : BaseFragment<FragmentMemorizeNumbersBinding>(),
 
     override fun onPause() {
         super.onPause()
-        if (playVoice != null) {
-            playVoice?.stop()
-        }
+//        if (playVoice != null) {
+//            playVoice?.stop()
+//        }
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -101,7 +102,8 @@ class MemorizeNumbersFragment : BaseFragment<FragmentMemorizeNumbersBinding>(),
                     }
                     R.id.imagePlay -> {
                         if (question != null) {
-                            playVoice?.speak(question?.reviewDesc!!)
+                            RxBus.get().send(RxBusCodes.LoadedVoice, question?.reviewDesc!!)
+//                            playVoice?.speak(question?.reviewDesc!!)
                         }
                     }
                 }

+ 9 - 0
moca/src/main/java/com/yingyangfly/moca/net/MocaApiService.kt

@@ -4,6 +4,7 @@ import com.yingyangfly.baselib.net.BaseResp
 import okhttp3.RequestBody
 import retrofit2.http.Body
 import retrofit2.http.POST
+import retrofit2.http.Query
 
 interface MocaApiService {
 
@@ -12,4 +13,12 @@ interface MocaApiService {
      */
     @POST("app/review/saveReviewRecord")
     suspend fun submitQuestions(@Body requestBody: RequestBody): BaseResp<Unit>
+
+    /**
+     * 获取声音url
+     */
+    @POST("app/video/getVoiceUrl")
+    suspend fun getVoiceUrl(
+        @Query("voiceMsg") voiceMsg: String
+    ): BaseResp<String>
 }

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

@@ -11,7 +11,8 @@ import com.yingyang.moca.databinding.FragmentListenRecordBinding
 import com.yingyangfly.baselib.base.BaseFragment
 import com.yingyangfly.baselib.ext.getEndAnimation
 import com.yingyangfly.baselib.ext.getScaleAnimation
-import com.yingyangfly.baselib.voice.PlayVoice
+import com.yingyangfly.baselib.utils.RxBusCodes
+import gorden.rxbus2.RxBus
 
 /**
  * 听录音说答案
@@ -23,7 +24,7 @@ class ListenRecordFragment : BaseFragment<FragmentListenRecordBinding>(),
      * 问题id
      */
     private var questionId = 0
-    private var playVoice: PlayVoice? = null
+//    private var playVoice: PlayVoice? = null
     private val words = mutableListOf<String>()
 
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -32,8 +33,8 @@ class ListenRecordFragment : BaseFragment<FragmentListenRecordBinding>(),
     }
 
     override fun initViews() {
-        playVoice = PlayVoice()
-        playVoice?.setContext(mContext)
+//        playVoice = PlayVoice()
+//        playVoice?.setContext(mContext)
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -94,9 +95,9 @@ class ListenRecordFragment : BaseFragment<FragmentListenRecordBinding>(),
 
     override fun onPause() {
         super.onPause()
-        if (playVoice != null) {
-            playVoice?.stop()
-        }
+//        if (playVoice != null) {
+//            playVoice?.stop()
+//        }
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -117,7 +118,8 @@ class ListenRecordFragment : BaseFragment<FragmentListenRecordBinding>(),
                         nextPage(v)
                     }
                     R.id.imagePlay -> {
-                        playVoice?.speakWithParagraph(words)
+                        RxBus.get().send(RxBusCodes.LoadedVoice, words)
+//                        playVoice?.speakWithParagraph(words)
                     }
                 }
             }