|
|
@@ -1,6 +1,9 @@
|
|
|
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.view.LayoutInflater
|
|
|
import android.view.View
|
|
|
@@ -11,7 +14,6 @@ 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
|
|
|
@@ -34,10 +36,8 @@ class CountdownDialog : DialogFragment() {
|
|
|
private var tvTitle: AppCompatTextView? = null
|
|
|
private var loadingOver = false
|
|
|
private var content: Context? = null
|
|
|
- private var oneStep = ""
|
|
|
- private var twoStep = ""
|
|
|
- private var threeStep = ""
|
|
|
- private var voicePlayer: VoicePlayer? = null
|
|
|
+ private var soundPool: SoundPool? = null
|
|
|
+ private val soundIdArr = IntArray(3)
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
super.onCreate(savedInstanceState)
|
|
|
@@ -45,33 +45,21 @@ class CountdownDialog : DialogFragment() {
|
|
|
dialog?.setCanceledOnTouchOutside(true)
|
|
|
}
|
|
|
|
|
|
- fun setContentBackground(
|
|
|
- url: String,
|
|
|
- title: String,
|
|
|
- oneStep: String,
|
|
|
- twoStep: String,
|
|
|
- threeStep: String
|
|
|
- ) {
|
|
|
+ fun setContentBackground(url: String, title: String) {
|
|
|
this.url = url
|
|
|
this.title = title
|
|
|
- this.oneStep = oneStep
|
|
|
- this.twoStep = twoStep
|
|
|
- this.threeStep = threeStep
|
|
|
}
|
|
|
|
|
|
override fun onStart() {
|
|
|
super.onStart()
|
|
|
dialog?.window?.setLayout(
|
|
|
- ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
- ViewGroup.LayoutParams.MATCH_PARENT
|
|
|
+ ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
|
|
|
)
|
|
|
dialog?.window?.setBackgroundDrawableResource(R.color.transparent)
|
|
|
}
|
|
|
|
|
|
override fun onCreateView(
|
|
|
- inflater: LayoutInflater,
|
|
|
- container: ViewGroup?,
|
|
|
- savedInstanceState: Bundle?
|
|
|
+ inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
|
|
|
): View? {
|
|
|
immersionBar {
|
|
|
hideBar(BarHide.FLAG_HIDE_BAR)
|
|
|
@@ -81,7 +69,16 @@ class CountdownDialog : DialogFragment() {
|
|
|
activity, R.layout.dialog_count_down, container, 2388, 1668
|
|
|
)
|
|
|
content = activity
|
|
|
- voicePlayer = VoicePlayer.getInstance(content)
|
|
|
+ // 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
|
|
|
+ }
|
|
|
findId(rootView)
|
|
|
initData()
|
|
|
initTimer()
|
|
|
@@ -98,10 +95,10 @@ class CountdownDialog : DialogFragment() {
|
|
|
}
|
|
|
|
|
|
private fun initData() {
|
|
|
- Glide.with(requireActivity())
|
|
|
- .asGif()
|
|
|
- .load(R.drawable.fish)
|
|
|
- .into(loadingImage!!)
|
|
|
+ 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)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -117,31 +114,17 @@ class CountdownDialog : DialogFragment() {
|
|
|
when (date.toString()) {
|
|
|
"1" -> {
|
|
|
imageCountdown!!.setBackgroundResource(R.mipmap.icon_one)
|
|
|
- imageCountdown!!.post {
|
|
|
- if (voicePlayer != null) {
|
|
|
- voicePlayer?.play(oneStep) {
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
+ soundPool!!.play(soundIdArr[0], 1f, 1f, 1, 0, 1f)
|
|
|
}
|
|
|
+
|
|
|
"2" -> {
|
|
|
imageCountdown!!.setBackgroundResource(R.mipmap.icon_two)
|
|
|
- imageCountdown!!.post {
|
|
|
- if (voicePlayer != null) {
|
|
|
- voicePlayer?.play(twoStep) {
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ soundPool!!.play(soundIdArr[1], 1f, 1f, 1, 0, 1f)
|
|
|
}
|
|
|
+
|
|
|
"3" -> {
|
|
|
imageCountdown!!.setBackgroundResource(R.mipmap.icon_three)
|
|
|
- imageCountdown!!.post {
|
|
|
- if (voicePlayer != null) {
|
|
|
- voicePlayer?.play(threeStep) {
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ soundPool!!.play(soundIdArr[2], 1f, 1f, 1, 0, 1f)
|
|
|
}
|
|
|
}
|
|
|
if (it == time) {
|
|
|
@@ -162,15 +145,14 @@ class CountdownDialog : DialogFragment() {
|
|
|
}
|
|
|
|
|
|
//游戏加载完成
|
|
|
- LiveEventBusUtil.observer<String>(this, RxBusCodes.LOADINGOVER)
|
|
|
- {
|
|
|
+ LiveEventBusUtil.observer<String>(this, RxBusCodes.LOADINGOVER) {
|
|
|
loadingOver = true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
override fun dismiss() {
|
|
|
- if (voicePlayer != null) {
|
|
|
- voicePlayer?.stop()
|
|
|
+ if (soundPool != null) {
|
|
|
+ soundPool?.release()
|
|
|
}
|
|
|
super.dismiss()
|
|
|
}
|