Commit f5418204 by patpat

增加TTS,增加“请不要重复扫码”语音

parent 409444ee
...@@ -6,6 +6,7 @@ import android.app.Application ...@@ -6,6 +6,7 @@ import android.app.Application
import android.content.Context import android.content.Context
import com.bgycc.smartcanteen.Storage.PayStorage import com.bgycc.smartcanteen.Storage.PayStorage
import com.bgycc.smartcanteen.action.UpdateAction import com.bgycc.smartcanteen.action.UpdateAction
import com.bgycc.smartcanteen.helper.TTSHelper
import com.bgycc.smartcanteen.helper.TimerHelper import com.bgycc.smartcanteen.helper.TimerHelper
import com.bgycc.smartcanteen.helper.WifiHelpler import com.bgycc.smartcanteen.helper.WifiHelpler
import com.bgycc.smartcanteen.manager.NetworkManager import com.bgycc.smartcanteen.manager.NetworkManager
...@@ -59,6 +60,7 @@ class App : Application() { ...@@ -59,6 +60,7 @@ class App : Application() {
NetworkManager.initialize(this) NetworkManager.initialize(this)
WifiHelpler.initialize(this) WifiHelpler.initialize(this)
PayStorage.initialize(this) PayStorage.initialize(this)
TTSHelper.initialize(this)
Device.initialize(this) Device.initialize(this)
PermissionUtils.permission(PermissionConstants.STORAGE).request() PermissionUtils.permission(PermissionConstants.STORAGE).request()
UpdateAction.getDefault().initialize() UpdateAction.getDefault().initialize()
......
...@@ -3,6 +3,7 @@ package com.bgycc.smartcanteen.action; ...@@ -3,6 +3,7 @@ package com.bgycc.smartcanteen.action;
import com.bgycc.smartcanteen.App; import com.bgycc.smartcanteen.App;
import com.bgycc.smartcanteen.Storage.PayStorage; import com.bgycc.smartcanteen.Storage.PayStorage;
import com.bgycc.smartcanteen.event.PayStateEvent; import com.bgycc.smartcanteen.event.PayStateEvent;
import com.bgycc.smartcanteen.event.QRCodeRepeatEvent;
import com.bgycc.smartcanteen.helper.TimerHelper; import com.bgycc.smartcanteen.helper.TimerHelper;
import com.bgycc.smartcanteen.server.websocket.MainWebSocket; import com.bgycc.smartcanteen.server.websocket.MainWebSocket;
import com.bgycc.smartcanteen.task.QRCodeTask; import com.bgycc.smartcanteen.task.QRCodeTask;
...@@ -83,6 +84,7 @@ public class PayOfflineAction extends Action { ...@@ -83,6 +84,7 @@ public class PayOfflineAction extends Action {
} }
if (checkRepeat && mPayCodeHistory.contains(payCode)) { if (checkRepeat && mPayCodeHistory.contains(payCode)) {
setState(State.FAIL, "请不要重复扫码"); setState(State.FAIL, "请不要重复扫码");
EventBus.getDefault().post(new QRCodeRepeatEvent());
return; return;
} }
addPayCodeHistory(payCode); addPayCodeHistory(payCode);
......
...@@ -17,6 +17,7 @@ import com.bgycc.smartcanteen.R ...@@ -17,6 +17,7 @@ import com.bgycc.smartcanteen.R
import com.bgycc.smartcanteen.action.* import com.bgycc.smartcanteen.action.*
import com.bgycc.smartcanteen.event.* import com.bgycc.smartcanteen.event.*
import com.bgycc.smartcanteen.helper.EthernetHelper import com.bgycc.smartcanteen.helper.EthernetHelper
import com.bgycc.smartcanteen.helper.TTSHelper
import com.bgycc.smartcanteen.helper.TimerHelper import com.bgycc.smartcanteen.helper.TimerHelper
import com.bgycc.smartcanteen.helper.WifiHelpler import com.bgycc.smartcanteen.helper.WifiHelpler
import com.bgycc.smartcanteen.manager.NetworkManager import com.bgycc.smartcanteen.manager.NetworkManager
...@@ -45,6 +46,7 @@ class MainActivity : BaseActivity() { ...@@ -45,6 +46,7 @@ class MainActivity : BaseActivity() {
private val mSoundPool: SoundPool = SoundPool(1, AudioManager.STREAM_SYSTEM, 0) private val mSoundPool: SoundPool = SoundPool(1, AudioManager.STREAM_SYSTEM, 0)
private var mBeepSoundId = 0 private var mBeepSoundId = 0
private var mQRCodeInvalidSoundId = 0 private var mQRCodeInvalidSoundId = 0
private var mQRCodeRepeatSoundId = 0
private var mPaySuccessSoundId = 0 private var mPaySuccessSoundId = 0
private var mPayFailSoundId = 0 private var mPayFailSoundId = 0
private var mAudioManager: AudioManager? = null private var mAudioManager: AudioManager? = null
...@@ -80,6 +82,7 @@ class MainActivity : BaseActivity() { ...@@ -80,6 +82,7 @@ class MainActivity : BaseActivity() {
mAudioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager mAudioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
mBeepSoundId = mSoundPool.load(assets.openFd("beep.mp3"), 1) mBeepSoundId = mSoundPool.load(assets.openFd("beep.mp3"), 1)
mQRCodeInvalidSoundId = mSoundPool.load(assets.openFd("qrcode-invalid.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) mPaySuccessSoundId = mSoundPool.load(assets.openFd("pay-success.mp3"), 1)
mPayFailSoundId = mSoundPool.load(assets.openFd("pay-fail.mp3"), 1) mPayFailSoundId = mSoundPool.load(assets.openFd("pay-fail.mp3"), 1)
MainWebSocket.initialize() MainWebSocket.initialize()
...@@ -166,6 +169,13 @@ class MainActivity : BaseActivity() { ...@@ -166,6 +169,13 @@ class MainActivity : BaseActivity() {
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: QRCodeInvalidEvent) { fun onMessageEvent(event: QRCodeInvalidEvent) {
mSoundPool.play(mQRCodeInvalidSoundId, 1f, 1f, 0, 0, 1f) 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("请不要重复扫码")
} }
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
...@@ -181,11 +191,13 @@ class MainActivity : BaseActivity() { ...@@ -181,11 +191,13 @@ class MainActivity : BaseActivity() {
if (StringUtil.isEmpty(event.message)) event.message = "支付成功" if (StringUtil.isEmpty(event.message)) event.message = "支付成功"
_message.text = event.message _message.text = event.message
mSoundPool.play(mPaySuccessSoundId, 1f, 1f, 0, 0, 1f) mSoundPool.play(mPaySuccessSoundId, 1f, 1f, 0, 0, 1f)
TTSHelper.speak(event.message)
} else if (event.state == PayStateEvent.StateEnum.FAIL) { } else if (event.state == PayStateEvent.StateEnum.FAIL) {
_message.setTextColor(0xFFFF0000.toInt()) _message.setTextColor(0xFFFF0000.toInt())
if (StringUtil.isEmpty(event.message)) event.message = "支付失败" if (StringUtil.isEmpty(event.message)) event.message = "支付失败"
_message.text = event.message _message.text = event.message
mSoundPool.play(mPayFailSoundId, 1f, 1f, 0, 0, 1f) mSoundPool.play(mPayFailSoundId, 1f, 1f, 0, 0, 1f)
TTSHelper.speak(event.message)
} }
} }
......
package com.bgycc.smartcanteen.event
class QRCodeRepeatEvent {
}
\ No newline at end of file
package com.bgycc.smartcanteen.helper
import android.content.Context
import android.speech.tts.TextToSpeech
import com.bgycc.smartcanteen.util.LogUtil
import com.blankj.utilcode.util.LogUtils
import java.util.*
object TTSHelper {
private val TAG = TTSHelper::class.java.simpleName
private var mTTS: TextToSpeech? = null
private var mInited = false
fun initialize(context: Context) {
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初始化成功")
}
}
}
fun speak(msg: String) {
if (!mInited) return
mTTS?.speak(msg, TextToSpeech.QUEUE_FLUSH, null)
}
}
\ 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