desc:播放SOS音效
parent
3232f36ed0
commit
f24181b49b
@ -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…
Reference in New Issue