diff --git a/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt b/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt index cbfca1e..8eeb2bc 100644 --- a/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt @@ -8,7 +8,6 @@ import android.os.Message import android.view.animation.Animation import android.view.animation.RotateAnimation -import androidx.lifecycle.lifecycleScope import com.casic.detector.R import com.casic.detector.base.SerialPortActivity import com.casic.detector.extensions.hexToString @@ -24,9 +23,6 @@ import com.pengxh.kt.lite.utils.WeakReferenceHandler import com.pengxh.kt.lite.widget.dialog.AlertMessageDialog import kotlinx.android.synthetic.main.activity_search_label.* -import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.flow -import kotlinx.coroutines.launch import java.io.IOException import java.text.DecimalFormat import java.util.* @@ -35,8 +31,9 @@ private val kTag = "SearchLabelActivity" private lateinit var soundPool: SoundPool + private lateinit var mediumSoundPool: SoundPool + private lateinit var fastSoundPool: SoundPool private var soundResId = 0 - private var slowResId = 0 private var mediumResId = 0 private var fastResId = 0 private val gpioManager by lazy { GpioManager() } @@ -46,7 +43,6 @@ private var isDetectDepth = false private var identifierId = "" private val lastDegreeMap by lazy { HashMap() } - private var energy = 0 override fun initData(savedInstanceState: Bundle?) { gpioManager.setGpioHigh("18") @@ -77,42 +73,17 @@ } //能量值音频 - slowResId = soundPool.load(this, R.raw.ring3, 1) - mediumResId = soundPool.load(this, R.raw.ring4, 1) - fastResId = soundPool.load(this, R.raw.ring2, 1) + mediumSoundPool = SoundPool.Builder() + .setMaxStreams(16) + .setAudioAttributes(audioAttributes) + .build() + mediumResId = mediumSoundPool.load(this, R.raw.ring4, 1) - //TODO 需要根据能量值大小播放不同的音频 - if (energy <= 20) { - lifecycleScope.launch { - flow { - val playResult = soundPool.play(slowResId, 1f, 1f, 0, 0, 1f) - delay(100) - emit(playResult) - }.collect { - soundPool.stop(slowResId) - } - } - } else if (energy >= 80) { - lifecycleScope.launch { - flow { - val playResult = soundPool.play(fastResId, 1f, 1f, 0, 0, 1f) - delay(100) - emit(playResult) - }.collect { - soundPool.stop(fastResId) - } - } - } else { - lifecycleScope.launch { - flow { - val playResult = soundPool.play(mediumResId, 1f, 1f, 0, 0, 1f) - delay(100) - emit(playResult) - }.collect { - soundPool.stop(mediumResId) - } - } - } + fastSoundPool = SoundPool.Builder() + .setMaxStreams(16) + .setAudioAttributes(audioAttributes) + .build() + fastResId = fastSoundPool.load(this, R.raw.ring2, 1) //角度 lastDegreeMap["lastDegree"] = 0f @@ -222,12 +193,41 @@ //用能量值转动表盘 val energyResponse = hex.substring(0, 10).hexToString() //去掉前缀并缩小100倍 - energy = energyResponse.substring(1).toInt() / 100 + var energy = energyResponse.substring(1).toInt() / 100 if (energy >= 50) { //测试过程最大值(5189)52 energy = 50 } + if (energy > 40) { + isPlaying = if (!isPlaying) { + fastSoundPool.play(fastResId, 1f, 1f, 0, 0, 1f) + true + } else { + fastSoundPool.stop(fastResId) + false + } + } else { + isPlaying = if (!isPlaying) { + mediumSoundPool.play(mediumResId, 1f, 1f, 0, 0, 1f) + true + } else { + mediumSoundPool.stop(mediumResId) + false + } + } + + if (energy < 10) { + energyTipsView.text = "标识器信号较弱,可能距离较远" + energyTipsView.setTextColor(Color.RED) + } else if (energy > 40) { + energyTipsView.text = "标识器信号极强,可能就在附近" + energyTipsView.setTextColor(Color.GREEN) + } else { + energyTipsView.text = "标识器信号不稳定,请继续移动位置" + energyTipsView.setTextColor(Color.YELLOW) + } + //转换为转动的角度 val degree = (energy.toFloat() / 50) * 180 energyValueView.text = decimal.format(degree) @@ -282,6 +282,8 @@ return true } + private var isPlaying = false + override fun initLayoutView(): Int = R.layout.activity_search_label override fun observeRequestState() { diff --git a/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt b/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt index cbfca1e..8eeb2bc 100644 --- a/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt @@ -8,7 +8,6 @@ import android.os.Message import android.view.animation.Animation import android.view.animation.RotateAnimation -import androidx.lifecycle.lifecycleScope import com.casic.detector.R import com.casic.detector.base.SerialPortActivity import com.casic.detector.extensions.hexToString @@ -24,9 +23,6 @@ import com.pengxh.kt.lite.utils.WeakReferenceHandler import com.pengxh.kt.lite.widget.dialog.AlertMessageDialog import kotlinx.android.synthetic.main.activity_search_label.* -import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.flow -import kotlinx.coroutines.launch import java.io.IOException import java.text.DecimalFormat import java.util.* @@ -35,8 +31,9 @@ private val kTag = "SearchLabelActivity" private lateinit var soundPool: SoundPool + private lateinit var mediumSoundPool: SoundPool + private lateinit var fastSoundPool: SoundPool private var soundResId = 0 - private var slowResId = 0 private var mediumResId = 0 private var fastResId = 0 private val gpioManager by lazy { GpioManager() } @@ -46,7 +43,6 @@ private var isDetectDepth = false private var identifierId = "" private val lastDegreeMap by lazy { HashMap() } - private var energy = 0 override fun initData(savedInstanceState: Bundle?) { gpioManager.setGpioHigh("18") @@ -77,42 +73,17 @@ } //能量值音频 - slowResId = soundPool.load(this, R.raw.ring3, 1) - mediumResId = soundPool.load(this, R.raw.ring4, 1) - fastResId = soundPool.load(this, R.raw.ring2, 1) + mediumSoundPool = SoundPool.Builder() + .setMaxStreams(16) + .setAudioAttributes(audioAttributes) + .build() + mediumResId = mediumSoundPool.load(this, R.raw.ring4, 1) - //TODO 需要根据能量值大小播放不同的音频 - if (energy <= 20) { - lifecycleScope.launch { - flow { - val playResult = soundPool.play(slowResId, 1f, 1f, 0, 0, 1f) - delay(100) - emit(playResult) - }.collect { - soundPool.stop(slowResId) - } - } - } else if (energy >= 80) { - lifecycleScope.launch { - flow { - val playResult = soundPool.play(fastResId, 1f, 1f, 0, 0, 1f) - delay(100) - emit(playResult) - }.collect { - soundPool.stop(fastResId) - } - } - } else { - lifecycleScope.launch { - flow { - val playResult = soundPool.play(mediumResId, 1f, 1f, 0, 0, 1f) - delay(100) - emit(playResult) - }.collect { - soundPool.stop(mediumResId) - } - } - } + fastSoundPool = SoundPool.Builder() + .setMaxStreams(16) + .setAudioAttributes(audioAttributes) + .build() + fastResId = fastSoundPool.load(this, R.raw.ring2, 1) //角度 lastDegreeMap["lastDegree"] = 0f @@ -222,12 +193,41 @@ //用能量值转动表盘 val energyResponse = hex.substring(0, 10).hexToString() //去掉前缀并缩小100倍 - energy = energyResponse.substring(1).toInt() / 100 + var energy = energyResponse.substring(1).toInt() / 100 if (energy >= 50) { //测试过程最大值(5189)52 energy = 50 } + if (energy > 40) { + isPlaying = if (!isPlaying) { + fastSoundPool.play(fastResId, 1f, 1f, 0, 0, 1f) + true + } else { + fastSoundPool.stop(fastResId) + false + } + } else { + isPlaying = if (!isPlaying) { + mediumSoundPool.play(mediumResId, 1f, 1f, 0, 0, 1f) + true + } else { + mediumSoundPool.stop(mediumResId) + false + } + } + + if (energy < 10) { + energyTipsView.text = "标识器信号较弱,可能距离较远" + energyTipsView.setTextColor(Color.RED) + } else if (energy > 40) { + energyTipsView.text = "标识器信号极强,可能就在附近" + energyTipsView.setTextColor(Color.GREEN) + } else { + energyTipsView.text = "标识器信号不稳定,请继续移动位置" + energyTipsView.setTextColor(Color.YELLOW) + } + //转换为转动的角度 val degree = (energy.toFloat() / 50) * 180 energyValueView.text = decimal.format(degree) @@ -282,6 +282,8 @@ return true } + private var isPlaying = false + override fun initLayoutView(): Int = R.layout.activity_search_label override fun observeRequestState() { diff --git a/app/src/main/res/layout/activity_search_label.xml b/app/src/main/res/layout/activity_search_label.xml index 53ddb46..7edacf9 100644 --- a/app/src/main/res/layout/activity_search_label.xml +++ b/app/src/main/res/layout/activity_search_label.xml @@ -62,6 +62,15 @@ android:textSize="40sp" /> + +