Reputation: 198
I'm using React Native to implement an Android app that helps us record outgoing calls.
After the user types a phone number and clicks call
, I start Recorder and then navigate the user to the phone screen to make a call.
The issue is that I only record voice before and after the user dials. nothing during the call.
I tested on a Nokia C31 (android 12)
here is my code.
import AudioRecorderPlayer, {
AudioEncoderAndroidType,
AudioSet,
AudioSourceAndroidType,
} from 'react-native-audio-recorder-player';
import dayjs from 'dayjs';
import RNFetchBlob from 'rn-fetch-blob';
const dirs = RNFetchBlob.fs.dirs;
const audioSet: AudioSet = {
AudioEncoderAndroid: AudioEncoderAndroidType.DEFAULT,
AudioSourceAndroid: AudioSourceAndroidType.VOICE_COMMUNICATION,
};
export default function Dialer() {
// other states...
const audioRecorderPlayer = useRef(new AudioRecorderPlayer());
const startRecording = async () => {
const formattedDate = dayjs().format('DD-MMM-YYYY_hh-mm-ss-A');
const fileName = `${dirs.CacheDir}/call-record_${formattedDate}.mp3`;
console.log(fileName);
setAudioPath(fileName);
try {
setIsRecording(true);
const result = await audioRecorderPlayer.current.startRecorder(
fileName,
audioSet,
true,
);
console.log('start recording', result);
} catch (error) {
console.error('Error starting recording:', error);
}
};
const handleCall = () => {
if (!isRecording) {
startRecording();
console.log('called');
Linking.openURL(`tel:${number}`);
}
};
//...
}
permissions requested
const permissions = [
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
PermissionsAndroid.PERMISSIONS.CALL_PHONE,
];
here is my manifest
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
I think I need to use the VOICE_CALL instead of VOICE_COMMUNICATION.
but VOICE_CALL needs android.permission.CAPTURE_AUDIO_OUTPUT
.
according to the documentation:
This permission is reserved for use by system components and is not available to third-party applications.
when I use VOICE_CALL, I can not start the Recorder (get error)
Upvotes: 0
Views: 656
Reputation: 93559
Only system apps can use VOICE_CALL. This is for security reasons. Android does not allow any app to get the audio of a call, except for preinstalled apps from the OEM. The only way you can record a call as an app on Android is to use the normal microphone and hope the volume of the call is loud enough that it gets picked up.
Upvotes: 1