Browse Source

1.优化语音播放时机

王鹏鹏 2 years ago
parent
commit
e44e2e3130

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

@@ -84,4 +84,19 @@ object RxBusCodes {
      * 游戏设置全屏
      */
     const val GAMEFULLSCREEN = "gameFullScreen"
+
+    /**
+     * 语音合成
+     */
+    const val SPEECHSYNTHESIS = "speechSynthesis"
+
+    /**
+     * 结束语音播放
+     */
+    const val STOPVOICE = "stopVoice"
+
+    /**
+     * 开始播放音乐
+     */
+    const val STARTMUSIC = "startMusic"
 }

+ 2 - 72
game/src/main/java/com/yingyangfly/game/introduction/GameIntroductionActivity.kt

@@ -1,9 +1,6 @@
 package com.yingyangfly.game.introduction
 
 import android.annotation.SuppressLint
-import android.app.Activity
-import android.app.ActivityManager
-import android.content.Context
 import android.content.Intent
 import android.os.Bundle
 import android.text.TextUtils
@@ -13,14 +10,12 @@ import com.alibaba.android.arouter.facade.annotation.Route
 import com.alibaba.android.arouter.launcher.ARouter
 import com.tencent.liteav.demo.superplayer.SuperPlayerModel
 import com.yingyangfly.baselib.config.AccountConfig
-import com.yingyangfly.baselib.db.VoicePlayerBean
 import com.yingyangfly.baselib.dialog.GameLoadingDialog
 import com.yingyangfly.baselib.ext.getEndAnimation
 import com.yingyangfly.baselib.ext.getScaleAnimation
 import com.yingyangfly.baselib.ext.show
 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.LiveEventBusUtil
 import com.yingyangfly.baselib.utils.RxBusCodes
@@ -47,11 +42,6 @@ class GameIntroductionActivity :
     private var gameBackgroundImage: String = ""
     private var gameBean: GameBean? = null
 
-    /**
-     * 语音合成
-     */
-    private var voicePlayer: VoicePlayer? = null
-
     override fun onCreate(savedInstanceState: Bundle?) {
         desn = intent.getStringExtra("desn") ?: ""
         gameCode = intent.getStringExtra("gameCode") ?: ""
@@ -93,7 +83,6 @@ class GameIntroductionActivity :
 
     override fun onResume() {
         super.onResume()
-        voicePlayer = VoicePlayer.getInstance(mContext)
         getGameDetailByCode()
     }
 
@@ -104,11 +93,7 @@ class GameIntroductionActivity :
 
     override fun onPause() {
         binding.playTxCloudView.onPause()
-        if (voicePlayer != null) {
-            if (voicePlayer!!.isPlaying) {
-                voicePlayer?.stop()
-            }
-        }
+        LiveEventBusUtil.send(RxBusCodes.STOPVOICE, "")
         super.onPause()
     }
 
@@ -215,52 +200,7 @@ class GameIntroductionActivity :
      * 语音合成
      */
     private fun speak() {
-        if (db != null) {
-            val voicePlayerDao = db?.getVoicePlayerDao()
-            if (voicePlayerDao != null) {
-                val voicePlayerBean = voicePlayerDao.getVoicePlayerBean(desn)
-                if (voicePlayerBean != null) {
-                    if (voicePlayer != null) {
-                        voicePlayer?.play(voicePlayerBean.url) {
-
-                        }
-                    }
-                } else {
-                    getVoiceUrl(desn)
-                }
-            } else {
-                getVoiceUrl(desn)
-            }
-        } else {
-            getVoiceUrl(desn)
-        }
-    }
-
-    /**
-     * 获取声音url
-     */
-    private fun getVoiceUrl(taskDesn: String) {
-        viewModel.getVoiceUrl(taskDesn, fail = {
-        }, 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) {
-                    if (isTopActivity(this)) {
-                        voicePlayer?.play(it) {
-                        }
-                    }
-                }
-            }
-        })
+        LiveEventBusUtil.send(RxBusCodes.SPEECHSYNTHESIS, desn)
     }
 
     /**
@@ -299,14 +239,4 @@ class GameIntroductionActivity :
             binding.playTxCloudView.playWithModelNeedLicence(model)
         }
     }
-
-    /**
-     * 判断当前activity是否位于栈顶
-     */
-    private fun isTopActivity(activity: Activity): Boolean {
-        val am = activity.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
-        val cn = am.getRunningTasks(1)[0].topActivity
-        return cn!!.className == activity.javaClass.name
-    }
-
 }

