<template>
<view>
<image class="read_img" @tap="read_btn" src="" mode="widthFix"></image>
</view>
</template>
<script>
var innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = false;
innerAudioContext.src = '';
export default {
data() {
return {
read_status: false,
read_url: ''
}
},
methods: {
read_btn() {
var _this = this;
if(this.read_status == false) {
this.play_audio()
}else{
this.stop_read()
}
},
stop_read() {
var _this = this;
innerAudioContext.pause()
innerAudioContext.onPause(function() {
console.log('暂停播放');
_this.currentTime = innerAudioContext.currentTime;
})
this.read_status = false;
},
play_audio() {
var _this = this;
var token = _this.current_timu.token;
var Text = _this.current_timu.text;
this.read_url = `http://tsn.baidu.com/text2audio?per=0&ctp=1&lan=zh&ie=UTF-8&spd=4&vol=5%pit=5&tok=${token}&cuid=123456PHP&tex='${Text }'`;
innerAudioContext.src = this.read_url;
innerAudioContext.play();
innerAudioContext.onPlay(function() {
uni.showToast({
title: '播放中...',
icon: 'none'
})
if (_this.currentTime) {
innerAudioContext.seek(_this.currentTime)
}
})
this.read_status = true;
}
}
}
</script>
uni中的audio介绍文档