Преглед на файлове

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

王鹏鹏 преди 2 години
родител
ревизия
d8b38ac02e
променени са 1 файла, в които са добавени 76 реда и са изтрити 8 реда
  1. 76 8
      game/src/main/java/com/yingyangfly/game/play/PlayGameActivity.kt

+ 76 - 8
game/src/main/java/com/yingyangfly/game/play/PlayGameActivity.kt

@@ -1,9 +1,11 @@
 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
 import android.text.TextUtils
 import android.util.Log
 import android.view.MotionEvent
@@ -21,8 +23,10 @@ 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,9 +75,10 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
     private var frameImg: String = ""
     private var mWebViewHeight = 0
     private var mWebViewWidth = 0
+    private var voicePlayer: VoicePlayer? = null
     private var mConstrainSet_one: ConstraintSet? = ConstraintSet()
 
-    override fun onCreate(savedInstanceState: Bundle?) {
+    override fun initViews() {
         gameCode = intent.getStringExtra("gameCode") ?: ""
         playClass = intent.getStringExtra("playClass") ?: ""
         url = intent.getStringExtra("url") ?: ""
@@ -84,10 +89,7 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
         gameShortDesn = intent.getStringExtra("gameShortDesn") ?: ""
         fullFlag = intent.getStringExtra("fullFlag") ?: ""
         frameImg = intent.getStringExtra("frameImg") ?: ""
-        super.onCreate(savedInstanceState)
-    }
-
-    override fun initViews() {
+        voicePlayer = VoicePlayer.getInstance(mContext)
         if (TextUtils.isEmpty(url).not()) {
             initWebView(url)
         }
@@ -314,7 +316,11 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
 
     override fun onPause() {
         super.onPause()
-        LiveEventBusUtil.send(RxBusCodes.STOPVOICE, "")
+        if (voicePlayer != null) {
+            if (voicePlayer!!.isPlaying) {
+                voicePlayer?.stop()
+            }
+        }
     }
 
     /**
@@ -449,7 +455,59 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
      * 语音合成
      */
     private fun speak(desn: String) {
-        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) {
+                        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) {
+                            binding.tvIntroduce.text = gameShortDesn
+                            callPlayBgMusic()
+                        }
+                    }
+                }
+            }
+        })
     }
 
     /**
@@ -564,4 +622,14 @@ 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
+    }
 }