+ 22 - 86
game/src/main/java/com/yingyangfly/game/play/PlayGameActivity.kt

@@ -1,9 +1,6 @@
 package com.yingyangfly.game.play
 
 import android.annotation.SuppressLint
-import android.app.Activity
-import android.app.ActivityManager
-import android.content.Context
 import android.content.Intent
 import android.os.Build
 import android.os.Bundle
@@ -24,10 +21,8 @@ import com.alibaba.android.arouter.launcher.ARouter
 import com.bumptech.glide.Glide
 import com.yingyangfly.baselib.bean.GameDataBean
 import com.yingyangfly.baselib.bean.GetSaveGameRecordBean
-import com.yingyangfly.baselib.db.VoicePlayerBean
 import com.yingyangfly.baselib.ext.*
 import com.yingyangfly.baselib.mvvm.BaseMVVMActivity
-import com.yingyangfly.baselib.player.VoicePlayer
 import com.yingyangfly.baselib.router.RouterUrlCommon
 import com.yingyangfly.baselib.utils.GsonUtil
 import com.yingyangfly.baselib.utils.LiveEventBusUtil
@@ -71,7 +66,6 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
     private var gameBackgroundImage = ""
     private var gameShortDesn = ""
     private lateinit var webSettings: WebSettings
-    private var voicePlayer: VoicePlayer? = null
     private var gameResultDialog: GameResultDialog? = null
 
     var mWebViewHeight = 0
@@ -91,7 +85,6 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
     }
 
     override fun initViews() {
-        voicePlayer = VoicePlayer.getInstance(mContext)
         if (TextUtils.isEmpty(url).not()) {
             initWebView(url)
         }
@@ -316,11 +309,7 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
 
     override fun onPause() {
         super.onPause()
-        if (voicePlayer != null) {
-            if (voicePlayer!!.isPlaying) {
-                voicePlayer?.stop()
-            }
-        }
+        LiveEventBusUtil.send(RxBusCodes.STOPVOICE, "")
     }
 
     /**
@@ -393,24 +382,30 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
         }
         //游戏加载完成
         LiveEventBusUtil.observer<String>(this, RxBusCodes.LOADINGOVER) {
-            loadingOver = true
-            if (countdownSuccess) {
-                callStartGame()
+            runOnUiThread {
+                loadingOver = true
+                if (countdownSuccess) {
+                    callStartGame()
+                }
             }
         }
         //播放语音
         LiveEventBusUtil.observer<String>(this, RxBusCodes.PLAYVOICE) {
-            if (it.isNullOrEmpty().not()) {
-                val msg = it.split("voice:")
-                if (msg.isNullOrEmpty().not()) {
-                    binding.tvIntroduce.text = msg[1]
-                    speak(msg[1])
+            runOnUiThread {
+                if (it.isNullOrEmpty().not()) {
+                    val msg = it.split("voice:")
+                    if (msg.isNullOrEmpty().not()) {
+                        binding.tvIntroduce.text = msg[1]
+                        speak(msg[1])
+                    }
                 }
             }
         }
         //游戏超时退出
         LiveEventBusUtil.observer<String>(this, RxBusCodes.GAMEOVERTIME) {
-            finish()
+            runOnUiThread {
+                finish()
+            }
         }
         //返回首页结束游戏
         LiveEventBusUtil.observer<String>(this, RxBusCodes.CALLQUITGAME) {
@@ -437,63 +432,17 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
                 setReduce(false)
             }
         }
+        //播放音乐
+        LiveEventBusUtil.observer<String>(this, RxBusCodes.STARTMUSIC) {
+            callPlayBgMusic()
+        }
     }
 
     /**
      * 语音合成
      */
     private fun speak(desn: String) {
-        if (db != null) {
-            val voicePlayerDao = db?.getVoicePlayerDao()
-            if (voicePlayerDao != null) {
-                val voicePlayerBean = voicePlayerDao.getVoicePlayerBean(desn)
-                if (voicePlayerBean != null) {
-                    if (voicePlayer != null) {
-                        if (isTopActivity(this)) {
-                            voicePlayer?.play(voicePlayerBean.url) {
-                                binding.tvIntroduce.text = gameShortDesn
-                                callPlayBgMusic()
-                            }
-                        }
-                    }
-                } else {
-                    getVoiceUrl(desn)
-                }
-            } else {
-                getVoiceUrl(desn)
-            }
-        } else {
-            getVoiceUrl(desn)
-        }
-    }
-
-    /**
-     * 获取声音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) {
-                    if (isTopActivity(this)) {
-                        voicePlayer?.play(it) {
-
-                        }
-                    }
-                }
-            }
-        })
+        LiveEventBusUtil.send(RxBusCodes.SPEECHSYNTHESIS, desn)
     }
 
     /**
@@ -501,11 +450,7 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
      */
     private fun callPlayBgMusic() {
         runOnUiThread {
-            binding.webView.evaluateJavascript(
-                "javascript:CallPlayBgMusic()"
-            ) {
-
-            }
+            binding.webView.evaluateJavascript("javascript:CallPlayBgMusic()") {}
         }
     }
 
@@ -579,15 +524,6 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
         )
     }
 
