Can not play audio file after recording on Android
Please fill the template to help you out. Also, please try the
Exampleproject compare before submiting the issue when you have certain issue with your project setup.
Version of react-native-audio-recorder-player
3.1.1
Version of React Native
0.63.4
Platforms you faced the error (IOS or Android or both?)
Android (didn't test on IOS)
Expected behavior
After recording audio to file I want to play this file
Actual behavior
When I press stop recording and after that I press start play I get an error Possible Unhandled Promise Rejection (id: 0): Error: Prepare failed.: status=0x1
If I reload app then I can play this audio file
Steps to reproduce the behabior
- Start record
- Finish record
- Start play
Code:
const startRecord = async () => { if (Platform.OS === 'android') { try { const grants = await PermissionsAndroid.requestMultiple([ PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE, PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, ]); console.log('write external stroage', grants); if ( grants['android.permission.WRITE_EXTERNAL_STORAGE'] === PermissionsAndroid.RESULTS.GRANTED && grants['android.permission.READ_EXTERNAL_STORAGE'] === PermissionsAndroid.RESULTS.GRANTED && grants['android.permission.RECORD_AUDIO'] === PermissionsAndroid.RESULTS.GRANTED ) { console.log('Permissions granted'); } else { console.log('All required permissions not granted'); return; } } catch (err) { console.warn(err); return; } };
const audioSet = {
AudioEncoderAndroid: AudioEncoderAndroidType.AAC,
AudioSourceAndroid: AudioSourceAndroidType.MIC,
AVEncoderAudioQualityKeyIOS: AVEncoderAudioQualityIOSType.high,
AVNumberOfChannelsKeyIOS: 2,
AVFormatIDKeyIOS: AVEncodingOption.aac,
};
console.log('path ', TEMP_SPEECH_FILE)
const uri = await audioRecorderPlayer.startRecorder(TEMP_SPEECH_FILE, audioSet);
audioRecorderPlayer.addRecordBackListener((e) => {
});
};
const stopRecord = async () => { const result = await audioRecorderPlayer.stopRecorder(); audioRecorderPlayer.removeRecordBackListener(); console.log(result); };
const startPlay = async (e) => { console.log('onStartPlay'); const msg = await audioRecorderPlayer.startPlayer(TEMP_SPEECH_FILE); await audioRecorderPlayer.setVolume(1.0); console.log(msg); audioRecorderPlayer.addPlayBackListener((e) => { if (e.current_position === e.duration) { console.log('finished'); audioRecorderPlayer.stopPlayer(); } }); };
@sheremet-vlad can you share the solution of this error because i am also facing same error on play audio
have you found the. solution.
did any one find the solution?
Try to wrap this with useCallback. For Example
import React, {useState} from 'react';
import {Platform} from 'react-native';
import AudioRecorderPlayer, {
AudioEncoderAndroidType,
AudioSourceAndroidType,
AVEncoderAudioQualityIOSType,
AVEncodingOption,
} from 'react-native-audio-recorder-player';
import {DocumentDirectoryPath} from 'react-native-fs';
const useAudio = () => {
const [recordSecs, setRecordSecs] = useState(0);
const [recordTime, setRecordTime] = useState(0);
const [currentPositionSec, setCurrentPositionSec] = useState(0);
const [currentDurationSec, setCurrentDurationSec] = useState(0);
const [playTime, setPlayTime] = useState(0);
const [duration, setDuration] = useState(0);
const audioRecorderPlayer = new AudioRecorderPlayer();
audioRecorderPlayer.setSubscriptionDuration(0.09); // optional. Default is 0.1
const path = Platform.select({
ios: 'hello.m4a',
android: `${DocumentDirectoryPath}/hello.mp4`,
});
const startRecord = React.useCallback(async () => {
const audioSet = {
AudioEncoderAndroid: AudioEncoderAndroidType.AAC,
AudioSourceAndroid: AudioSourceAndroidType.MIC,
AVEncoderAudioQualityKeyIOS: AVEncoderAudioQualityIOSType.high,
AVNumberOfChannelsKeyIOS: 2,
AVFormatIDKeyIOS: AVEncodingOption.aac,
};
const meteringEnabled = false;
const uri = await audioRecorderPlayer.startRecorder(
path,
audioSet,
meteringEnabled,
);
audioRecorderPlayer.addRecordBackListener(e => {
setRecordSecs(e.currentPosition);
setRecordTime(audioRecorderPlayer.mmssss(Math.floor(e.currentPosition)));
});
}, []);
const stopRecord = React.useCallback(async () => {
const result = await audioRecorderPlayer.stopRecorder();
audioRecorderPlayer.removeRecordBackListener();
setRecordSecs(0);
console.log(result);
}, []);
const playSound = React.useCallback(async e => {
const msg = await audioRecorderPlayer.startPlayer(path);
audioRecorderPlayer.setVolume(1.0);
audioRecorderPlayer.addPlayBackListener(e => {
if (e.currentPosition === e.duration) {
console.log('finished');
audioRecorderPlayer.stopPlayer();
}
setCurrentPositionSec(e.currentPosition);
setCurrentDurationSec(e.duration);
setPlayTime(audioRecorderPlayer.mmssss(Math.floor(e.currentPosition)));
setDuration(audioRecorderPlayer.mmssss(Math.floor(e.duration)));
});
}, []);
const pauseSound = React.useCallback(async e => {
await audioRecorderPlayer.pausePlayer();
}, []);
const stopSound = React.useCallback(async e => {
audioRecorderPlayer.stopPlayer();
audioRecorderPlayer.removePlayBackListener();
}, []);
return {
recordSecs,
recordTime,
currentPositionSec,
currentDurationSec,
playTime,
duration,
startRecord,
stopRecord,
playSound,
pauseSound,
stopSound,
};
};
export default useAudio;