I am trying to use the package as mentioned, and here is my code , not sure why it seems to have recorded, but when I click play, no sound can be heard, and even I drag the file to another music player, it can't play the file. I have some basic setup just follow the Example in git from the package.
const RecordingScreen = () => {
const [audio, setAudio] =useState({
recordSecs:0,
recordTime:'00:00:00',
currentPositionSec: 0,
currentDurationSec: 0,
playTime: '00:00:00',
duration:'00:00:00',
})
const audioRecorderPlayer = new AudioRecorderPlayer();
const meteringEnabled = false;
const SetRecording = () => {
onStartRecord();
};
const SetPauseRecording = () => {
onStopRecord();
};
const SetStartPlaying = () => {
onStartPlay();
};
const onStartRecord = async() => {
if (Platform.OS === 'android') {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'Permissions for write access',
message: 'Give permission to your storage to write a file',
buttonPositive: 'ok',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('You can use the storage');
} else {
console.log('permission denied');
return;
}
} catch (err) {
console.warn(err);
return;
}
}
if (Platform.OS === 'android') {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
{
title: 'Permissions for write access',
message: 'Give permission to your storage to write a file',
buttonPositive: 'ok',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('You can use the camera');
} else {
console.log('permission denied');
return;
}
} catch (err) {
console.warn(err);
return;
}
}
const path = Platform.select({
ios: 'file:////XXXXXXsomepathname/audio/helloworld.m4a',
android: 'file:////XXXXXXsomepathname/audio/helloworld.m4a',
});
const audioSet: AudioSet = {
AudioEncoderAndroid: AudioEncoderAndroidType.AAC,
AudioSourceAndroid: AudioSourceAndroidType.MIC,
AVEncoderAudioQualityKeyIOS: AVEncoderAudioQualityIOSType.high,
AVNumberOfChannelsKeyIOS: 2,
AVFormatIDKeyIOS: AVEncodingOption.aac,
};
console.log('audioSet', audioSet);
const uri = await audioRecorderPlayer.startRecorder(path, audioSet);
audioRecorderPlayer.addRecordBackListener((e: any) => {
setAudio({...audio,
recordSecs: e.current_position,
recordTime: audioRecorderPlayer.mmssss(
Math.floor(e.current_position),
),
});
return;
});
console.log(`uri: ${uri}`);
};
const onStopRecord = async() => {
const result = await audioRecorderPlayer.stopRecorder();
audioRecorderPlayer.removeRecordBackListener();
setAudio({...audio,
recordSecs: 0,
});
console.log(`this is the stop result: ${result}`);
};
const onStartPlay = async () => {
console.log('onStartPlay');
const path = Platform.select({
ios: 'file:////XXXXXXsomepathname/audio/helloworld.m4a',
android: 'file:////XXXXXXsomepathname/audio/helloworld.m4a',
});
const msg = await audioRecorderPlayer.startPlayer(path);
audioRecorderPlayer.setVolume(1.0);
console.log(msg);
audioRecorderPlayer.addPlayBackListener((e: any) => {
if (e.current_position === e.duration) {
console.log('finished');
audioRecorderPlayer.stopPlayer();
}
setAudio({...audio,
currentPositionSec: e.current_position,
currentDurationSec: e.duration,
playTime: audioRecorderPlayer.mmssss(
Math.floor(e.current_position),
),
duration: audioRecorderPlayer.mmssss(Math.floor(e.duration)),
});
return
});
};
all the console.log is working, but I am not sure why it seems can't record the sound, I need to use the local path as I have more than one audio file need to be recorded.