utils.uts 4.0 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
import { fileIo } from '@kit.CoreFileKit';
import { getEnv } from '@dcloudio/uni-runtime';

export function isFileUri(path: string) {
  return path && typeof (path) === 'string' && (path.startsWith('file://') || path.startsWith('datashare://'));
}

export function isSandboxPath(path: string) {
  return path && typeof (path) === 'string' && path.startsWith('/data/storage/');
}

export function getFdFromUriOrSandBoxPath(uri: string) {
  try {
    const file = fileIo.openSync(uri, fileIo.OpenMode.READ_ONLY);
    return file.fd;
  } catch (error) {
    console.info(`[AdvancedAPI] Can not get file from uri: ${uri} `);
  }
  throw new Error('file is not exist')
}

function callCallbacks(callbacks: Function[], ...args: any[]) {
  callbacks.forEach(cb => {
    typeof cb === 'function' && cb(...args)
  })
}

function remoteCallback(callbacks: Function[], callback: Function) {
  const index = callbacks.indexOf(callback)
  if (index > -1) {
    callbacks.splice(index, 1)
  }
}

export class AudioPlayerError {
  errMsg: string
  errCode: number
  constructor(errMsg: string, errCode: number) {
    this.errMsg = errMsg
    this.errCode = errCode
  }
}

export class AudioPlayerCallback {
  onCanplayCallbacks: Function[] = []
  onPlayCallbacks: Function[] = []
  onPauseCallbacks: Function[] = []
  onStopCallbacks: Function[] = []
  onEndedCallbacks: Function[] = []
  onTimeUpdateCallbacks: Function[] = []
  onErrorCallbacks: Function[] = []
  onWaitingCallbacks: Function[] = []
  onSeekingCallbacks: Function[] = []
  onSeekedCallbacks: Function[] = []

  constructor() { }

  canPlay() {
    callCallbacks(this.onCanplayCallbacks)
  }
  onCanplay(callback: Function) {
    this.onCanplayCallbacks.push(callback)
  }
  offCanplay(callback: Function) {
    remoteCallback(this.onCanplayCallbacks, callback)
  }
  play() {
    callCallbacks(this.onPlayCallbacks)
  }
  onPlay(callback: Function) {
    this.onPlayCallbacks.push(callback)
  }
  offPlay(callback: Function) {
    remoteCallback(this.onPlayCallbacks, callback)
  }
  pause() {
    callCallbacks(this.onPauseCallbacks)
  }
  onPause(callback: Function) {
    this.onPauseCallbacks.push(callback)
  }
  offPause(callback: Function) {
    remoteCallback(this.onPauseCallbacks, callback)
  }
  stop() {
    callCallbacks(this.onStopCallbacks)
  }
  onStop(callback: Function) {
    this.onStopCallbacks.push(callback)
  }
  offStop(callback: Function) {
    remoteCallback(this.onStopCallbacks, callback)
  }
  ended() {
    callCallbacks(this.onEndedCallbacks)
  }
  onEnded(callback: Function) {
    this.onEndedCallbacks.push(callback)
  }
  offEnded(callback: Function) {
    remoteCallback(this.onEndedCallbacks, callback)
  }
  timeUpdate(time: number) {
    callCallbacks(this.onTimeUpdateCallbacks, time)
  }
  onTimeUpdate(callback: Function) {
    this.onTimeUpdateCallbacks.push(callback)
  }
  offTimeUpdate(callback: Function) {
    remoteCallback(this.onTimeUpdateCallbacks, callback)
  }
  error(res: AudioPlayerError) {
    callCallbacks(this.onErrorCallbacks, res)
  }
  onError(callback: Function) {
    this.onErrorCallbacks.push(callback)
  }
  offError(callback: Function) {
    remoteCallback(this.onErrorCallbacks, callback)
  }
  onPrev(callback: Function) {
    console.info('ios only');
  }
  onNext(callback: Function) {
    console.info('ios only');
  }
  waiting() {
    callCallbacks(this.onWaitingCallbacks)
  }
  onWaiting(callback: Function) {
    this.onWaitingCallbacks.push(callback)
  }
  offWaiting(callback: Function) {
    remoteCallback(this.onWaitingCallbacks, callback)
  }
  seeking() {
    callCallbacks(this.onSeekingCallbacks)
  }
  onSeeking(callback: Function) {
    this.onSeekingCallbacks.push(callback)
  }
  offSeeking(callback: Function) {
    remoteCallback(this.onSeekingCallbacks, callback)
  }
  seeked() {
    callCallbacks(this.onSeekedCallbacks)
  }
  onSeeked(callback: Function) {
    this.onSeekedCallbacks.push(callback)
  }
  offSeeked(callback: Function) {
    remoteCallback(this.onSeekedCallbacks, callback)
  }
}