Procházet zdrojové kódy

1.优化语音播放时机

王鹏鹏 před 2 roky
rodič
revize
0fd6bad391

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

@@ -7,12 +7,10 @@ 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
@@ -32,14 +30,8 @@ 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")
@@ -124,61 +116,4 @@ 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()
-    }
 }

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

@@ -14,8 +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.utils.LiveEventBusUtil
 import com.yingyangfly.baselib.utils.RxBusCodes
-import gorden.rxbus2.RxBus
 
 /**
  * 听录音重复
@@ -27,7 +27,6 @@ class JudgmentRecordFragment : BaseFragment<FragmentJudgmentRecordBinding>(), Vi
      */
     private var questionId = 0
     private var question: QuestionsBean? = null
-//    private var playVoice: PlayVoice? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
         questionId = arguments?.getInt("questionId") ?: 24
@@ -35,8 +34,6 @@ class JudgmentRecordFragment : BaseFragment<FragmentJudgmentRecordBinding>(), Vi
     }
 
     override fun initViews() {
-//        playVoice = PlayVoice()
-//        playVoice?.setContext(mContext)
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -105,8 +102,7 @@ class JudgmentRecordFragment : BaseFragment<FragmentJudgmentRecordBinding>(), Vi
                     }
                     R.id.soundImage -> {
                         if (question != null) {
-                            RxBus.get().send(RxBusCodes.LoadedVoice, question?.reviewItem!!)
-//                            playVoice?.speak(question?.reviewItem!!)
+                            LiveEventBusUtil.send(RxBusCodes.SPEECHSYNTHESIS, question?.reviewItem!!)
                         }
                     }
                 }
@@ -145,4 +141,9 @@ class JudgmentRecordFragment : BaseFragment<FragmentJudgmentRecordBinding>(), Vi
             }
         }
     }
+
+    override fun onPause() {
+        super.onPause()
+        LiveEventBusUtil.send(RxBusCodes.STOPVOICE, "")
+    }
 }

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

@@ -11,8 +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.utils.LiveEventBusUtil
 import com.yingyangfly.baselib.utils.RxBusCodes
-import gorden.rxbus2.RxBus
 
 /**
  * 听录音做动作
@@ -35,8 +35,6 @@ class RecordActionFragment : BaseFragment<FragmentRecordActionBinding>(), View.O
     }
 
     override fun initViews() {
-//        playVoice = PlayVoice()
-//        playVoice?.setContext(mContext)
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -79,9 +77,7 @@ class RecordActionFragment : BaseFragment<FragmentRecordActionBinding>(), View.O
 
     override fun onPause() {
         super.onPause()
-//        if (playVoice != null) {
-//            playVoice?.stop()
-//        }
+        LiveEventBusUtil.send(RxBusCodes.STOPVOICE, "")
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -105,8 +101,7 @@ class RecordActionFragment : BaseFragment<FragmentRecordActionBinding>(), View.O
                         val stringBuffer = StringBuffer()
                         stringBuffer.append(firstWord).append(",").append(secondWord).append(",")
                             .append(thirdWord)
-                        RxBus.get().send(RxBusCodes.LoadedVoice, stringBuffer.toString())
-//                        playVoice?.speakWithParagraph(firstWord, secondWord, thirdWord)
+                        LiveEventBusUtil.send(RxBusCodes.SPEECHSYNTHESIS, stringBuffer.toString())
                     }
                 }
             }

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

@@ -11,8 +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.utils.LiveEventBusUtil
 import com.yingyangfly.baselib.utils.RxBusCodes
-import gorden.rxbus2.RxBus
 
 /**
  * 听录音判断
@@ -35,8 +35,7 @@ class SoundRecordFragment : BaseFragment<FragmentSoundRecordBinding>(), View.OnT
     }
 
     override fun initViews() {
-//        playVoice = PlayVoice()
-//        playVoice?.setContext(mContext)
+
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -81,9 +80,7 @@ class SoundRecordFragment : BaseFragment<FragmentSoundRecordBinding>(), View.OnT
 
     override fun onPause() {
         super.onPause()
-//        if (playVoice != null) {
-//            playVoice?.stop()
-//        }
+        LiveEventBusUtil.send(RxBusCodes.STOPVOICE, "")
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -107,8 +104,7 @@ class SoundRecordFragment : BaseFragment<FragmentSoundRecordBinding>(), View.OnT
                         val stringBuffer = StringBuffer()
                         stringBuffer.append(firstWord).append(",").append(secondWord).append(",")
                             .append(thirdWord)
-                        RxBus.get().send(RxBusCodes.LoadedVoice, stringBuffer.toString())
-//                        playVoice?.speakWithParagraph(firstWord, secondWord, thirdWord)
+                        LiveEventBusUtil.send(RxBusCodes.SPEECHSYNTHESIS, stringBuffer.toString())
                     }
                 }
             }

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

@@ -7,12 +7,10 @@ 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
@@ -31,11 +29,9 @@ 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")
@@ -119,62 +115,4 @@ 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()
-    }
-
 }

+ 4 - 9
moca/src/main/java/com/yingyangfly/moca/listenspeak/ListenSpeakFragment.kt

@@ -12,8 +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.utils.LiveEventBusUtil
 import com.yingyangfly.baselib.utils.RxBusCodes
-import gorden.rxbus2.RxBus
 
 class ListenSpeakFragment : BaseFragment<FragmentListenSpeakBinding>(),
     View.OnTouchListener {
@@ -21,7 +21,6 @@ class ListenSpeakFragment : BaseFragment<FragmentListenSpeakBinding>(),
      * 问题id
      */
     private var questionId = 0
