(簡單實用)Android支付寶商家收款語音播報


支付寶商家收款時,語音提示:支付寶收款xxx元,當時覺得這東西還挺有趣的,第一時間通知給商家,減少不必要的糾紛,節約時間成本,對商家對用戶都挺好的。
在商家版有這樣收款播報的功能,我覺得挺好的。
對列處理及電話中斷已經處理。
 

使用

  1. gradle引入
   allprojects {
        repositories {
        ...
            maven { url 'https://jitpack.io' }
        }
    }

    dependencies {
        implementation 'com.github.YzyCoding:PushVoiceBroadcast:1.0.2'
    }

 

  1. 一行代碼引用
  VoicePlay.with(MainActivity.this).play(amount);

 



需求

  • 固定播報文字,除了金額動態
  • 收到多條推送,順序播報
  • 來電時,暫停播報,掛斷后繼續播報
  • 正在播放音樂,暫停音樂,播放完成繼續播放音樂
  • 如果音量過小,調節音量

思路

  • 金額轉大寫
  • 文字轉音頻
  • 順序播放

實踐

  1. 關於金額的工具類
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: 楊




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM