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

784 views Asked by At

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:

  • I thought that its extension issue so I tried replacing mp3 with 3gp and m4a
  • I tried removing file:// from file.externalRootDirectory with slice method - file.externalRootDirectory.slice(7)
  • Instead of externalRootDirectory I also tried applicationDirectory and externalDataDirectory
  • I also submitted my issue on their github repository but I didn't got any response yet
  • I tried finding solutions on other communities and stackoverflow itself but problem still exists
1

There are 1 answers

2
Pankaj Sati On BEST ANSWER

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