cordova-plugin-media icon indicating copy to clipboard operation
cordova-plugin-media copied to clipboard

Not able to record audio in Android 10

Open julia-fix opened this issue 3 years ago • 7 comments

The following code works in Android 8:

var onSuccess = function (e) {
	console.log('onSuccess', e);
};
var onError = function (e) {
	console.log('onError', e);
};
my_media = new Media(cordova.file.externalDataDirectory + 'audio.mp3', onSuccess, onError);
my_media.startRecord();

However startRecord gives an error {code: 1} in Android 10.

What have I tried with same result:

my_media = new Media(cordova.file.cacheDirectory + 'audio.mp3', onSuccess, onError);
my_media.startRecord();

my_media = new Media(cordova.file.applicationStorageDirectory + 'audio.mp3', onSuccess, onError);
my_media.startRecord();

my_media = new Media(cordova.file.dataDirectory + 'audio.mp3', onSuccess, onError);
my_media.startRecord();

I also tried to create file before recording:

resolveLocalFileSystemURL(cordova.file.externalDataDirectory , function (entry) {
	entry.getFile('audio.mp3', {
		create: true,
		exclusive: false
	}, function (fileEntry) {
		console.log('getFile', fileEntry);
                my_media = new Media(cordova.file.externalDataDirectory + 'audio.mp3', onSuccess, onError);
                my_media.startRecord();
	}, function (err) {
		console.log(err);
	});
	}, function (err) {
	console.log(err);
});

The file is created but recording does not start. Please tell if I am doing something wrong or if this plugin is not working with Android 10+ so I will not waste my days trying to make it work.

P.S. similar issues suggests requestLegacyExternalStorage flag but it is not solving problem anymore because Play Market now requires apps to use API level 30.

julia-fix avatar Nov 11 '21 06:11 julia-fix

android:requestLegacyExternalStorage="true"

yuganghe avatar Nov 12 '21 01:11 yuganghe

@yuganghe please read the last line of my question

julia-fix avatar Nov 12 '21 04:11 julia-fix

Hey, it looks like it's fixed by #317. Personally, I've installed cordova-plugin-media directly from GitHub by doing this: npm i --save apache/cordova-plugin-media#4093f7e (4093f7e is the latest commit so far on master branch)

rozpuszczalny avatar Dec 02 '21 14:12 rozpuszczalny

Hey, it looks like it's fixed by #317. Personally, I've installed cordova-plugin-media directly from GitHub by doing this: npm i --save apache/cordova-plugin-media#4093f7e (4093f7e is the latest commit so far on master branch)

it work for me by apply the commit changes into cordova-plugin-media

jpiero-exus avatar Dec 11 '21 16:12 jpiero-exus

@erisu i have tried below github as well but not working. i have tested in android 10. please give some information.

https://github.com/erisu/cordova-plugin-media

edvenkat avatar Dec 23 '21 11:12 edvenkat

the solution that worked for me: since API 30 Android has restrictions on access to external storage.

install file plugin : cordova plugin add cordova-plugin-file

var onSuccess = function (e) { console.log('onSuccess', e); }; var onError = function (e) { console.log('onError', e); };

window.resolveLocalFileSystemURL(cordova.file.dataDirectory,function (directory) { var my_media = new Media(directory+'recording.mp3', onSuccess, onError ) }, err =>{ console.log('resolveLocalFileSystemURL ', JSON.stringify(err)); } );

and it works for me !!!!!

kenfouo avatar Apr 27 '22 15:04 kenfouo

@kenfouo This did not fix the issue for me.

After a number of hours debugging this I finally got it working (my project is two years old):

What worked for me: In package.json -> devDependencies explicitly set versions to: "cordova-plugin-file": "^7.0.0", "cordova-plugin-media": "^6.1.0",

Then (without doing number 2 and 3 below I could not get it to work):

  1. Remove android platform
  2. delete node_modules folder
  3. delete plugins folder
  4. add android platform
  5. run android

orhan-swe avatar Sep 16 '22 16:09 orhan-swe