[英]react-native audio recorder not stopping
當我單擊記錄開始時,我的記錄停止按鈕不起作用,然后記錄停止按鈕收到一條錯誤消息,提示“Println 需要一條消息”。 我需要改變什么?
此外,我的計時器狀態組件也沒有更新任何建議,為什么? 當我點擊開始記錄按鈕時計時器應該改變,然后在我說停止時清除
`import AudioRecorderPlayer, {
AVEncoderAudioQualityIOSType,
AVEncodingOption,
AudioEncoderAndroidType,
AudioSet,
AudioSourceAndroidType,
} from 'react-native-audio-recorder-player'
import React, { Component } from 'react'
import { View, Button, Text } from 'react-native'
import { Header, Divider } from 'react-native-elements'
class Record extends Component {
constructor(props) {
super(props);
this.state = {
isLoggingIn: false,
recordSecs: 0,
recordTime: '00:00:00',
currentPositionSec: 0,
currentDurationSec: 0,
playTime: '00:00:00',
duration: '00:00:00',
};
this.audioRecorderPlayer = new AudioRecorderPlayer();
this.audioRecorderPlayer.setSubscriptionDuration(0.09); // optional. Default is 0.1
}
onStartRecord = async () => {
const path = 'hello.m4a';
const audioSet = {
AudioEncoderAndroid: AudioEncoderAndroidType.AAC,
AudioSourceAndroid: AudioSourceAndroidType.MIC,
AVEncoderAudioQualityKeyIOS: AVEncoderAudioQualityIOSType.high,
AVNumberOfChannelsKeyIOS: 2,
AVFormatIDKeyIOS: AVEncodingOption.aac,
};
console.log('audioSet', audioSet);
const uri = await this.audioRecorderPlayer.startRecorder(path, audioSet);
this.audioRecorderPlayer.addRecordBackListener((e) => {
this.setState({
recordSecs: e.current_position,
recordTime: this.audioRecorderPlayer.mmssss(
Math.floor(e.current_position),
),
});
});
console.log(`uri: ${uri}`);
};
onStopRecord = async () => {
const result = await this.audioRecorderPlayer.stopRecorder();
this.audioRecorderPlayer.removeRecordBackListener();
this.setState({
recordSecs: 0,
});
console.log(result);
};
onStartPlay = async (e) => {
console.log('onStartPlay');
const path = 'sdcard/hello.m4a'
const msg = await this.audioRecorderPlayer.startPlayer(path);
this.audioRecorderPlayer.setVolume(1.0);
console.log(msg);
this.audioRecorderPlayer.addPlayBackListener((e) => {
if (e.current_position === e.duration) {
console.log('finished');
this.audioRecorderPlayer.stopPlayer();
}
this.setState({
currentPositionSec: e.current_position,
currentDurationSec: e.duration,
playTime: this.audioRecorderPlayer.mmssss(
Math.floor(e.current_position),
),
duration: this.audioRecorderPlayer.mmssss(Math.floor(e.duration)),
});
});
};
onPausePlay = async (e) => {
await this.audioRecorderPlayer.pausePlayer();
};
onStopPlay = async (e) => {
console.log('onStopPlay');
this.audioRecorderPlayer.stopPlayer();
this.audioRecorderPlayer.removePlayBackListener();
};
render() {
return (<View>
<Header>InstaPlayer</Header>
<Text>{this.state.recordTime}</Text>
<Button title="Record" onPress={() => this.onStartRecord()} />
<Button title="Stop"
onPress={() => this.onStopRecord()}
/>
<Text>{this.state.playTime} / {this.state.duration}</Text>
<Button title="PLAY" onPress={() => this.onStartPlay()} />
<Button
title="Pause"
onPress={() => this.onPausePlay()}
/>
<Button
title="Stop"
onPress={() => this.onStopPlay()}
/>
</View>)
}
}
export default Record`
根據我的經驗,當記錄器在上次運行中沒有正確停止時,似乎會發生此錯誤。 如果錄音機正確停止,我就不會出現此錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.