简体   繁体   中英

react-native audio recorder not stopping

My record stop button isnt working when I'm clicking on the record start then the record stop button getting an error that says "Println needs a message". What do I need to change?

Also my state component for the timer also isnt updating any suggestion why? The timer should change when i click the start record button and then clear out when i say stop

`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`

From my experience this error seems to be happening when the recorder is not stopped properly in the previous run. If the recorder is stopped properly, this error doesn't occur for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM