index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Tencent is pleased to support the open source community by making Face-2-Face Translator available.
  3. Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
  4. Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  5. http://opensource.org/licenses/MIT
  6. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  7. */
  8. import { language } from '../../utils/conf.js'
  9. Component({
  10. properties: {
  11. /*
  12. item 格式
  13. {
  14. create: '04/27 15:37',
  15. text: '一二三四五',
  16. translateText: '12345',
  17. voicePath: '',
  18. translateVoicePath: '',
  19. id: 0,
  20. },*/
  21. item: {
  22. type: Object,
  23. value: {},
  24. observer: function(newVal, oldVal){
  25. // 翻译完成后,文字有改变触发重新翻译
  26. if(this.data.recordStatus == 2 && oldVal.text && oldVal.text != '' && newVal.text != oldVal.text) {
  27. this.triggerEvent('translate', {
  28. item: this.data.item,
  29. index: this.data.index,
  30. })
  31. }
  32. // 翻译内容改变触发播放
  33. if(newVal.autoPlay && newVal.translateVoicePath != oldVal.translateVoicePath){
  34. this.autoPlayTranslateVoice()
  35. } else if(newVal.translateVoicePath == "") {
  36. this.playAnimationEnd()
  37. }
  38. }
  39. },
  40. editShow: {
  41. type: Boolean,
  42. value: false,
  43. },
  44. index: {
  45. type: Number,
  46. },
  47. currentTranslateVoice: {
  48. type: String,
  49. observer: function(newVal, oldVal){
  50. if(newVal != '' && newVal != this.data.item.translateVoicePath) {
  51. this.playAnimationEnd()
  52. }
  53. },
  54. },
  55. recordStatus: {
  56. type: Number,
  57. value: 2, // 0:正在识别,1:正在翻译,2:翻译完成
  58. },
  59. },
  60. data: {
  61. tips_language: language[0], // 目前只有中文
  62. modalShow: false, // 展示悬浮框
  63. playType: 'wait', // 语音播放状态
  64. waiting_animation: {},
  65. waiting_animation_1: {},
  66. edit_icon_path: '../../image/edit.png'
  67. },
  68. ready: function () {
  69. if(this.data.item.autoPlay) {
  70. this.autoPlayTranslateVoice()
  71. }
  72. },
  73. // 组件生命周期函数,在组件实例被从页面节点树移除时执行
  74. detached: function() {
  75. // console.log("detach")
  76. },
  77. methods: {
  78. /**
  79. * 显示悬浮框
  80. */
  81. showModal: function() {
  82. this.setData({modalShow: true})
  83. },
  84. /**
  85. * 离开悬浮框
  86. */
  87. modalLeave: function() {
  88. this.setData({modalShow: false})
  89. },
  90. /**
  91. * 点击播放图标
  92. */
  93. playTranslateVoice: function() {
  94. let nowTime = parseInt(+ new Date() / 1000)
  95. let voiceExpiredTime = this.data.item.translateVoiceExpiredTime || 0
  96. if(this.data.playType == 'playing') {
  97. wx.stopBackgroundAudio()
  98. this.playAnimationEnd()
  99. } else if(nowTime < voiceExpiredTime) {
  100. this.autoPlayTranslateVoice()
  101. } else {
  102. this.setData({
  103. playType: 'loading',
  104. })
  105. this.triggerEvent('expired', {
  106. item: this.data.item,
  107. index: this.data.index,
  108. })
  109. }
  110. },
  111. /**
  112. * 播放背景音乐
  113. */
  114. autoPlayTranslateVoice: function (path,index) {
  115. let play_path = this.data.item.translateVoicePath
  116. if(!play_path) {
  117. console.warn("no translate voice path")
  118. return
  119. }
  120. wx.onBackgroundAudioStop(res => {
  121. console.log("play voice end",res)
  122. this.playAnimationEnd()
  123. })
  124. this.playAnimationStart()
  125. wx.playBackgroundAudio({
  126. dataUrl: play_path,
  127. title: '',
  128. success: (res) => {
  129. this.playAnimationStart()
  130. },
  131. fail: (res) => {
  132. // fail
  133. console.log("failed played", play_path);
  134. this.playAnimationEnd()
  135. },
  136. complete: function (res) {
  137. console.log("complete played");
  138. }
  139. })
  140. },
  141. /**
  142. * 开始播放
  143. */
  144. playAnimationStart: function() {
  145. this.setData({
  146. playType: 'playing',
  147. })
  148. },
  149. /**
  150. * 结束播放
  151. */
  152. playAnimationEnd: function() {
  153. this.setData({
  154. playType: 'wait',
  155. })
  156. },
  157. }
  158. });