0
votes

I have installed the plugin and checked all permissions needed, but still getting error code 1 on the media plugin.

I've also added <application android:requestLegacyExternalStorage="true" /> to <edit-config>.

Versions

  • ionic-native/app-version: "^5.33.1"
  • capacitor/cli: "^2.4.8"
  • capacitor/android: "^2.4.6"
  • cordova-plugin-media: "^5.0.3"
  • @ionic-native/file: "^5.34.0"

Code Example:

  startRecording() {
    if (this.platform.is('ios')) {
      this.fileName = 'record' + new Date().getDate() + new Date().getMonth() + new Date().getFullYear() + new Date().getHours() + new Date().getMinutes() + new Date().getSeconds() + '.mp3';
      this.filePath = this.file.documentsDirectory.replace(/file:\/\//g, '') + this.fileName;
      this.audio = this.media.create(this.filePath);
    } else if (this.platform.is('android')) {
      this.fileName = 'record' + new Date().getDate() + new Date().getMonth() + new Date().getFullYear() + new Date().getHours() + new Date().getMinutes() + new Date().getSeconds() + '.mp3';
      this.filePath = this.file.externalDataDirectory + this.fileName;
      this.audio = this.media.create(this.filePath);
    }
    this.audio.onStatusUpdate.subscribe(status => {
      console.log("status", status)
    });

    this.audio.onSuccess.subscribe(() => {
      console.log("success")
    });

    this.audio.onError.subscribe(error => {
      console.log("error", error)
    });

    this.audio.startRecord();
    this.recording = true;
  }
1

1 Answers

0
votes

Since Android 11+ (SDK 30+) you can't write files to Documents or ExternalStorage.

Change the DIR you write your files to. (this.filePath)

See: https://developer.android.com/about/versions/11/privacy/storage