|
|
@@ -0,0 +1,152 @@
|
|
|
+/**
|
|
|
+ *示例
|
|
|
+ * this.playPriceAudio(123.32);
|
|
|
+ */
|
|
|
+
|
|
|
+export default {
|
|
|
+ onLunch() {},
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ playing: false,
|
|
|
+ priceHanderList: []
|
|
|
+ };
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ playPriceAudio(price) {
|
|
|
+ this.priceHanderList.push(() => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ this.formatPriceAudio(price)
|
|
|
+ .then(res => {
|
|
|
+ resolve(res);
|
|
|
+ })
|
|
|
+ .catch(err => {
|
|
|
+ reject(err);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+ if (this.playing) {
|
|
|
+ } else {
|
|
|
+ this.playAllPriceAudio();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ async playAllPriceAudio() {
|
|
|
+ try {
|
|
|
+ this.playing = true;
|
|
|
+ await this.formatAudioPromise(this.priceHanderList);
|
|
|
+
|
|
|
+ this.playing = false;
|
|
|
+ return true;
|
|
|
+ } catch (error) {
|
|
|
+ this.playing = false;
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ getPriceAutioSrcByType(type) {
|
|
|
+ let src = `${this.$constant.imgUrl}/audio/${type}.wav`;
|
|
|
+
|
|
|
+ return src;
|
|
|
+ },
|
|
|
+ async formatPriceAudio(num) {
|
|
|
+ let priceHandler = [];
|
|
|
+ let count = String(num);
|
|
|
+ let countList = count.split(".");
|
|
|
+ let intNum = countList[0];
|
|
|
+ let floatNum = countList[1];
|
|
|
+
|
|
|
+ if (intNum >= 0) {
|
|
|
+ let priceList = [];
|
|
|
+ for (const item of intNum) {
|
|
|
+ priceList.push(item);
|
|
|
+ }
|
|
|
+ let individual = priceList.pop();
|
|
|
+ if (individual) {
|
|
|
+ priceHandler.unshift(this.createPlayHandler(individual));
|
|
|
+ let ten = priceList.pop();
|
|
|
+ if (ten) {
|
|
|
+ priceHandler.unshift(this.createPlayHandler("shi"));
|
|
|
+ priceHandler.unshift(this.createPlayHandler(ten));
|
|
|
+
|
|
|
+ let hundred = priceList.pop();
|
|
|
+ if (hundred) {
|
|
|
+ priceHandler.unshift(this.createPlayHandler("bai"));
|
|
|
+ priceHandler.unshift(this.createPlayHandler(hundred));
|
|
|
+
|
|
|
+ let thousand = priceList.pop();
|
|
|
+ if (thousand) {
|
|
|
+ priceHandler.unshift(this.createPlayHandler("qian"));
|
|
|
+ priceHandler.unshift(this.createPlayHandler(thousand));
|
|
|
+
|
|
|
+ if (priceList.length > 0) {
|
|
|
+ priceHandler.unshift(this.createPlayHandler("wan"));
|
|
|
+ for (const item of priceList) {
|
|
|
+ priceList = [this.createPlayHandler(item)].concat(priceList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (floatNum >= 0) {
|
|
|
+ priceHandler.unshift(this.createPlayHandler("0"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (floatNum >= 0) {
|
|
|
+ priceHandler.push(this.createPlayHandler("."));
|
|
|
+ for (const item of floatNum) {
|
|
|
+ priceHandler.push(this.createPlayHandler(item));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ priceHandler.push(this.createPlayHandler("yuan"));
|
|
|
+ priceHandler.unshift(this.createPlayHandler("ccb"));
|
|
|
+
|
|
|
+ const outBack = async () => {
|
|
|
+ return new Promise(resolve => {
|
|
|
+ setTimeout(function() {
|
|
|
+ resolve();
|
|
|
+ }, 500);
|
|
|
+ });
|
|
|
+ };
|
|
|
+ priceHandler.push(outBack);
|
|
|
+
|
|
|
+ return this.formatAudioPromise(priceHandler);
|
|
|
+ },
|
|
|
+ async formatAudioPromise(handlerList) {
|
|
|
+ try {
|
|
|
+ const handlerItem = handlerList.shift();
|
|
|
+ if (handlerItem) {
|
|
|
+ await handlerItem();
|
|
|
+ if (handlerList.length > 0) {
|
|
|
+ return this.formatAudioPromise(handlerList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ } catch (error) {
|
|
|
+ return Promise.reject(error);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ createPlayHandler(type) {
|
|
|
+ return () => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ const innerAudioContext = uni.createInnerAudioContext();
|
|
|
+ innerAudioContext.autoplay = true;
|
|
|
+ innerAudioContext.src = this.getPriceAutioSrcByType(type);
|
|
|
+ innerAudioContext.onPlay(() => {
|
|
|
+ console.log("开始播放", type);
|
|
|
+ });
|
|
|
+ innerAudioContext.onError(res => {
|
|
|
+ console.log(res.errMsg);
|
|
|
+ console.log(res.errCode);
|
|
|
+ reject(res);
|
|
|
+ });
|
|
|
+ innerAudioContext.onEnded(err => {
|
|
|
+ console.log("开始结束", type);
|
|
|
+ innerAudioContext.destroy();
|
|
|
+ resolve(err);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ };
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|