123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import {formatZero, logs} from "@/utils/util";
- import Config from "@/utils/config";
- export class audioClass {
- constructor(topClass, op = null) {
- this.topClass = topClass
- this.audio = null
- this.option = {
- machine: 1,
- version: 0,
- model: 0,
- sex: 1,
- }
- this.allFileList = {
- // machine1: audioMachine1,
- }
- this.run = {
- type: 0, // 0 手机播放;1 音箱播放;
- list: {}
- }
- this.audio = null
- this.init()
- this.setOption(op)
- }
- init() {
- this.audio = uni.createInnerAudioContext({
- useWebAudioImplement: true,
- }); //创建播放器对象
- this.audio.onPlay(() => {
- console.log('Audio', '开始播放');
- })
- this.audio.onError((res) => {
- console.log('Audio err', res);
- })
- this.audio.onSeeking(() => {
- console.log('Audio onSeeking');
- })
- this.audio.onSeeked(() => {
- console.log('Audio onSeeked');
- })
- this.audio.onWaiting((res) => {
- console.log('Audio onWaiting', res);
- })
- // this.audio.onPause(() => {
- // console.log('Audio', 'onPause');
- // })
- // this.audio.onStop(() => {
- // console.log('Audio', 'onStop');
- // })
- // this.audio.onCanplay((e) => {
- // console.log('Audio onCanplay', e);
- // })
- this.audio.onEnded((e) => {
- console.log('Audio onEnded 自然结束', this.audio.duration);
- })
- }
- async setOption(option) {
- if (!option) return !1
- this.option = Object.assign(this.option, option)
- this.run.type = 0
- this.run.list = {}
- let modelList = this.allFileList[`machine${this.option.machine}`][`model${this.option.model}`]
- this.run.type = 0
- let defaultKey = 'default'
- let loudspeakerKey = 'loudspeaker'
- if (this.option.sex == 2) defaultKey = 'defaultGirl'
- if (this.option.version > 0) {
- if (this.option.sex == 2) loudspeakerKey = 'loudspeakerGirl'
- if (this.option.version >= modelList[loudspeakerKey].version) this.run.type = 1
- }
- if (this.run.type == 0 && modelList[defaultKey]) {
- this.run.list = modelList[defaultKey]
- // for (const i in this.run.list) {
- // let it = this.run.list[i]
- // if (!it.file) return !0;
- // // it.file =it.file
- // }
- if (this.run.list) {
- this.run.list.pause = {pause: !0, seek: 0}
- this.run.list.stop = {stop: !0, seek: 0}
- }
- }
- if (this.run.type == 1 && modelList[loudspeakerKey]) {
- this.run.list = modelList[loudspeakerKey].list
- this.run.list.pause = 'pause'
- this.run.list.stop = 'pause'
- }
- }
- play(seconds, queue = false) {
- let item = this.run.list[seconds.toString()]
- if (!item) return;
- // logs('play item', item)
- if (this.run.type == 0) {
- if (item.file) {
- let file = Config.get('imgUrl') + item.file
- if (this.audio.src != file) {
- this.audio.src = file
- }
- this.audio.startTime = 0
- }
- if (item.seek) this.audio.seek(item.seek)
- if (item.pause) this.audio.pause()
- if (item.stop) this.audio.stop()
- if (item.play) this.audio.play()
- } else {
- let mKey = formatZero(this.option.machine, 2)
- let machineClass = this.topClass[`Machine${mKey}Class`]
- if (machineClass) {
- // if (!queue) this.topClass[`machine${mKey}`]().sentOrder(item, 2)
- // else {
- logs(`audio type:${this.option.machine} data:${item}`)
- this.topClass.queueAdd({
- mT: this.option.machine,
- t: 2,
- p: item,
- });
- // }
- }
- }
- }
- }
|