繁体   English   中英

反应原生录音机不会停止

[英]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.

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