ionic-frameworkcapacitorcordova-media-plugin

Recorded file neither saving nor playing in cordova-plugin-media (Error code 1)


I'm using this plugin: cordova-plugin-media with ionic capacitor instead of cordova with command ionic cap sync.

My code:

audioFile: MediaObject;
constructor(private media: Media, private plt: Platform, private file: File) {}
ionViewDidEnter() {
    this.plt.ready()
    .then(() => {
      setTimeout(() => {
        this.audioFile = this.media.create(this.file.externalRootDirectory + 'audiofile.mp3');
        this.audioFile.onStatusUpdate.subscribe(status => console.log('status: ', status));
        this.audioFile.onSuccess.subscribe(() => console.log('Action is successful'));
        this.audioFile.onError.subscribe(error => console.log('Error: ', error));
      }, 1000);
    })
    .catch(err => console.log('ready error: ', err));
}

record() {
    // When record button clicked
    this.audioFile.startRecord();
}

stop() {
    // When stop button clicked
    this.audioFile.stopRecord();
    this.audioFile.play();
}

When I clicked Record btn, record() was executed and output was: Error: 1 and when I stopped recording than the output was status: 4 and Action is successful I expected to either play the recording or get audiofile.mp3 in the file manager of my android device but I didn't observed any of it. Can someone help me with this issue?

What I tried:


Solution

  • Android 10 uses a new file system therefore plugins that have not updated themselves might not read/create files on Android 10.

    You need to manually add this line in your manifest file : android:requestLegacyExternalStorage="true"

    1. Open your project in Android Studio and open the AndroidManifest file.
    2. In tag inside your manifest file, add android:requestLegacyExternalStorage="true"
    3. Rebuild your project