React-native: Как сохранить файлы аудиозаписи в определенном месте приложения - PullRequest
0 голосов
/ 20 февраля 2019

Я создал проект для записи аудиозаписей.Теперь я хочу сохранить файлы в папку.После этого оттуда я должен хранить имена файлов в базе данных.Любая идея, как это сделать?

componentDidMount() {
  AudioRecorder.requestAuthorization().then((isAuthorised) => {
    this.setState({ hasPermission: isAuthorised });

    if (!isAuthorised) return;

    this.prepareRecordingPath(this.state.audioPath);

    AudioRecorder.onProgress = (data) => {
      this.setState({currentTime: Math.floor(data.currentTime)});
    };

    AudioRecorder.onFinished = (data) => {
      // Android callback comes in the form of a promise instead.
      if (Platform.OS === 'ios') {
        this._finishRecording(data.status === "OK", data.audioFileURL, data.audioFileSize);
      }
    };
  });
}

_renderButton(title, onPress, active) {
  var style = (active) ? styles.activeButtonText : styles.buttonText;

  return (
    <TouchableHighlight style={styles.button} onPress={onPress}>
      <Text style={style}>
        {title}
      </Text>
    </TouchableHighlight>
  );
}

_renderPauseButton(onPress, active) {
  var style = (active) ? styles.activeButtonText : styles.buttonText;
  var title = this.state.paused ? "RESUME" : "PAUSE";
  return (
    <TouchableHighlight style={styles.button} onPress={onPress}>
      <Text style={style}>
        {title}
      </Text>
    </TouchableHighlight>
  );
}

async _pause() {
  if (!this.state.recording) {
    console.warn('Can\'t pause, not recording!');
    return;
  }

  try {
    const filePath = await AudioRecorder.pauseRecording();
    this.setState({paused: true});
  } catch (error) {
    console.error(error);
  }
}

async _resume() {
  if (!this.state.paused) {
    console.warn('Can\'t resume, not paused!');
    return;
  }

  try {
    await AudioRecorder.resumeRecording();
    this.setState({paused: false});
  } catch (error) {
    console.error(error);
  }
}

async _stop() {
  if (!this.state.recording) {
    console.warn('Can\'t stop, not recording!');
    return;
  }

  this.setState({stoppedRecording: true, recording: false, paused: false});

  try {
    const filePath = await AudioRecorder.stopRecording();

    if (Platform.OS === 'android') {
      this._finishRecording(true, filePath);
    }
    return filePath;
  } catch (error) {
    console.error(error);
  }
}

async _play() {
  if (this.state.recording) {
    await this._stop();
  }

  // These timeouts are a hacky workaround for some issues with react-native-sound.
  // See https://github.com/zmxv/react-native-sound/issues/89.
  setTimeout(() => {
    var sound = new Sound(this.state.audioPath, '', (error) => {
      if (error) {
        console.log('failed to load the sound', error);
      }
    });

    setTimeout(() => {
      sound.play((success) => {
        if (success) {
          console.log('successfully finished playing');
        } else {
          console.log('playback failed due to audio decoding errors');
        }
      });
    }, 100);
  }, 100);
}

async _record() {
  if (this.state.recording) {
    console.warn('Already recording!');
    return;
  }

  if (!this.state.hasPermission) {
    console.warn('Can\'t record, no permission granted!');
    return;
  }

  if(this.state.stoppedRecording){
    this.prepareRecordingPath(this.state.audioPath);
  }

  this.setState({recording: true, paused: false});

  try {
    const filePath = await AudioRecorder.startRecording();
  } catch (error) {
    console.error(error);
  }
}

_finishRecording(didSucceed, filePath, fileSize) {
  this.setState({ finished: didSucceed });
  console.warn(`Finished recording of duration ${this.state.currentTime} seconds at path: ${filePath} and size of ${fileSize || 0} bytes`);
}

, если кто-либо имеет об этой концепции, поделитесь примерами ссылок на блоги, я искал много блогов, но я не получаю, потому что я новичок, чтобы реагировать нативно.

...