react-native - react-native 录音机没有停止
问题描述
当我单击记录开始时,我的记录停止按钮不起作用,然后记录停止按钮出现错误,显示“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`
解决方案
根据我的经验,当记录器在上一次运行中未正确停止时,似乎会发生此错误。如果刻录机正确停止,我不会出现此错误。
推荐阅读
- android - 将 ImageReader 和 SurfaceTexture 与 Android Camera2 输出同步
- reactjs - 如何在本机反应中使用列中的图像制作固定网格视图?
- debugging - 量角器:如何禁用木偶调试模式?
- bash - 从特定行号跟踪日志文件
- javascript - 如何在每个页面中加载 Header HTML 文件
- ios - 来自数据模型的对象中的数组为零
- php - Laravel 同时在多个集合中
- sql - 为什么我在 sparkSQL 中得到错误的 count() 值
- java - 我可以在哪里配置特定战争文件的端口号以在 jboss 实例上运行?
- java - 在 JUnit / Mockito 测试中使用 Mocked 对象