desc:播放SOS音效

main
xiaowusky 1 year ago
parent 3232f36ed0
commit f24181b49b

@ -3,6 +3,7 @@ package com.yinuo.safetywatcher.watcher
import com.common.commonlib.CommonApplication
import com.common.commonlib.db.DBUtils
import com.common.commonlib.db.entity.Warning
import com.yinuo.safetywatcher.R
import com.yinuo.safetywatcher.TestUtils
import com.yinuo.safetywatcher.watcher.constant.CAMERA_DNS
import com.yinuo.safetywatcher.watcher.constant.CAMERA_GATEWAY
@ -10,6 +11,7 @@ import com.yinuo.safetywatcher.watcher.constant.CAMERA_IP
import com.yinuo.safetywatcher.watcher.constant.CAMERA_NETMASK
import com.yinuo.safetywatcher.watcher.port.cmd.GasPortStatus
import com.yinuo.safetywatcher.watcher.utils.PlatformUtils
import com.yinuo.safetywatcher.watcher.utils.SoundUtils
import com.yinuo.safetywatcher.watcher.wifi.WiFiConfig
import com.yinuo.safetywatcher.watcher.wifi.WiFiModule
import kotlinx.coroutines.DelicateCoroutinesApi
@ -28,6 +30,7 @@ class App : CommonApplication() {
wifiConfig()
tryFixDbData()
TestUtils.insertData()
SoundUtils.init(this, R.raw.sound)
}
private fun wifiConfig() {

@ -24,9 +24,11 @@ import com.yinuo.safetywatcher.watcher.constant.NAMESPACE_ANDROID
import com.yinuo.safetywatcher.watcher.ui.view.CommonDialog
import com.yinuo.safetywatcher.watcher.ui.view.CommonTopBar
import com.yinuo.safetywatcher.watcher.utils.PlatformUtils
import com.yinuo.safetywatcher.watcher.utils.SoundUtils
import com.yinuo.safetywatcher.watcher.utils.VolumeUtil
import com.yinuo.safetywatcher.watcher.utils.trySetCommonBg
abstract class BaseActivity : AppCompatActivity() {
private val baseBinding: ActivityBaseBinding by lazy {
@ -193,10 +195,17 @@ abstract class BaseActivity : AppCompatActivity() {
PlatformUtils.shutDown()
return true
}
} else if (keyCode == KeyEvent.KEYCODE_F1) {
LogUtils.w("BaseActivity F1 pressed")
playSound()
}
return super.dispatchKeyEvent(event)
}
fun playSound() {
SoundUtils.playSound()
}
private fun dealActionDown(event: KeyEvent): Boolean {
val repeatCount = event.repeatCount
val keyCode = event.keyCode

@ -0,0 +1,77 @@
package com.yinuo.safetywatcher.watcher.utils
import android.util.Log
import java.io.File
import java.io.FileNotFoundException
import java.io.FileReader
import java.io.FileWriter
import java.io.IOException
object GPIOUtils {
private const val TAG: String = "GPIOUtils"
private fun setGpioDirection(gpio: Int, direction: String) {
val path = "/sys/class/gpio/gpio$gpio/direction"
if (File(path).exists()) {
var writer: FileWriter? = null
try {
writer = FileWriter(path)
writer.write(direction)
writer.flush()
} catch (ex: IOException) {
Log.d(TAG, "" + ex)
} catch (ex: NumberFormatException) {
Log.d(TAG, "" + ex)
} finally {
if (writer != null) {
try {
writer.close()
} catch (ex: IOException) {
}
}
}
}
}
private fun setGpioValue(gpio: Int, value: Int) {
val path = "/sys/class/gpio/gpio$gpio/value"
if (File(path).exists()) {
var writer: FileWriter? = null
try {
writer = FileWriter(path)
writer.write(value.toString())
writer.flush()
} catch (ex: IOException) {
Log.d(TAG, "" + ex)
} catch (ex: NumberFormatException) {
Log.d(TAG, "" + ex)
} finally {
if (writer != null) {
try {
writer.close()
} catch (ex: IOException) {
}
}
}
}
}
private fun getGpioValue(gpio: Int): String? {
val path = "/sys/class/gpio/gpio$gpio/value"
if (File(path).exists()) {
var reader: FileReader? = null
try {
reader = FileReader(path)
val buf = CharArray(2)
reader.read(buf, 0, 2)
reader.close()
return String(buf).substring(0, 2)
} catch (e: FileNotFoundException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
}
}
return null
}
}

@ -0,0 +1,31 @@
package com.yinuo.safetywatcher.watcher.utils
import android.content.Context
import android.media.AudioAttributes
import android.media.SoundPool
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 var soundId = -1
fun init(context: Context, resID: Int){
soundId = soundPool.load(context, resID, 1); // 替换为你的音效资源文件
}
fun playSound() {
// 播放音效
soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f); // 参数依次为音效ID、左声道音量、右声道音量、优先级、循环次数、播放速度
}
}

Binary file not shown.
Loading…
Cancel
Save