Example #1
0
  async prepareToRecordAsync(): Promise<RecordingStatus> {
    if (!_enabled) {
      throw _DISABLED_ERROR;
    }

    if (_recorderExists) {
      throw new Error(
        'Only one Recording object can be prepared at a given time.'
      );
    }

    if (this._isDoneRecording) {
      throw new Error(
        'This Recording object is done recording; you must make a new one.'
      );
    }

    if (!this._canRecord) {
      const {
        uri,
        status,
      }: {
        uri: string,
        status: Object, // status is of type RecordingStatus, but without the canRecord field populated.
      } = await NativeModules.ExponentAV.prepareAudioRecorder();
      _recorderExists = true;
      this._uri = uri;
      this._canRecord = true;
      this._callCallbackForNewStatus(status);
      this._enablePollingIfNecessaryAndPossible();
      return status;
    } else {
      throw new Error('This Recording object is already prepared to record.');
    }
  }
Example #2
0
  async stopAndUnloadAsync(): Promise<RecordingStatus> {
    if (!this._canRecord) {
      throw new Error('Cannot unload a Recording that has not been prepared.');
    }
    // We perform a separate native API call so that the state of the Recording can be updated with
    // the final duration of the recording. (We cast stopStatus as Object to appease Flow)
    const stopStatus: Object = await NativeModules.ExponentAV.stopAudioRecording();
    this._finalDurationMillis = stopStatus.durationMillis;
    this._disablePolling();

    await NativeModules.ExponentAV.unloadAudioRecorder();
    this._canRecord = false;
    this._isDoneRecording = true;
    _recorderExists = false;
    return this.getStatusAsync(); // Automatically calls the callback for the final state.
  }
Example #3
0
export async function setAudioModeAsync(mode) {
    const missingKeys = _findMissingKeys(mode, [
        'allowsRecordingIOS',
        'interruptionModeIOS',
        'playsInSilentModeIOS',
        'interruptionModeAndroid',
        'shouldDuckAndroid',
        'playThroughEarpieceAndroid',
    ]);
    if (missingKeys.length > 0) {
        throw new Error(`Audio mode attempted to be set without the required keys: ${JSON.stringify(missingKeys)}`);
    }
    if (!_isValueValid(mode.interruptionModeIOS, [
        INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS,
        INTERRUPTION_MODE_IOS_DO_NOT_MIX,
        INTERRUPTION_MODE_IOS_DUCK_OTHERS,
    ])) {
        throw new Error(`"interruptionModeIOS" was set to an invalid value.`);
    }
    if (!_isValueValid(mode.interruptionModeAndroid, [
        INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
        INTERRUPTION_MODE_ANDROID_DUCK_OTHERS,
    ])) {
        throw new Error(`"interruptionModeAndroid" was set to an invalid value.`);
    }
    if (typeof mode.allowsRecordingIOS !== 'boolean' ||
        typeof mode.playsInSilentModeIOS !== 'boolean' ||
        typeof mode.shouldDuckAndroid !== 'boolean' ||
        typeof mode.playThroughEarpieceAndroid !== 'boolean') {
        throw new Error('"allowsRecordingIOS", "playsInSilentModeIOS", "playThroughEarpieceAndroid", and "shouldDuckAndroid" must be booleans.');
    }
    return await NativeModules.ExponentAV.setAudioMode(mode);
}
Example #4
0
export async function setIsEnabledAsync(value: boolean): Promise<void> {
  _enabled = value;
  await NativeModules.ExponentAV.setAudioIsEnabled(value);
  // TODO : We immediately pause all players when disabled, but we do not resume all shouldPlay
  // players when enabled. Perhaps for completeness we should allow this; the design of the
  // enabling API is for people to enable / disable this audio library, but I think that it should
  // intuitively also double as a global pause/resume.
}
Example #5
0
 // TODO: We can optimize by only using time observer on native if (this._callback).
 _setStatusUpdateCallback() {
   if (this._loaded) {
     NativeModules.ExponentAV.setStatusUpdateCallbackForSound(
       this._key,
       this._statusUpdateCallback
     );
   }
 }
 const loadSuccess = (key: number, status: PlaybackStatus) => {
   this._key = key;
   this._loaded = true;
   this._loading = false;
   NativeModules.ExponentAV.setErrorCallbackForSound(this._key, this._errorCallback);
   this._subscribeToNativeStatusUpdateEvents();
   this._callOnPlaybackStatusUpdateForNewStatus(status);
   resolve(status);
 };
