首页 > 解决方案 > 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`

标签: react-native

解决方案


根据我的经验,当记录器在上一次运行中未正确停止时,似乎会发生此错误。如果刻录机正确停止,我不会出现此错误。


推荐阅读