desc:气体浓度达到高低报警值时需要和声光报警器联动报警;且高报和低报报警频率区分。
parent
a4b059762c
commit
ebda6d1a5b
@ -1,60 +1,94 @@
|
||||
package com.yinuo.safetywatcher.watcher.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.media.AudioAttributes
|
||||
import android.media.SoundPool
|
||||
import android.os.Handler
|
||||
import android.os.HandlerThread
|
||||
import com.common.commonlib.utils.LogUtils
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
object SoundUtils {
|
||||
|
||||
private val soundPool by lazy {
|
||||
// 创建 SoundPool
|
||||
val audioAttributes = AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||
.build()
|
||||
SoundPool.Builder()
|
||||
.setAudioAttributes(audioAttributes)
|
||||
.setMaxStreams(1) // 指定最大音效流的数量
|
||||
.build()
|
||||
}
|
||||
private const val TYPE_SOS = 0
|
||||
|
||||
private var soundId = -1
|
||||
private const val TYPE_WARN_HIGHT = 1
|
||||
private const val TYPE_WARN_LOW = 2
|
||||
private val soundMap = hashMapOf<Int, Boolean>(
|
||||
TYPE_SOS to false,
|
||||
TYPE_WARN_LOW to false,
|
||||
TYPE_WARN_HIGHT to false
|
||||
)
|
||||
private var soundPlaying = false
|
||||
private var currentPlayingDelay = 500
|
||||
private var soundThread: HandlerThread? = null
|
||||
private var soundHandler: Handler? = null
|
||||
var gpioValue = 0
|
||||
|
||||
fun init(context: Context, resID: Int) {
|
||||
soundId = soundPool.load(context, resID, 1); // 替换为你的音效资源文件
|
||||
init {
|
||||
GPIOUtils.setGpioDirection(140, "out")
|
||||
soundThread = HandlerThread("sound")
|
||||
soundThread!!.start()
|
||||
soundHandler = Handler(soundThread!!.looper)
|
||||
}
|
||||
|
||||
fun playSound() {
|
||||
// 播放音效
|
||||
soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f); // 参数依次为音效ID、左声道音量、右声道音量、优先级、循环次数、播放速度
|
||||
fun warnSound(isHigh: Boolean) {
|
||||
playSoundIo(if (isHigh) TYPE_WARN_HIGHT else TYPE_WARN_LOW)
|
||||
}
|
||||
|
||||
var soundPlaying = false
|
||||
fun warnSoundClose() {
|
||||
stopSoundIo(TYPE_WARN_HIGHT)
|
||||
stopSoundIo(TYPE_WARN_LOW)
|
||||
}
|
||||
|
||||
fun playSoundIo() {
|
||||
if (soundPlaying) {
|
||||
fun playSoundIo(type: Int = TYPE_SOS) {
|
||||
if (soundMap[type] == true && soundPlaying) {
|
||||
return
|
||||
}
|
||||
GPIOUtils.setGpioDirection(140, "out")
|
||||
var gpioValue = 0
|
||||
soundPlaying = true
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
while (soundPlaying) {
|
||||
gpioValue = if (gpioValue > 0) 0 else 1
|
||||
LogUtils.w("cyy gpioValue ${gpioValue}")
|
||||
GPIOUtils.setGpioValue(140, gpioValue)
|
||||
delay(500)
|
||||
soundMap[type] = true
|
||||
val delayTime = getDelayTime(soundMap)
|
||||
// 不需要变化时,直接返回
|
||||
if (soundPlaying && delayTime == currentPlayingDelay) {
|
||||
return
|
||||
}
|
||||
if (!soundPlaying) {
|
||||
currentPlayingDelay = delayTime
|
||||
soundHandler?.post {
|
||||
setVoiceIo()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun stopSoundIo() {
|
||||
soundPlaying = false
|
||||
private fun getDelayTime(soundMap: HashMap<Int, Boolean>): Int {
|
||||
if (soundMap[TYPE_SOS] == true || soundMap[TYPE_WARN_HIGHT] == true) {
|
||||
return 500
|
||||
} else if (soundMap[TYPE_WARN_LOW] == true) {
|
||||
return 700
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
private fun setVoiceIo() {
|
||||
// 在启动新的
|
||||
soundPlaying = true
|
||||
while (soundPlaying) {
|
||||
gpioValue = if (gpioValue > 0) 0 else 1
|
||||
LogUtils.w("cyy gpioValue ${gpioValue}, delay = $currentPlayingDelay")
|
||||
GPIOUtils.setGpioValue(140, gpioValue)
|
||||
Thread.sleep(currentPlayingDelay.toLong())
|
||||
checkStatus()
|
||||
}
|
||||
GPIOUtils.setGpioValue(140, 0)
|
||||
}
|
||||
|
||||
private fun checkStatus() {
|
||||
val finalStatus =
|
||||
(soundMap[TYPE_SOS] == true && soundMap[TYPE_WARN_LOW] == true && soundMap[TYPE_WARN_HIGHT] == true)
|
||||
if (!finalStatus) {
|
||||
soundPlaying = false
|
||||
} else {
|
||||
val delayTime = getDelayTime(soundMap)
|
||||
currentPlayingDelay = delayTime
|
||||
}
|
||||
}
|
||||
|
||||
fun stopSoundIo(type: Int = TYPE_SOS) {
|
||||
soundMap[type] = false
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue