audioClass.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {formatZero, logs} from "@/utils/util";
  2. import Config from "@/utils/config";
  3. export class audioClass {
  4. constructor(topClass, op = null) {
  5. this.topClass = topClass
  6. this.audio = null
  7. this.option = {
  8. machine: 1,
  9. version: 0,
  10. model: 0,
  11. sex: 1,
  12. }
  13. this.allFileList = {
  14. // machine1: audioMachine1,
  15. }
  16. this.run = {
  17. type: 0, // 0 手机播放;1 音箱播放;
  18. list: {}
  19. }
  20. this.audio = null
  21. this.init()
  22. this.setOption(op)
  23. }
  24. init() {
  25. this.audio = uni.createInnerAudioContext({
  26. useWebAudioImplement: true,
  27. }); //创建播放器对象
  28. this.audio.onPlay(() => {
  29. console.log('Audio', '开始播放');
  30. })
  31. this.audio.onError((res) => {
  32. console.log('Audio err', res);
  33. })
  34. this.audio.onSeeking(() => {
  35. console.log('Audio onSeeking');
  36. })
  37. this.audio.onSeeked(() => {
  38. console.log('Audio onSeeked');
  39. })
  40. this.audio.onWaiting((res) => {
  41. console.log('Audio onWaiting', res);
  42. })
  43. // this.audio.onPause(() => {
  44. // console.log('Audio', 'onPause');
  45. // })
  46. // this.audio.onStop(() => {
  47. // console.log('Audio', 'onStop');
  48. // })
  49. // this.audio.onCanplay((e) => {
  50. // console.log('Audio onCanplay', e);
  51. // })
  52. this.audio.onEnded((e) => {
  53. console.log('Audio onEnded 自然结束', this.audio.duration);
  54. })
  55. }
  56. async setOption(option) {
  57. if (!option) return !1
  58. this.option = Object.assign(this.option, option)
  59. this.run.type = 0
  60. this.run.list = {}
  61. let modelList = this.allFileList[`machine${this.option.machine}`][`model${this.option.model}`]
  62. this.run.type = 0
  63. let defaultKey = 'default'
  64. let loudspeakerKey = 'loudspeaker'
  65. if (this.option.sex == 2) defaultKey = 'defaultGirl'
  66. if (this.option.version > 0) {
  67. if (this.option.sex == 2) loudspeakerKey = 'loudspeakerGirl'
  68. if (this.option.version >= modelList[loudspeakerKey].version) this.run.type = 1
  69. }
  70. if (this.run.type == 0 && modelList[defaultKey]) {
  71. this.run.list = modelList[defaultKey]
  72. // for (const i in this.run.list) {
  73. // let it = this.run.list[i]
  74. // if (!it.file) return !0;
  75. // // it.file =it.file
  76. // }
  77. if (this.run.list) {
  78. this.run.list.pause = {pause: !0, seek: 0}
  79. this.run.list.stop = {stop: !0, seek: 0}
  80. }
  81. }
  82. if (this.run.type == 1 && modelList[loudspeakerKey]) {
  83. this.run.list = modelList[loudspeakerKey].list
  84. this.run.list.pause = 'pause'
  85. this.run.list.stop = 'pause'
  86. }
  87. }
  88. play(seconds, queue = false) {
  89. let item = this.run.list[seconds.toString()]
  90. if (!item) return;
  91. // logs('play item', item)
  92. if (this.run.type == 0) {
  93. if (item.file) {
  94. let file = Config.get('imgUrl') + item.file
  95. if (this.audio.src != file) {
  96. this.audio.src = file
  97. }
  98. this.audio.startTime = 0
  99. }
  100. if (item.seek) this.audio.seek(item.seek)
  101. if (item.pause) this.audio.pause()
  102. if (item.stop) this.audio.stop()
  103. if (item.play) this.audio.play()
  104. } else {
  105. let mKey = formatZero(this.option.machine, 2)
  106. let machineClass = this.topClass[`Machine${mKey}Class`]
  107. if (machineClass) {
  108. // if (!queue) this.topClass[`machine${mKey}`]().sentOrder(item, 2)
  109. // else {
  110. logs(`audio type:${this.option.machine} data:${item}`)
  111. this.topClass.queueAdd({
  112. mT: this.option.machine,
  113. t: 2,
  114. p: item,
  115. });
  116. // }
  117. }
  118. }
  119. }
  120. }