An error occurs while trying to stop the recording on IOS
srish opened this issue · 4 comments
While trying to stop the recording on IOS, I run into the following error:
AVAudioSession.mm:1079:-[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.
The code is below. In a nutshell, it plays a video in the background by default, and there is a button for recording/ stopping the audio.
export default class VideoPlayer extends Component {
constructor(props){
super(props);
this.state = {
recording: false
}
}
render() {
const { recording } = this.state;
let button = (
<TouchableOpacity
onPress={this.startAudioRecording.bind(this)}
style={styles.capture}
>
<Text style={{ fontSize: 14 }}> RECORD </Text>
</TouchableOpacity>
);
if (recording) {
button = (
<TouchableOpacity
onPress={this.stopAudioRecording.bind(this)}
style={styles.capture}
>
<Text style={{ fontSize: 14 }}> STOP </Text>
</TouchableOpacity>
);
}
return(
<View style={styles.videoContainer}>
<Video source={{ uri: global.clipUrl }}
ref={(ref) => {
this.player = ref
}}
muted={false}
onBuffer={this.onBuffer}
onEnd={this.onEnd}
onError={this.videoError}
style={styles.backgroundVideo} />
<View
style={{ flex: 0, flexDirection: "row", justifyContent: "center" }}
>
{button}
</View>
</View>
);
}
async startAudioRecording() {
this.setState({ recording: true });
await SoundRecorder.start(SoundRecorder.PATH_CACHE + '/audio_1.mp4')
.then(function() {
console.log('started recording');
});
}
stopAudioRecording() {
SoundRecorder.stop()
.then(function(result) {
console.log('Stopped recording, audio file saved at: ' + result.path);
}).catch(function(error) {
console.log("An error occured while stoping the recording: " + error);
});
this.setState({ recording: false });
}
}
After searching for this issue a bit more, I found a related one here: jsierles/react-native-audio#85. It seems like it might be happening because there isn't much time gap between stopping/pausing the recording and deactivating the audio session. If I add a delay between the two executions after this line in RNSoundRecorder.m
(see below), it works.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(100.0 * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[session setActive:NO error:&err];
});
I'm testing the code on a 6s
device. Not sure if this seems like a right approach to address the issue. Happy to send a PR if it feels right.
@kevinresol I've added the code in the issue description with a brief explanation of what I'm trying to do.
Thanks, could you make a PR?
Closed by #31