Example #7
0
 async unloadAsync(): Promise<PlaybackStatus> {
   if (this._loaded) {
     this._loaded = false;
     const key = this._key;
     this._key = -1;
     const status = await NativeModules.ExponentAV.unloadForSound(key);
     this._callCallbackForNewStatus(status);
     return status;
   } else {
     return this.getStatusAsync(); // Automatically calls the callback.
   }
 }
 async unloadAsync(): Promise<PlaybackStatus> {
   if (this._loaded) {
     this._loaded = false;
     const key = this._key;
     this._key = -1;
     const status = await NativeModules.ExponentAV.unloadForSound(key);
     this._callOnPlaybackStatusUpdateForNewStatus(status);
     this._clearSubscriptions();
     return status;
   } else {
     return this.getStatusAsync(); // Automatically calls onPlaybackStatusUpdate.
   }
 }
 function(resolve, reject) {
   const loadSuccess = (key: number, status: PlaybackStatus) => {
     this._key = key;
     this._loaded = true;
     this._loading = false;
     NativeModules.ExponentAV.setErrorCallbackForSound(this._key, this._errorCallback);
     this._subscribeToNativeStatusUpdateEvents();
     this._callOnPlaybackStatusUpdateForNewStatus(status);
     resolve(status);
   };
   const loadError = (error: string) => {
     this._loading = false;
     reject(new Error(error));
   };
   NativeModules.ExponentAV.loadForSound(
     nativeSource,
     fullInitialStatus,
     loadSuccess,
     loadError
   );
 }.bind(this)
Example #10
0
export async function setAudioModeAsync(mode: AudioMode): Promise<void> {
  if (
    !('allowsRecordingIOS' in mode) ||
    !('interruptionModeIOS' in mode) ||
    !('playsInSilentModeIOS' in mode) ||
    !('interruptionModeAndroid' in mode) ||
    !('shouldDuckAndroid' in mode)
  ) {
    throw new Error(
      'Audio mode must contain keys "allowsRecordingIOS", "interruptionModeIOS", "playsInSilentModeIOS", "interruptionModeAndroid", and "shouldDuckAndroid".'
    );
  }
  if (
    mode.interruptionModeIOS !== INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS &&
    mode.interruptionModeIOS !== INTERRUPTION_MODE_IOS_DO_NOT_MIX &&
    mode.interruptionModeIOS !== INTERRUPTION_MODE_IOS_DUCK_OTHERS
  ) {
    throw new Error(
      `"interruptionModeIOS" must an integer between ${INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS} and ${INTERRUPTION_MODE_IOS_DUCK_OTHERS}.`
    );
  }
  if (
    mode.interruptionModeAndroid !== INTERRUPTION_MODE_ANDROID_DO_NOT_MIX &&
    mode.interruptionModeAndroid !== INTERRUPTION_MODE_ANDROID_DUCK_OTHERS
  ) {
    throw new Error(
      `"interruptionModeAndroid" must an integer between ${INTERRUPTION_MODE_ANDROID_DO_NOT_MIX} and ${INTERRUPTION_MODE_ANDROID_DUCK_OTHERS}.`
    );
  }
  if (
    typeof mode.allowsRecordingIOS !== 'boolean' ||
    typeof mode.playsInSilentModeIOS !== 'boolean' ||
    typeof mode.shouldDuckAndroid !== 'boolean'
  ) {
    throw new Error(
      '"allowsRecordingIOS", "playsInSilentModeIOS", and "shouldDuckAndroid" must be booleans.'
    );
  }
  await NativeModules.ExponentAV.setAudioMode(mode);
}
Example #11
0
 return this._performOperationAndHandleStatusAsync((tag: number) =>
   NativeModules.ExponentAV.loadForVideo(tag, nativeSource, fullInitialStatus)
 return this._performOperationAndHandleStatusAsync(() =>
   NativeModules.ExponentAV.replaySound(this._key, {
     ...status,
     positionMillis: 0,
     shouldPlay: true,
   })
Example #13
0
 return this._performOperationAndHandleStatusAsync((tag: number) =>
   NativeModules.ExponentAV.setStatusForVideo(tag, status)
Example #14
0
 return this._performOperationAndHandleStatusAsync(() =>
   NativeModules.ExponentAV.pauseAudioRecording()
Example #15
0
 return this._performOperationAndHandleStatusAsync((tag: number) =>
   NativeModules.ExponentAV.replayVideo(tag, {
     ...status,
     positionMillis: 0,
     shouldPlay: true,
   })
Example #16
0
 return this._performOperationAndHandleStatusAsync(() =>
   NativeModules.ExponentAV.setStatusForSound(this._key, status)
Example #17
0
this._performOperationAndHandleStatusAsync((tag: number) =>
  NativeModules.ExponentAV.presentFullscreenPlayer(tag)