I am working on creating something to record call in my react native App. For this I have used ReactNative Headless task to get the status of phone and react-native-audio-recorder-player for recording the call but at the end I am getting a file with no sound .
react-native-audio-recorder-player its working fine separetly when I am using this not on call but its saving a file with no sound during call.
Here is my index.js
import { AppRegistry } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
import { AudioRecorder, AudioUtils } from 'react-native-audio'
import AudioRecorderPlayer from 'react-native-audio-recorder-player';
const audioRecorderPlayer = new AudioRecorderPlayer();
// const audioPath = AudioUtils.DocumentDirectoryPath + '/record.aac'
const Rec = async (data) => {
console.log(data.state)
try{
if (data.state === 'extra_state_offhook') {
const result= await audioRecorderPlayer.startRecorder().then(result=>console.log("started")).catch(err=>console.log(err))
audioRecorderPlayer.addRecordBackListener((e) => {
console.log('Recording . . . ', e.current_position);
return;
});
console.log(result);
} else if (data.state === 'extra_state_idle') {
const result = await audioRecorderPlayer.stopRecorder();
audioRecorderPlayer.removeRecordBackListener()
console.log(result);
console.log('Stopped')
}
}catch(err ){
console.log(err)
}
}
AppRegistry.registerHeadlessTask('Rec', () => Rec)
AppRegistry.registerComponent(appName, () => App);
Here is my AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cadric">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.STORAGE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application android:requestLegacyExternalStorage="true" android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="false" android:usesCleartextTraffic="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode" android:launchMode="singleTask" android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
</intent-filter>
</activity>
<service android:name="com.cadric.service.RecService" android:enabled="true" android:exported="true" />
<receiver android:name="com.cadric.receiver.RecReceiver" android:enabled="true" android:exported="true" android:permission="android.permission.BIND_DEVICE_ADMIN">
<intent-filter android:priority="0">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is my RecReciever.java file
package com.cadric.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import com.cadric.service.RecService;
import com.facebook.react.HeadlessJsTaskService;
public final class RecReceiver extends BroadcastReceiver {
public final void onReceive(Context context, Intent intent) {
Boolean incomingCall=false;
Intent recIntent = new Intent(context, RecService.class);
if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
recIntent.putExtra("action", "phone_state");
String phoneState = intent.getStringExtra("state");
if (phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = intent.getStringExtra("incoming_number");
System.out.println(phoneNumber);
incomingCall = true;
recIntent.putExtra("state", "extra_state_ringing");
recIntent.putExtra("incoming_call", true);
recIntent.putExtra("number", phoneNumber);
} else if (phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
if (incomingCall) {
incomingCall = false;
}
recIntent.putExtra("state", "extra_state_offhook");
recIntent.putExtra("incoming_call", false);
} else if (phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
if (incomingCall) {
incomingCall = false;
}
recIntent.putExtra("state", "extra_state_idle");
recIntent.putExtra("incoming_call", false);
}
} else {
recIntent.putExtra("action", "new_outgoing_call");
}
context.startService(recIntent);
HeadlessJsTaskService.acquireWakeLockNow(context);
}
}
Here is my RecService.java file
package com.cadric.service;
import android.content.Intent;
import android.os.Bundle;
import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
import javax.annotation.Nullable;
public class RecService extends HeadlessJsTaskService {
@Nullable
protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
Bundle extras = intent.getExtras();
return new HeadlessJsTaskConfig(
"Rec",
extras != null ? Arguments.fromBundle(extras) : null,
5000);
}
}
Please help me I am stucked , invested more than 20 hours in that already.