Commit 873fc394 by patpat

增加支付结果语音

parent f5418204
......@@ -36,6 +36,7 @@ import java.text.SimpleDateFormat
import java.util.*
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
import java.lang.Exception
class MainActivity : BaseActivity() {
......@@ -43,12 +44,6 @@ class MainActivity : BaseActivity() {
var TAG: String = MainActivity::class.java.simpleName
}
private val mSoundPool: SoundPool = SoundPool(1, AudioManager.STREAM_SYSTEM, 0)
private var mBeepSoundId = 0
private var mQRCodeInvalidSoundId = 0
private var mQRCodeRepeatSoundId = 0
private var mPaySuccessSoundId = 0
private var mPayFailSoundId = 0
private var mAudioManager: AudioManager? = null
private val mHandler = Handler()
private var mTimerId = -1L
......@@ -80,11 +75,6 @@ class MainActivity : BaseActivity() {
private fun init() {
mAudioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
mBeepSoundId = mSoundPool.load(assets.openFd("beep.mp3"), 1)
mQRCodeInvalidSoundId = mSoundPool.load(assets.openFd("qrcode-invalid.mp3"), 1)
mQRCodeRepeatSoundId = mSoundPool.load(assets.openFd("qrcode-repeat.mp3"), 1)
mPaySuccessSoundId = mSoundPool.load(assets.openFd("pay-success.mp3"), 1)
mPayFailSoundId = mSoundPool.load(assets.openFd("pay-fail.mp3"), 1)
MainWebSocket.initialize()
QRCodeTask.getInstance().start()
ButtonTask.start()
......@@ -134,14 +124,14 @@ class MainActivity : BaseActivity() {
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
Log.i(TAG, "keyCode: $keyCode")
when(keyCode) {
KeyEvent.KEYCODE_NUMPAD_ADD -> {
KeyEvent.KEYCODE_DPAD_UP -> {
mAudioManager!!.adjustStreamVolume(
AudioManager.STREAM_SYSTEM,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_SHOW_UI
)
}
KeyEvent.KEYCODE_NUMPAD_SUBTRACT -> {
KeyEvent.KEYCODE_DPAD_DOWN -> {
mAudioManager!!.adjustStreamVolume(
AudioManager.STREAM_SYSTEM,
AudioManager.ADJUST_LOWER,
......@@ -163,18 +153,16 @@ class MainActivity : BaseActivity() {
@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: QRCodeEvent) {
printQRCode(event)
mSoundPool.play(mBeepSoundId, 1f, 1f, 0, 0, 1f)
TTSHelper.speak("beep")
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: QRCodeInvalidEvent) {
mSoundPool.play(mQRCodeInvalidSoundId, 1f, 1f, 0, 0, 1f)
TTSHelper.speak("无效二维码")
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: QRCodeRepeatEvent) {
mSoundPool.play(mQRCodeRepeatSoundId, 1f, 1f, 0, 0, 1f)
TTSHelper.speak("请不要重复扫码")
}
......@@ -190,13 +178,11 @@ class MainActivity : BaseActivity() {
_message.setTextColor(0xFF009900.toInt())
if (StringUtil.isEmpty(event.message)) event.message = "支付成功"
_message.text = event.message
mSoundPool.play(mPaySuccessSoundId, 1f, 1f, 0, 0, 1f)
TTSHelper.speak(event.message)
} else if (event.state == PayStateEvent.StateEnum.FAIL) {
_message.setTextColor(0xFFFF0000.toInt())
if (StringUtil.isEmpty(event.message)) event.message = "支付失败"
_message.text = event.message
mSoundPool.play(mPayFailSoundId, 1f, 1f, 0, 0, 1f)
TTSHelper.speak(event.message)
}
}
......
package com.bgycc.smartcanteen.helper
import android.content.Context
import android.content.res.AssetManager
import android.media.AudioManager
import android.media.SoundPool
import android.speech.tts.TextToSpeech
import com.bgycc.smartcanteen.util.LogUtil
import com.blankj.utilcode.util.LogUtils
import java.util.*
import kotlin.collections.HashMap
object TTSHelper {
private val TAG = TTSHelper::class.java.simpleName
private var mAssetManager: AssetManager? = null
private var mSupportTTS = false
private var mTTS: TextToSpeech? = null
private var mInited = false
private val mSoundPool: SoundPool = SoundPool(1, AudioManager.STREAM_SYSTEM, 0)
private val mSoundIdMap = HashMap<String, Int>()
fun initialize(context: Context) {
mAssetManager = context.assets
mSoundPool.setOnLoadCompleteListener { soundPool, sampleId, status ->
if (status == 0) {
soundPool.play(sampleId, 1f, 1f, 100, 0, 1f)
}
}
mTTS = TextToSpeech(context) {
if (it == TextToSpeech.SUCCESS) {
LogUtil.i(TAG, "TTS初始化成功")
LogUtils.file(TAG, "TTS初始化成功")
val result = mTTS?.setLanguage(Locale.CHINESE)
mInited = true
} else {
LogUtil.i(TAG, "TTS初始化失败")
LogUtils.file(TAG, "TTS初始化成功")
val result = mTTS?.setLanguage(Locale.CHINESE)!!
if (result >= TextToSpeech.LANG_AVAILABLE) {
mSupportTTS = true
}
}
}
}
fun speak(msg: String) {
if (!mInited) return
mTTS?.speak(msg, TextToSpeech.QUEUE_FLUSH, null)
if (mSupportTTS) {
mTTS?.speak(msg, TextToSpeech.QUEUE_FLUSH, null)
} else {
playSound(msg)
}
}
private fun playSound(msg: String) {
mAssetManager ?: return
val soundName = when (msg) {
"beep" -> "beep.mp3"
"无效二维码" -> "qrcode-invalid.mp3"
"请不要重复扫码" -> "qrcode-repeat.mp3"
"支付成功" -> "pay-success.mp3"
"支付失败" -> "pay-fail.mp3"
"未获取到当前设备信息" -> "pay-device-not-found.mp3"
"设备已禁用" -> "pay-device-disable.mp3"
"未设置食堂参数" -> "pay-canteen-params-unset.mp3"
"当前不支持第三方支付" -> "pay-unsupported-other-platform.mp3"
"超过扣款限制" -> "pay-over-cost-limit.mp3"
"设备未绑定商品" -> "pay-unbind.mp3"
"扣款金额为0" -> "pay-price-zero.mp3"
"未获取到当前用户信息" -> "pay-no-user-info.mp3"
"已就餐,请勿重复支付" -> "pay-once.mp3"
"您的就餐权限已失效" -> "pay-invalid-permission.mp3"
"非本窗口营业时间" -> "pay-window-rest-time.mp3"
"非营业时间" -> "pay-canteen-rest-time.mp3"
"可用余额不足" -> "pay-balance-not-enough.mp3"
else -> null
} ?: return
var id = mSoundIdMap[soundName]
if (id == null) {
id = mSoundPool.load(mAssetManager!!.openFd(soundName), 1)
mSoundIdMap[soundName] = id
} else {
mSoundPool.play(id, 1f, 1f, 0, 0, 1f)
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment