diff --git a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt index c6f6879..6b777cb 100644 --- a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt @@ -4,7 +4,7 @@ import android.app.DatePickerDialog import android.content.Context import android.content.Intent -import android.media.AudioManager +import android.media.AudioAttributes import android.media.SoundPool import android.os.Bundle import android.os.Handler @@ -60,6 +60,7 @@ private var colorSelectedItem = "" private lateinit var taskViewModel: TaskViewModel private lateinit var soundPool: SoundPool + private var soundResId = 0 private val gpioManager by lazy { GpioManager() } private lateinit var weakReferenceHandler: WeakReferenceHandler @@ -84,8 +85,14 @@ weakReferenceHandler = WeakReferenceHandler(this) - soundPool = SoundPool(10, AudioManager.STREAM_SYSTEM, 5) - soundPool.load(this, R.raw.ring4, 1) + val audioAttributes = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_MEDIA) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) + .build() + soundPool = SoundPool.Builder() + .setMaxStreams(16) + .setAudioAttributes(audioAttributes) + .build() } override fun initEvent() { @@ -347,6 +354,11 @@ LoadingDialogHub.show(this, "标识器读取中,请稍后...") + soundResId = soundPool.load(this, R.raw.ling, 1) + soundPool.setOnLoadCompleteListener { soundPool, _, _ -> + soundPool.play(soundResId, 1f, 1f, 0, -1, 1f) + } + // 1000ms后发送读标识器或搜索信号 weakReferenceHandler.postDelayed({ try { @@ -361,7 +373,6 @@ } override fun onDataReceived(buffer: ByteArray?, size: Int) { - //[48, 48, 48, 56, 57, 56, 55, 53, 49, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Log.d(kTag, "onDataReceived => " + buffer.contentToString()) if (buffer != null) { val message = weakReferenceHandler.obtainMessage() @@ -373,12 +384,12 @@ override fun handleMessage(msg: Message): Boolean { if (msg.what == 2023061601) { + soundPool.stop(soundResId) + val buffer = msg.obj as ByteArray val hex = buffer.toHex() - //30303038393837353134000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + //3030303839383735313400000000000000000000000000000000000000000000 if (hex.length >= 20) { - soundPool.play(1, 1f, 1f, 0, 0, 1f) - val identifierId = hex.substring(0, 20).hexToString() gpioManager.setGpioLow("18") @@ -390,7 +401,6 @@ } else { "此标识器已安装,请更换标识器!".show(this) } - LoadingDialogHub.dismiss() } } @@ -491,6 +501,7 @@ } override fun onDestroy() { + soundPool.stop(soundResId) gpioManager.setGpioLow("18") super.onDestroy() } diff --git a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt index c6f6879..6b777cb 100644 --- a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt @@ -4,7 +4,7 @@ import android.app.DatePickerDialog import android.content.Context import android.content.Intent -import android.media.AudioManager +import android.media.AudioAttributes import android.media.SoundPool import android.os.Bundle import android.os.Handler @@ -60,6 +60,7 @@ private var colorSelectedItem = "" private lateinit var taskViewModel: TaskViewModel private lateinit var soundPool: SoundPool + private var soundResId = 0 private val gpioManager by lazy { GpioManager() } private lateinit var weakReferenceHandler: WeakReferenceHandler @@ -84,8 +85,14 @@ weakReferenceHandler = WeakReferenceHandler(this) - soundPool = SoundPool(10, AudioManager.STREAM_SYSTEM, 5) - soundPool.load(this, R.raw.ring4, 1) + val audioAttributes = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_MEDIA) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) + .build() + soundPool = SoundPool.Builder() + .setMaxStreams(16) + .setAudioAttributes(audioAttributes) + .build() } override fun initEvent() { @@ -347,6 +354,11 @@ LoadingDialogHub.show(this, "标识器读取中,请稍后...") + soundResId = soundPool.load(this, R.raw.ling, 1) + soundPool.setOnLoadCompleteListener { soundPool, _, _ -> + soundPool.play(soundResId, 1f, 1f, 0, -1, 1f) + } + // 1000ms后发送读标识器或搜索信号 weakReferenceHandler.postDelayed({ try { @@ -361,7 +373,6 @@ } override fun onDataReceived(buffer: ByteArray?, size: Int) { - //[48, 48, 48, 56, 57, 56, 55, 53, 49, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Log.d(kTag, "onDataReceived => " + buffer.contentToString()) if (buffer != null) { val message = weakReferenceHandler.obtainMessage() @@ -373,12 +384,12 @@ override fun handleMessage(msg: Message): Boolean { if (msg.what == 2023061601) { + soundPool.stop(soundResId) + val buffer = msg.obj as ByteArray val hex = buffer.toHex() - //30303038393837353134000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + //3030303839383735313400000000000000000000000000000000000000000000 if (hex.length >= 20) { - soundPool.play(1, 1f, 1f, 0, 0, 1f) - val identifierId = hex.substring(0, 20).hexToString() gpioManager.setGpioLow("18") @@ -390,7 +401,6 @@ } else { "此标识器已安装,请更换标识器!".show(this) } - LoadingDialogHub.dismiss() } } @@ -491,6 +501,7 @@ } override fun onDestroy() { + soundPool.stop(soundResId) gpioManager.setGpioLow("18") super.onDestroy() } 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 0531dda..cbfca1e 100644 --- a/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt @@ -1,13 +1,14 @@ package com.casic.detector.view import android.graphics.Color -import android.media.AudioManager +import android.media.AudioAttributes import android.media.SoundPool import android.os.Bundle import android.os.Handler 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 @@ -23,6 +24,9 @@ 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.* @@ -31,20 +35,87 @@ private val kTag = "SearchLabelActivity" private lateinit var soundPool: SoundPool + private var soundResId = 0 + private var slowResId = 0 + private var mediumResId = 0 + private var fastResId = 0 private val gpioManager by lazy { GpioManager() } private lateinit var weakReferenceHandler: WeakReferenceHandler private val timer by lazy { Timer() } private val decimal by lazy { DecimalFormat("#.0") } 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") weakReferenceHandler = WeakReferenceHandler(this) - soundPool = SoundPool(10, AudioManager.STREAM_SYSTEM, 5) - soundPool.load(this, R.raw.ring2, 1) + /** + * maxStreams:最大同时支持音频播放数; + * streamType:音频流类型,一般为AudioManager.STREAM_MUSIC; + * srcQuality:采样的转换的质量,目前没有作用,以0为默认值。 + * 在版本为21以上时,推荐使用Builder去构建SoundPool,默认的构造方法已经过时。 + * */ + val audioAttributes = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_MEDIA) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) + .build() + soundPool = SoundPool.Builder() + .setMaxStreams(16) + .setAudioAttributes(audioAttributes) + .build() + /** + * 声音效果速度 + * ling + soundPool.play(soundResId, 1f, 1f, 0, -1, 1f) + } + + //能量值音频 + slowResId = soundPool.load(this, R.raw.ring3, 1) + mediumResId = soundPool.load(this, R.raw.ring4, 1) + fastResId = soundPool.load(this, R.raw.ring2, 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) + } + } + } + + //角度 + lastDegreeMap["lastDegree"] = 0f //搜索信号 timer.schedule(object : TimerTask() { @@ -151,23 +222,27 @@ //用能量值转动表盘 val energyResponse = hex.substring(0, 10).hexToString() //去掉前缀并缩小100倍 - var energy = energyResponse.substring(1).toInt() / 100 + energy = energyResponse.substring(1).toInt() / 100 if (energy >= 50) { //测试过程最大值(5189)52 energy = 50 } + //转换为转动的角度 val degree = (energy.toFloat() / 50) * 180 energyValueView.text = decimal.format(degree) val animation = RotateAnimation( - 0f, degree, + lastDegreeMap["lastDegree"]!!, degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f ) animation.duration = 0 animation.fillAfter = true needleView.startAnimation(animation) + + //保存旋转后的角度 + lastDegreeMap["lastDegree"] = degree } catch (e: NumberFormatException) { e.printStackTrace() } @@ -199,6 +274,8 @@ searchResultView.setTextColor(Color.RED) depthButton.isEnabled = true degreeButton.isEnabled = true + + soundPool.stop(soundResId) } } } @@ -217,6 +294,7 @@ } override fun onDestroy() { + soundPool.stop(soundResId) gpioManager.setGpioLow("18") super.onDestroy() }