-    /**
-     * 判断当前activity是否位于栈顶
-     */
-    private fun isTopActivity(activity: Activity): Boolean {
-        val am = activity.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
-        val cn = am.getRunningTasks(1)[0].topActivity
-        return cn!!.className == activity.javaClass.name
-    }
-
     /**
      * 是否缩放
      */

+ 74 - 0
home/src/main/java/com/yingyangfly/home/activity/HomeActivity.kt

@@ -14,6 +14,7 @@ import com.yingyang.home.R
 import com.yingyang.home.databinding.ActivityHomeBinding
 import com.yingyangfly.baselib.bean.GetSaveGameRecordBean
 import com.yingyangfly.baselib.bean.Record
+import com.yingyangfly.baselib.db.VoicePlayerBean
 import com.yingyangfly.baselib.dialog.TaskFragment
 import com.yingyangfly.baselib.dialog.TipsDialog
 import com.yingyangfly.baselib.ext.getEndAnimation
@@ -23,6 +24,7 @@ import com.yingyangfly.baselib.ext.toast
 import com.yingyangfly.baselib.guideview.Guide
 import com.yingyangfly.baselib.guideview.GuideBuilder
 import com.yingyangfly.baselib.mvvm.BaseMVVMActivity
+import com.yingyangfly.baselib.player.VoicePlayer
 import com.yingyangfly.baselib.router.RouterUrlCommon
 import com.yingyangfly.baselib.utils.*
 import com.yingyangfly.home.adapter.GameAdapter
@@ -52,11 +54,17 @@ class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(),
     private var gameList = mutableListOf<Record>()
     private var isFirstPage = true
 
+    /**
+     * 语音合成
+     */
+    private var voicePlayer: VoicePlayer? = null
+
     //推荐游戏分页数据
     private var pageList = mutableListOf<Record>()
     private val gameAdapter by lazy { GameAdapter() }
 
     override fun initViews() {
+        voicePlayer = VoicePlayer.getInstance(mContext)
         binding {
             rvGame.layoutManager = GridLayoutManager(this@HomeActivity, 2)
             rvGame.adapter = gameAdapter
@@ -628,6 +636,22 @@ class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(),
                 saveData(it)
             }
         }
+
+        //语音合成
+        LiveEventBusUtil.observer<String>(this, RxBusCodes.SPEECHSYNTHESIS) {
+            if (TextUtils.isEmpty(it).not()) {
+                speak(it)
+            }
+        }
+
+        //结束语音播放
+        LiveEventBusUtil.observer<String>(this, RxBusCodes.STOPVOICE) {
+            if (voicePlayer != null) {
+                if (voicePlayer!!.isPlaying) {
+                    voicePlayer?.stop()
+                }
+            }
+        }
     }
 
     /**
@@ -638,4 +662,54 @@ class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(),
         }, success = {
         })
     }
+
+    /**
+     * 语音合成
+     */
+    private fun speak(desn: String) {
+        if (db != null) {
+            val voicePlayerDao = db?.getVoicePlayerDao()
+            if (voicePlayerDao != null) {
+                val voicePlayerBean = voicePlayerDao.getVoicePlayerBean(desn)
+                if (voicePlayerBean != null) {
+                    if (voicePlayer != null) {
+                        voicePlayer?.play(voicePlayerBean.url) {
+                            LiveEventBusUtil.send(RxBusCodes.STARTMUSIC, "")
+                        }
+                    }
+                } else {
+                    getVoiceUrl(desn)
+                }
+            } else {
+                getVoiceUrl(desn)
+            }
+        } else {
+            getVoiceUrl(desn)
+        }
+    }
+
+    /**
+     * 获取声音url
+     */
+    private fun getVoiceUrl(taskDesn: String) {
+        viewModel.getVoiceUrl(taskDesn, fail = {
+        }, 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) {
+                    }
+                }
+            }
+        })
+    }
 }