-//    private var playVoice: PlayVoice? = null
     private var questionsBean: QuestionsBean? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -30,8 +29,7 @@ class ListenSpeakFragment : BaseFragment<FragmentListenSpeakBinding>(),
     }
 
     override fun initViews() {
-//        playVoice = PlayVoice()
-//        playVoice?.setContext(mContext)
+
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -68,9 +66,7 @@ class ListenSpeakFragment : BaseFragment<FragmentListenSpeakBinding>(),
 
     override fun onPause() {
         super.onPause()
-//        if (playVoice != null) {
-//            playVoice?.stop()
-//        }
+        LiveEventBusUtil.send(RxBusCodes.STOPVOICE, "")
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -92,8 +88,7 @@ class ListenSpeakFragment : BaseFragment<FragmentListenSpeakBinding>(),
                     }
                     R.id.imagePlay -> {
                         if (questionsBean != null) {
-                            RxBus.get().send(RxBusCodes.LoadedVoice, questionsBean?.reviewDesc!!)
-//                            playVoice?.speak(questionsBean?.reviewDesc!!)
+                            LiveEventBusUtil.send(RxBusCodes.SPEECHSYNTHESIS, questionsBean?.reviewDesc!!)
                         }
                     }
                 }

+ 4 - 9
moca/src/main/java/com/yingyangfly/moca/memorizenumbers/MemorizeNumbersFragment.kt

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

+ 4 - 9
moca/src/main/java/com/yingyangfly/moca/record/ListenRecordFragment.kt

@@ -11,8 +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.utils.LiveEventBusUtil
 import com.yingyangfly.baselib.utils.RxBusCodes
-import gorden.rxbus2.RxBus
 
 /**
  * 听录音说答案
@@ -24,7 +24,6 @@ class ListenRecordFragment : BaseFragment<FragmentListenRecordBinding>(),
      * 问题id
      */
     private var questionId = 0
-//    private var playVoice: PlayVoice? = null
     private val words = mutableListOf<String>()
 
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -33,8 +32,7 @@ class ListenRecordFragment : BaseFragment<FragmentListenRecordBinding>(),
     }
 
     override fun initViews() {
-//        playVoice = PlayVoice()
-//        playVoice?.setContext(mContext)
+
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -95,9 +93,7 @@ class ListenRecordFragment : BaseFragment<FragmentListenRecordBinding>(),
 
     override fun onPause() {
         super.onPause()
-//        if (playVoice != null) {
-//            playVoice?.stop()
-//        }
+        LiveEventBusUtil.send(RxBusCodes.STOPVOICE, "")
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -118,8 +114,7 @@ class ListenRecordFragment : BaseFragment<FragmentListenRecordBinding>(),
                         nextPage(v)
                     }
                     R.id.imagePlay -> {
-                        RxBus.get().send(RxBusCodes.LoadedVoice, words)
-//                        playVoice?.speakWithParagraph(words)
+                        LiveEventBusUtil.send(RxBusCodes.SPEECHSYNTHESIS, words)
                     }
                 }
             }