支付宝商家收款时,语音提示:支付宝收款xxx元,当时觉得这东西还挺有趣的,第一时间通知给商家,减少不必要的纠纷,节约时间成本,对商家对用户都挺好的。
在商家版有这样收款播报的功能,我觉得挺好的。
在商家版有这样收款播报的功能,我觉得挺好的。
对列处理及电话中断已经处理。
使用
- gradle引入
allprojects { repositories { ... maven { url \'https://jitpack.io\' } } } dependencies { implementation \'com.github.YzyCoding:PushVoiceBroadcast:1.0.2\' }
- 一行代码引用
VoicePlay.with(MainActivity.this).play(amount);
需求
- 固定播报文字,除了金额动态
- 收到多条推送,顺序播报
- 来电时,暂停播报,挂断后继续播报
- 正在播放音乐,暂停音乐,播放完成继续播放音乐
- 如果音量过小,调节音量
思路
- 金额转大写
- 文字转音频
- 顺序播放
实践
- 关于金额的工具类
public class MoneyUtils { private static final char[] NUM = {\'0\', \'1\', \'2\', \'3\', \'4\', \'5\', \'6\', \'7\', \'8\', \'9\'}; private static final char[] CHINESE_UNIT = {\'元\', \'拾\', \'佰\', \'仟\', \'万\', \'拾\', \'佰\', \'仟\', \'亿\', \'拾\', \'佰\', \'仟\'}; /** * 返回关于钱的中文式大写数字,支仅持到亿 */ public static String readInt(int moneyNum) { String res = ""; int i = 0; if (moneyNum == 0) { return "0"; } if (moneyNum == 10) { return "拾"; } if (moneyNum > 10 && moneyNum < 20) { return "拾" + moneyNum % 10; } while (moneyNum > 0) { res = CHINESE_UNIT[i++] + res; res = NUM[moneyNum % 10] + res; moneyNum /= 10; } return res.replaceAll("0[拾佰仟]", "0") .replaceAll("0+亿", "亿") .replaceAll("0+万", "万") .replaceAll("0+元", "元") .replaceAll("0+", "0") .replace("元", ""); } }
容错处理
/** * 提取字符串中的 数字 带小数点 ,没有就返回"" * * @param money * @return */ public static String getMoney(String money) { Pattern pattern = Pattern.compile("(\\d+\\.\\d+)"); Matcher m = pattern.matcher(money); if (m.find()) { money = m.group(1) == null ? "" : m.group(1); } else { pattern = Pattern.compile("(\\d+)"); m = pattern.matcher(money); if (m.find()) { money = m.group(1) == null ? "" : m.group(1); } else { money = ""; } } return money; }
by: 杨