Browse Source

1.优化进入游戏页面倒计时音效从接口获取

王鹏鹏 1 year ago
parent
commit
0abba4730b

+ 3 - 4
baselib/src/main/java/com/yingyangfly/baselib/db/GameSoundBean.java

@@ -14,19 +14,18 @@ public class GameSoundBean {
 
     @NotNull
     @PrimaryKey
-    private int id;//待合成词语
+    private long id;//待合成词语
 
     private String voiceUrl;//待合成词语
     private String voiceType;//合成连接
 
-    public int getId() {
+    public long getId() {
         return id;
     }
 
-    public void setId(int id) {
+    public void setId(long id) {
         this.id = id;
     }
-
     public String getVoiceUrl() {
         return voiceUrl;
     }

+ 33 - 25
game/src/main/java/com/yingyangfly/game/dialog/CountdownDialog.kt

@@ -1,10 +1,8 @@
 package com.yingyangfly.game.dialog
 
 import android.content.Context
-import android.media.AudioAttributes
-import android.media.AudioManager
-import android.media.SoundPool
 import android.os.Bundle
+import android.text.TextUtils
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
@@ -14,6 +12,7 @@ import androidx.fragment.app.DialogFragment
 import com.bumptech.glide.Glide
 import com.gyf.immersionbar.BarHide
 import com.gyf.immersionbar.ktx.immersionBar
+import com.yingyangfly.baselib.player.VoicePlayer
 import com.yingyangfly.baselib.utils.LiveEventBusUtil
 import com.yingyangfly.baselib.utils.RxBusCodes
 import com.yingyangfly.baselib.utils.RxTimer
@@ -29,6 +28,9 @@ class CountdownDialog : DialogFragment() {
     private lateinit var rxTimer: RxTimer
     private var url = ""
     private var title: String = ""
+    private var oneStep = ""
+    private var twoStep = ""
+    private var threeStep = ""
     private var gameImage: AppCompatImageView? = null
     private var imageCountdown: AppCompatImageView? = null
     var onDialogClickListener: ((bean: String) -> Unit)? = null
@@ -36,8 +38,7 @@ class CountdownDialog : DialogFragment() {
     private var tvTitle: AppCompatTextView? = null
     private var loadingOver = false
     private var content: Context? = null
-    private var soundPool: SoundPool? = null
-    private val soundIdArr = IntArray(3)
+    private var voicePlayer: VoicePlayer? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -45,9 +46,14 @@ class CountdownDialog : DialogFragment() {
         dialog?.setCanceledOnTouchOutside(true)
     }
 
-    fun setContentBackground(url: String, title: String) {
+    fun setContentBackground(
+        url: String, title: String, oneStep: String, twoStep: String, threeStep: String
+    ) {
         this.url = url
         this.title = title
+        this.oneStep = oneStep
+        this.twoStep = twoStep
+        this.threeStep = threeStep
     }
 
     override fun onStart() {
@@ -69,16 +75,7 @@ class CountdownDialog : DialogFragment() {
             activity, R.layout.dialog_count_down, container, 2388, 1668
         )
         content = activity
-        // 5.0 及 之后
-        soundPool =
-            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
-                val audioAttributes: AudioAttributes =
-                    AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA)
-                        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build()
-                SoundPool.Builder().setMaxStreams(3).setAudioAttributes(audioAttributes).build()
-            } else { // 5.0 以前
-                SoundPool(3, AudioManager.STREAM_MUSIC, 0)  // 创建SoundPool
-            }
+        voicePlayer = VoicePlayer.getInstance(content)
         findId(rootView)
         initData()
         initTimer()
@@ -96,9 +93,6 @@ class CountdownDialog : DialogFragment() {
 
     private fun initData() {
         Glide.with(requireActivity()).asGif().load(R.drawable.fish).into(loadingImage!!)
-        soundIdArr[0] = soundPool!!.load(content, R.raw.one, 1)
-        soundIdArr[1] = soundPool!!.load(content, R.raw.two, 1)
-        soundIdArr[2] = soundPool!!.load(content, R.raw.three, 1)
     }
 
     /**
@@ -114,17 +108,31 @@ class CountdownDialog : DialogFragment() {
             when (date.toString()) {
                 "1" -> {
                     imageCountdown!!.setBackgroundResource(R.mipmap.icon_one)
-                    soundPool!!.play(soundIdArr[0], 1f, 1f, 1, 0, 1f)
+                    if (voicePlayer != null && TextUtils.isEmpty(oneStep).not()) {
+                        imageCountdown!!.post {
+                            voicePlayer?.play(oneStep) {}
+                        }
+                    }
                 }
 
                 "2" -> {
                     imageCountdown!!.setBackgroundResource(R.mipmap.icon_two)
-                    soundPool!!.play(soundIdArr[1], 1f, 1f, 1, 0, 1f)
+                    if (voicePlayer != null && TextUtils.isEmpty(twoStep).not()) {
+                        imageCountdown!!.post {
+                            voicePlayer?.stop()
+                            voicePlayer?.play(twoStep) {}
+                        }
+                    }
                 }
 
                 "3" -> {
                     imageCountdown!!.setBackgroundResource(R.mipmap.icon_three)
-                    soundPool!!.play(soundIdArr[2], 1f, 1f, 1, 0, 1f)
+                    if (voicePlayer != null && TextUtils.isEmpty(threeStep).not()) {
+                        imageCountdown!!.post {
+                            voicePlayer?.stop()
+                            voicePlayer?.play(threeStep) {}
+                        }
+                    }
                 }
             }
             if (it == time) {
@@ -151,9 +159,9 @@ class CountdownDialog : DialogFragment() {
     }
 
     override fun dismiss() {
-        if (soundPool != null) {
-            soundPool?.release()
-        }
         super.dismiss()
+        if (voicePlayer != null) {
+            voicePlayer?.stop()
+        }
     }
 }

+ 16 - 1
game/src/main/java/com/yingyangfly/game/play/PlayGameActivity.kt

@@ -98,6 +98,9 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
     private var gameSuccess = ""
     private var gameFailed = ""
     private var gameTimeOut = ""
+    private var oneStep = ""
+    private var twoStep = ""
+    private var threeStep = ""
 
     override fun initViews() {
         id = intent.getStringExtra("id") ?: ""
@@ -338,6 +341,18 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
                     gameSoundbeans?.forEach {
                         if (TextUtils.isEmpty(it.voiceType).not()) {
                             when (it.voiceType) {
+                                "one" -> {
+                                    oneStep = it.voiceUrl
+                                }
+
+                                "two" -> {
+                                    twoStep = it.voiceUrl
+                                }
+
+                                "three" -> {
+                                    threeStep = it.voiceUrl
+                                }
+
                                 "fail" -> {
                                     gameFailed = it.voiceUrl
                                 }
@@ -356,7 +371,7 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
             }
             val countdownDialog = CountdownDialog()
             countdownDialog.setContentBackground(
-                gameInbetweenImage, gameName
+                gameInbetweenImage, gameName, oneStep, twoStep, threeStep
             )
             countdownDialog.onDialogClickListener = {
                 countdownSuccess = true