Browse Source

1.修改离开当前页面语音还在播放bug

王鹏鹏 2 years ago
parent
commit
3e74a93df8

+ 17 - 3
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
@@ -30,6 +33,7 @@ import gorden.rxbus2.RxBus
 import gorden.rxbus2.Subscribe
 import gorden.rxbus2.ThreadMode
 
+
 /**
  * 游戏介绍
  */
@@ -240,7 +244,6 @@ class GameIntroductionActivity :
      */
     private fun getVoiceUrl(taskDesn: String) {
         viewModel.getVoiceUrl(taskDesn, fail = {
-
         }, success = {
             if (TextUtils.isEmpty(it).not()) {
                 if (db != null) {
@@ -254,8 +257,9 @@ class GameIntroductionActivity :
                     }
                 }
                 if (voicePlayer != null) {
-                    voicePlayer?.play(it) {
-
+                    if (isTopActivity(this)) {
+                        voicePlayer?.play(it) {
+                        }
                     }
                 }
             }
@@ -286,4 +290,14 @@ 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
+    }
+
 }