elise-audio.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <view class='flex audio' :class="{active:status}" :style="{background:audioColor}" @click='play(audioId)'>
  3. <view class='mr-3'>
  4. <view class="wifi-symbol " :class="status?'active':''">
  5. <view class="wifi-circle first"></view>
  6. <view class="wifi-circle second"></view>
  7. <view class="wifi-circle third"></view>
  8. </view>
  9. </view>
  10. <view v-if="!create" class="ml-4">生成</view>
  11. <block v-else>
  12. <view v-if="Loading" class="loading_icon">
  13. <u-loading-icon size="18" color="#fff"></u-loading-icon>
  14. </view>
  15. <view v-else class="ml-4">{{status?'暂停':'阅读'}}</view>
  16. </block>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. context: null,
  24. duration: 100,
  25. status: false,
  26. task_id:'',
  27. Loading:false,
  28. currentid:''
  29. }
  30. },
  31. props: {
  32. url: {
  33. type: String,
  34. default: ''
  35. },
  36. content: {
  37. type: String,
  38. default: ''
  39. },
  40. audioColor: {
  41. type: String,
  42. default: '#5ba5ef'
  43. },
  44. index: {
  45. type: Number,
  46. default: 0
  47. },
  48. cancel: {
  49. type: Boolean,
  50. default:false
  51. },
  52. create: {
  53. type: Boolean,
  54. default:false
  55. },
  56. durationS: [String, Number],
  57. audioId: [String, Number]
  58. },
  59. created() {
  60. this.context = uni.createInnerAudioContext();
  61. // this.context.autoplay = true
  62. this.context.src = this.url;
  63. this.context.volume = 1;
  64. this.onEnded();
  65. uni.$on('stop', (id) => {
  66. if (id && id != this.audioId) {
  67. this.context.stop();
  68. this.status = false;
  69. } else if (!id) {
  70. this.context.stop();
  71. this.status = false;
  72. }
  73. })
  74. },
  75. methods: {
  76. play(id) { //点击播放
  77. if (this.url) {
  78. if (this.status) {
  79. this.context.pause();
  80. this.status = !this.status;
  81. } else {
  82. uni.$emit('stop', id)
  83. this.context.volume = 1;
  84. this.context.play()
  85. this.status = !this.status;
  86. }
  87. } else {
  88. if(id == this.currentid&&this.Loading){
  89. uni.showToast({
  90. title:'正在生成',
  91. icon:'none'
  92. })
  93. return;
  94. }
  95. if(id == this.currentid&&!this.Loading){
  96. // console.log('1');
  97. if (this.status) {
  98. this.context.pause();
  99. this.status = !this.status;
  100. } else {
  101. uni.$emit('stop', id)
  102. this.context.volume = 1;
  103. this.context.play()
  104. this.status = !this.status;
  105. }
  106. return;
  107. }
  108. this.create = true
  109. this.cancel = false
  110. this.Loading = true
  111. this.currentid = id
  112. this.$http('ai.tts', {
  113. text: this.content
  114. }).then(res => {
  115. if(res.code == 0){
  116. this.task_id = res.data.task_id
  117. this.aittsJob(id)
  118. }else{
  119. uni.showToast({
  120. title:'阅读失败',
  121. icon:'none'
  122. })
  123. this.Loading = false
  124. this.create = false
  125. }
  126. })
  127. }
  128. },
  129. aittsJob(id){
  130. this.$http('ai.ttsJob', {
  131. task_ids: this.task_id
  132. }).then(res => {
  133. if (res.code == 0) {
  134. if(this.cancel){
  135. this.status = false;
  136. this.Loading = false
  137. return;
  138. }
  139. if (res.data.task_status == 'Running') {
  140. setTimeout(() => {
  141. this.aittsJob()
  142. }, 1500);
  143. return;
  144. }
  145. let ress = {
  146. speech_url:res.data.task_result.speech_url,
  147. index:this.index
  148. }
  149. this.context.src = res.data.task_result.speech_url
  150. uni.$emit('stop', id)
  151. // this.status = !this.status;
  152. this.Loading = false
  153. this.$emit('aittsjob',ress);
  154. // this.context.play()
  155. } else {
  156. uni.showToast({
  157. title:'阅读失败',
  158. icon:'none'
  159. })
  160. this.Loading = false
  161. this.create = false
  162. }
  163. });
  164. },
  165. onEnded() { //播放结束
  166. this.context.onEnded(() => {
  167. this.status = false;
  168. })
  169. },
  170. }
  171. }
  172. </script>
  173. <style lang="scss">
  174. .audio {
  175. background: #68d7bb;
  176. height: 50rpx;
  177. border-radius: 60rpx;
  178. width: 150rpx;
  179. align-items: center;
  180. position: relative;
  181. // padding: 20rpx;
  182. &.active {
  183. opacity: 0.8;
  184. }
  185. }
  186. .flex {
  187. display: flex;
  188. flex-direction: row;
  189. justify-content: space-between;
  190. }
  191. .flex-1 {
  192. flex: 1;
  193. }
  194. .ml-3 {
  195. margin-right: 30rpx;
  196. color: #fff;
  197. }
  198. .ml-4 {
  199. font-size: 24rpx;
  200. margin-right: 20rpx;
  201. color: #fff;
  202. position: absolute;
  203. right: 0;
  204. }
  205. .mr-3 {
  206. margin-left: 30rpx;
  207. }
  208. .wifi-symbol {
  209. width: 50rpx;
  210. height: 50rpx;
  211. box-sizing: border-box;
  212. overflow: hidden;
  213. transform: rotate(135deg) translate3d(0, 0, 0);
  214. -webkit-transform: rotate(135deg) translate3d(0, 0, 0);
  215. backface-visibility: hidden;
  216. -webkit-backface-visibility: hidden;
  217. }
  218. .wifi-circle {
  219. border: 5rpx solid #fff;
  220. border-radius: 50%;
  221. position: absolute;
  222. }
  223. .first {
  224. width: 5rpx;
  225. height: 5rpx;
  226. background: #fff;
  227. top: 45rpx;
  228. left: 45rpx;
  229. }
  230. .second {
  231. width: 25rpx;
  232. height: 25rpx;
  233. top: 35rpx;
  234. left: 35rpx;
  235. }
  236. .third {
  237. width: 40rpx;
  238. height: 40rpx;
  239. top: 25rpx;
  240. left: 25rpx;
  241. }
  242. .active {
  243. .second {
  244. animation: fadeInOut 1s infinite 0.2s;
  245. -webkit-animation: fadeInOut 1s infinite 0.2s;
  246. }
  247. .third {
  248. animation: fadeInOut 1s infinite 0.4s;
  249. -webkit-animation: fadeInOut 1s infinite 0.4s;
  250. }
  251. }
  252. @keyframes fadeInOut {
  253. 0% {
  254. opacity: 0;
  255. /*初始状态 透明度为0*/
  256. }
  257. 100% {
  258. opacity: 1;
  259. /*结尾状态 透明度为1*/
  260. }
  261. }
  262. .loading_icon{
  263. margin-right: 18rpx;
  264. }
  265. </style>