<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-app中的createInnerAudioContext使用
最新推荐文章于 2024-05-11 14:55:52 发布