voice-manager.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import {isVoiceRecordUseLatestVersion} from "../../../modules/chat-input/chat-input";
  2. import IMOperator from "../im-operator";
  3. import FileManager from "./base/file-manager";
  4. export default class VoiceManager extends FileManager{
  5. constructor(page) {
  6. super(page);
  7. this.isLatestVersion = isVoiceRecordUseLatestVersion();
  8. //判断是否需要使用高版本语音播放接口
  9. if (this.isLatestVersion) {
  10. this.innerAudioContext = wx.createInnerAudioContext();
  11. }
  12. //在该类被初始化时,绑定语音点击播放事件
  13. this._page.chatVoiceItemClickEvent = (e) => {
  14. let dataset = e.currentTarget.dataset;
  15. console.log('语音Item', dataset);
  16. this._playVoice({dataset})
  17. }
  18. }
  19. /**
  20. * 停止播放所有语音
  21. */
  22. stopAllVoicePlay() {
  23. let that = this._page;
  24. if (this._page.data.isVoicePlaying) {
  25. this._stopVoice();
  26. that.data.chatItems.forEach(item => {
  27. if (IMOperator.VoiceType === item.type) {
  28. item.isPlaying = false
  29. }
  30. });
  31. that.setData({
  32. chatItems: that.data.chatItems,
  33. isVoicePlaying: false
  34. })
  35. }
  36. }
  37. /**
  38. * 停止播放 兼容低版本语音播放
  39. * @private
  40. */
  41. _stopVoice() {
  42. if (this.isLatestVersion) {
  43. this.innerAudioContext.stop();
  44. } else {
  45. wx.stopVoice();
  46. }
  47. }
  48. _playVoice({dataset}) {
  49. let that = this._page;
  50. if (dataset.voicePath === that.data.latestPlayVoicePath && that.data.chatItems[dataset.index].isPlaying) {
  51. this.stopAllVoicePlay();
  52. } else {
  53. this._startPlayVoice(dataset);
  54. let localPath = dataset.voicePath;//优先读取本地路径,可能不存在此文件
  55. this._myPlayVoice(localPath, dataset, function () {
  56. console.log('成功读取了本地语音');
  57. }, () => {
  58. console.log('读取本地语音文件失败,一般情况下是本地没有该文件,需要从服务器下载');
  59. wx.downloadFile({
  60. url: dataset.voicePath,
  61. success: res => {
  62. console.log('下载语音成功', res);
  63. this.__playVoice({
  64. filePath: res.tempFilePath,
  65. success: () => {
  66. this.stopAllVoicePlay();
  67. },
  68. fail: (res) => {
  69. console.log('播放失败了', res);
  70. }
  71. });
  72. }
  73. });
  74. });
  75. }
  76. }
  77. /**
  78. * 播放语音 兼容低版本语音播放
  79. * @param filePath
  80. * @param success
  81. * @param fail
  82. * @private
  83. */
  84. __playVoice({filePath, success, fail}) {
  85. if (this.isLatestVersion) {
  86. this.innerAudioContext.src = filePath;
  87. this.innerAudioContext.startTime = 0;
  88. this.innerAudioContext.play();
  89. this.innerAudioContext.onError((error) => {
  90. this.innerAudioContext.offError();
  91. fail && fail(error);
  92. });
  93. this.innerAudioContext.onEnded(() => {
  94. this.innerAudioContext.offEnded();
  95. success && success();
  96. });
  97. } else {
  98. wx.playVoice({filePath, success, fail});
  99. }
  100. }
  101. _myPlayVoice(filePath, dataset, cbOk, cbError) {
  102. let that = this._page;
  103. if (dataset.isMy || that.data.isAndroid) {
  104. this.__playVoice({
  105. filePath: filePath,
  106. success: () => {
  107. this.stopAllVoicePlay();
  108. typeof cbOk === "function" && cbOk();
  109. },
  110. fail: (res) => {
  111. console.log('播放失败了1', res);
  112. typeof cbError === "function" && cbError(res);
  113. }
  114. });
  115. } else {
  116. wx.downloadFile({
  117. url: dataset.voicePath,
  118. success: res => {
  119. console.log('下载语音成功', res);
  120. this.__playVoice({
  121. filePath: res.tempFilePath,
  122. success: () => {
  123. this.stopAllVoicePlay();
  124. typeof cbOk === "function" && cbOk();
  125. },
  126. fail: (res) => {
  127. console.log('播放失败了', res);
  128. typeof cbError === "function" && cbError(res);
  129. }
  130. });
  131. }
  132. });
  133. }
  134. }
  135. _startPlayVoice(dataset) {
  136. let that = this._page;
  137. let chatItems = that.data.chatItems;
  138. chatItems[dataset.index].isPlaying = true;
  139. if (that.data.latestPlayVoicePath && that.data.latestPlayVoicePath !== chatItems[dataset.index].content) {//如果重复点击同一个,则不将该isPlaying置为false
  140. for (let i = 0, len = chatItems.length; i < len; i++) {
  141. if ('voice' === chatItems[i].type && that.data.latestPlayVoicePath === chatItems[i].content) {
  142. chatItems[i].isPlaying = false;
  143. break;
  144. }
  145. }
  146. }
  147. that.setData({
  148. chatItems: chatItems,
  149. isVoicePlaying: true
  150. });
  151. that.data.latestPlayVoicePath = dataset.voicePath;
  152. }
  153. }