Selaa lähdekoodia

1.添加直播中玩游戏功能

王鹏鹏 2 vuotta sitten
vanhempi
commit
97af0f9407

+ 75 - 14
game/src/main/java/com/yingyangfly/game/introduction/GameIntroductionActivity.kt

@@ -1,6 +1,9 @@
 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
@@ -10,12 +13,14 @@ 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
@@ -38,19 +43,18 @@ class GameIntroductionActivity :
     private var isEnterGame = false
     private var gameCode: String = ""
     private var playClass: String = ""
-    private var gameInbetweenImage: String = ""
-    private var gameBackgroundImage: String = ""
     private var gameBean: GameBean? = null
 
-    override fun onCreate(savedInstanceState: Bundle?) {
+    /**
+     * 语音合成
+     */
+    private var voicePlayer: VoicePlayer? = null
+
+    override fun initViews() {
         desn = intent.getStringExtra("desn") ?: ""
         gameCode = intent.getStringExtra("gameCode") ?: ""
         playClass = intent.getStringExtra("playClass") ?: ""
-        super.onCreate(savedInstanceState)
-    }
-
-    override fun initViews() {
-        ImgUtil.loadGameBackground(mContext, gameInbetweenImage, binding.imageBg)
+        voicePlayer = VoicePlayer.getInstance(mContext)
         if (isEnterGame.not()) {
             val gameLoadingDialog = GameLoadingDialog()
             gameLoadingDialog.onDialogClickListener = {
@@ -91,7 +95,11 @@ class GameIntroductionActivity :
 
     override fun onPause() {
         binding.playTxCloudView.onPause()
-        LiveEventBusUtil.send(RxBusCodes.STOPVOICE, "")
+        if (voicePlayer != null) {
+            if (voicePlayer!!.isPlaying) {
+                voicePlayer?.stop()
+            }
+        }
         super.onPause()
     }
 
@@ -106,8 +114,7 @@ class GameIntroductionActivity :
                 gameBean = it
                 binding.data = it
                 desn = it.desn
-                gameInbetweenImage = it.gameInbetweenImage
-                gameBackgroundImage = it.gameBackgroundImage
+                ImgUtil.loadGameBackground(mContext, it.gameInbetweenImage, binding.imageBg)
                 if (TextUtils.isEmpty(it.gameVideoUrl).not()) {
                     loadVideo(it.gameVideoUrl)
                 }
@@ -180,8 +187,8 @@ class GameIntroductionActivity :
                             .withString("url", url)
                             .withString("gameType", gameBean!!.gameType)
                             .withString("gameTotalScore", gameBean!!.gameTotalScore)
-                            .withString("gameInbetweenImage", gameInbetweenImage)
-                            .withString("gameBackgroundImage", gameBackgroundImage)
+                            .withString("gameInbetweenImage", gameBean!!.gameInbetweenImage)
+                            .withString("gameBackgroundImage", gameBean!!.gameBackgroundImage)
                             .withString("gameShortDesn", gameBean!!.gameShortDesn)
                             .withString("fullFlag", gameBean!!.fullFlag)
                             .withString("frameImg", gameBean!!.frameImg)
@@ -203,7 +210,52 @@ class GameIntroductionActivity :
      * 语音合成
      */
     private fun speak() {
-        LiveEventBusUtil.send(RxBusCodes.SPEECHSYNTHESIS, desn)
+        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) {
+                        }
+                    }
+                }
+            }
+        })
     }
 
     /**
@@ -242,4 +294,13 @@ 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
+    }
 }