之前发现直接播放会自动中断,所以纠结了半天
先说思路,首先金额转成字符串,然后截取,得到两段数组,然后遍历,前段需要追加单位,后段小数点后,之后得到需要播放的音频数组,在每段播放完进行完后播放下一段。
语音包有点问题就不放了,还是看代码吧
data: {
v_if:false,
money:0,
video_list:[],//语音列表
now_play:0,
max_play:0,
now_video_src:'',
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
wx.setNavigationBarTitle({
title: '支付成功'
})
this.my_video = wx.createInnerAudioContext();
console.log(options);
this.setData({
money:options.money
})
let num = options.money;
let video_url_0 = '../../static/video/0.mp3';
let video_url_1 = '../../static/video/1.mp3';
let video_url_2 = '../../static/video/2.mp3';
let video_url_3 = '../../static/video/3.mp3';
let video_url_4 = '../../static/video/4.mp3';
let video_url_5 = '../../static/video/5.mp3';
let video_url_6 = '../../static/video/6.mp3';
let video_url_7 = '../../static/video/7.mp3';
let video_url_8 = '../../static/video/8.mp3';
let video_url_9 = '../../static/video/9.mp3';
let video_url_10 = '../../static/video/10.mp3';
let video_url_dian = '../../static/video/点.mp3';
let video_url_yuan = '../../static/video/元.mp3';
let video_url_bai = '../../static/video/百.mp3';
let video_url_qian = '../../static/video/千.mp3';
let video_url_wan = '../../static/video/万.mp3';
let num_string = num.toString();
let now_num = num_string.split('.');//截取
// console.log(now_num);
// console.log(now_num[0]);
// console.log(now_num[1]);
this.video_list = [];//每次重置语音列表
for(let i = 0;i<now_num[0].length;i++){
switch (now_num[0][i]){
case '0':
this.add_video(video_url_0);
break;
case '1':
this.add_video(video_url_1);
break;
case '2':
this.add_video(video_url_2);
break;
case '3':
this.add_video(video_url_3);
break;
case '4':
this.add_video(video_url_4);
break;
case '5':
this.add_video(video_url_5);
break;
case '6':
this.add_video(video_url_6);
break;
case '7':
this.add_video(video_url_7);
break;
case '8':
this.add_video(video_url_8);
break;
case '9':
this.add_video(video_url_9);
break;
case '.':
this.add_video(video_url_dian);
default:
}
//处理单位
let now_company = now_num[0].length-i;
console.log('now_company'+now_company);
if(now_num[0][i] != 0){
switch(now_company){
case 2:
this.add_video(video_url_10);
break;
case 3:
this.add_video(video_url_bai);
break;
case 4:
this.add_video(video_url_qian);
break;
case 5:
this.add_video(video_url_wan);
break;
}
}
}
if(now_num[1]){
this.add_video(video_url_dian);
for(let i = 0;i<now_num[1].length;i++){
console.log(now_num[1][i]);
switch (now_num[1][i]){
case '0':
this.add_video(video_url_0);
break;
case '1':
this.add_video(video_url_1);
break;
case '2':
this.add_video(video_url_2);
break;
case '3':
this.add_video(video_url_3);
break;
case '4':
this.add_video(video_url_4);
break;
case '5':
this.add_video(video_url_5);
break;
case '6':
this.add_video(video_url_6);
break;
case '7':
this.add_video(video_url_7);
break;
case '8':
this.add_video(video_url_8);
break;
case '9':
this.add_video(video_url_9);
break;
}
}
}
this.add_video(video_url_yuan);
var video_list = this.video_list;
console.log(video_list);
this.data.max_play = video_list.length;
this.play_video();
},
add_video(e){
// console.log(e)
let video_list =this.video_list;
video_list.push(e);
this.video_list = video_list;
console.log( this.video_list);
},
play_video(){
var that = this;
that.setData({
video_status: false
})
that.now_video_src = that.video_list[that.data.now_play ]
console.log(that.now_video_src);
//开始播放语音
const ordervideo = wx.createInnerAudioContext()//新建一个createInnerAudioContext();
// const ordervideo = that.my_video;
ordervideo.autoplay = true;//音频自动播放设置
ordervideo.src = that.now_video_src;//链接到音频的地址
ordervideo.volume = 9;//音量
ordervideo.play();
ordervideo.onPlay((res) => {
});
ordervideo.onEnded((res) => {
console.log('播放完成');
// console.log( that.data.max_play);
console.log(that.data.now_play);
if(that.data.now_play < that.data.max_play){
let now_play = Number(that.data.now_play)+1;
console.log(now_play);
that.data.now_video_src = that.video_list[now_play];
// that.now_play = now_play;
that.data.now_play = now_play
that.play_video();
}
});
ordervideo.onStop((res) => {
});
ordervideo.onError((res) => {//打印错误
// console.log(res.errMsg);//错误信息
// console.log(res.errCode);//错误码
})
},