import React from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, TextInput, Image, TouchableOpacity, View, Vibration, PermissionsAndroid, NativeEventEmitter, Animated } from 'react-native'; // import LottieView from 'lottie-react-native'; //@ts-ignore // import { Recognizer, Synthesizer, SpeechConstant } from "react-native-speech-iflytek"; import { colors, filterEmoji, isEmpty, isIos, onePx, px2dp, toast, toastShort } from '../../utils'; import redux from './../../redux/store'; interface Props { title?: string initContent?: string hasPermission?: boolean maxLength?: number placeHolder?: string inlineLayout?: boolean isNeeded?: boolean contentHeight?: number } interface State { remark: string animatedValue: any active: boolean } export default class KitRemarkView extends React.Component { constructor(props: Props) { super(props); }; public floatBtnRef = null; public playerIconRef = null; public contentInputRef = null; public playerIconContainerRef = null; public recognizerEventEmitter = null; public _state = redux.getState(); public readonly state: Readonly = { remark: "", animatedValue: new Animated.Value(0), active: false, }; componentDidMount() { const { initContent } = this.props; if (!isEmpty(initContent)) { this.setState({ remark: initContent + "" }) } // if (!isIos()) { // this.recognizerEventEmitter = new NativeEventEmitter(Recognizer); // this.recognizerEventEmitter.addListener('onRecognizerResult', this._onRecognizerResult) // this.recognizerEventEmitter.addListener('onRecognizerError', this._onRecognizerError) // } } componentWillUnmount() { // this.recognizerEventEmitter && this.recognizerEventEmitter.removeAllListeners(Recognizer); this.setState = (state, callback) => { return; } }; getRemark() { return this.state.remark; } setRemark(text) { this.setState({ remark: text }) }; _onRecognizerError = (e) => { console.log(e) if (e && e.message) { toastShort('语音识别暂不可用,原因: ' + e.message); } else { toastShort('语音识别暂不可用'); } }; _onRecognizerResult = (e) => { console.log('识别结果', e) if (!e.isLast) { return; }; let text = this.state.remark; text = text + e.result; this.setState({ remark: text }) }; onRecordStart = () => { const isNetConnet = redux.getState().isNetConnet; console.log("kit中网络连接情况----->", redux.getState().isNetConnet) if (!isNetConnet) { return toast("无网时不可使用此功能") } PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.RECORD_AUDIO) .then(res => { console.log(res); if (res) { this.justRecord(); } else { this.getPermission(); } }) .catch(err => { console.log(err) }); }; justRecord = () => { //console.log("正式开始听写") Vibration.vibrate([0, 50], false); // Recognizer.start(); this.handelStatrAnimated(); }; onRecordEnd = () => { //console.log("听写结束") // Recognizer.stop(); this.handelEndAnimated(); }; getPermission = async () => { toastShort('语音识别需要您授予麦克风权限') await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.RECORD_AUDIO); toastShort('请再次尝试按下语音识别按钮'); }; handelStatrAnimated() { this.playerIconContainerRef.setNativeProps({ style: { width: '100%', height: px2dp(355), } }); this.playerIconRef && this.playerIconRef.play(1); Animated.spring( this.state.animatedValue, { toValue: 1, useNativeDriver: true } ).start(); this.setState({ active: true }) }; handelEndAnimated() { this.playerIconContainerRef.setNativeProps({ style: { width: 0, height: 0, } }); this.playerIconRef && this.playerIconRef.pause(); Animated.spring( this.state.animatedValue, { toValue: 0, useNativeDriver: true } ).start(); this.setState({ active: false }) }; onChangeText(text) { const newText = filterEmoji(text); this.setState({ remark: newText }) } render() { const { title = "备注", hasPermission = true, maxLength = 300, placeHolder = "", inlineLayout = false, isNeeded = false, contentHeight = px2dp(108) } = this.props; const { remark, animatedValue, active } = this.state; return ( { inlineLayout ? {'备\n注'} { active &&  {'听\n写\n中'} } this.contentInputRef.focus()} style={[styles.remarkContent, { width: px2dp(302) }]} > this.contentInputRef = c} value={remark} style={styles.remarkContentInput} multiline maxLength={maxLength} placeholder={placeHolder ? placeHolder : `请填写相关备注,可语音输入,不超过${maxLength}字...`} onChangeText={this.onChangeText.bind(this)} /> : {title} { isNeeded ? * : null } { active &&    听写中...要结束听写请松开手指 } this.contentInputRef.focus()} style={[styles.remarkContent, { height: contentHeight }]} > this.contentInputRef = c} value={remark} style={styles.remarkContentInput} multiline maxLength={maxLength} placeholder={placeHolder ? placeHolder : `请填写相关备注,可语音输入,不超过${maxLength}字...`} onChangeText={this.onChangeText.bind(this)} /> } {/* { !isIos() ? <> this.playerIconContainerRef = c} style={styles.voiceAnimateContainer}> this.playerIconRef = c} style={{ width: '100%', height: '100%' }} source={require('./../../animated/voice-listener.json')} /> this.floatBtnRef = c} style={[styles.voiceFloatBtn, { transform: [ { scale: animatedValue.interpolate({ inputRange: [0, 1], outputRange: [1, 1.4], }) } ] }]} > { console.log("开始触发"); }} onLongPress={() => this.onRecordStart()} onPressOut={() => this.onRecordEnd()} style={styles.voiceFloatBtnContentOut} > {remark.length}/{maxLength} : null } */} ) } } const styles = StyleSheet.create({ formSingleRow: { position: 'relative', width: '100%', justifyContent: 'center', alignItems: 'center', paddingBottom: px2dp(10), backgroundColor: '#f7f7f8', overflow: 'hidden' }, remarkContentBody: { width: px2dp(350), backgroundColor: '#fff', paddingTop: px2dp(10), paddingHorizontal: px2dp(10), borderBottomLeftRadius: px2dp(6), borderBottomRightRadius: px2dp(6) }, remarkContentBodyInline: { flexDirection: 'row', overflow: 'hidden' }, formGroupTitleContainer: { width: '100%', height: px2dp(24), justifyContent: 'center', alignItems: 'flex-start', marginBottom: px2dp(8) }, formRowTitle: { fontSize: 16, color: '#7F7F7F', }, remarkContent: { width: '100%', height: px2dp(108), backgroundColor: '#f2f1f2', paddingVertical: px2dp(6), paddingHorizontal: px2dp(6), marginBottom: px2dp(12) }, remarkContentInput: { width: '100%', color: colors.blackColor, fontSize: 14, paddingVertical: px2dp(0), }, voiceFloatBtn: { width: px2dp(126), height: px2dp(56), justifyContent: 'center', alignItems: 'center', borderRadius: px2dp(36), zIndex: 999, // backgroundColor: 'red', marginTop: px2dp(-30), }, voiceFloatBtnContentOut: { width: px2dp(126), height: px2dp(56), justifyContent: 'center', alignItems: 'center', // backgroundColor: 'tomato' }, voiceFloatBtnContent: { width: px2dp(56), height: px2dp(56), justifyContent: 'center', alignItems: 'center', backgroundColor: colors.themeColor, borderRadius: px2dp(28), }, voiceIcon: { width: px2dp(28), height: px2dp(28), tintColor: '#fff' }, voiceAnimateContainer: { position: 'absolute', left: px2dp(-4), bottom: px2dp(-128), width: 0, height: 0, // width: '100%', // height: px2dp(355), zIndex: 1, // backgroundColor: '#999' }, textNumContainer: { position: 'absolute', bottom: px2dp(52), right: px2dp(18), flexDirection: 'row', justifyContent: 'flex-end', alignItems: 'center' }, })