addVoice.js 4.23 KB
/**
 * Created by Cassie on 2018/05/14
 */
import React, { Component } from 'react';
import {
    StyleSheet,
    View,
    Text,
    TouchableOpacity,
    Image,
    Platform,
    InteractionManager,
    NativeModules,
    PermissionsAndroid
} from 'react-native';
import {AudioRecorder,AudioUtils} from 'react-native-audio';

import {zoomW,zoomH} from '../../utils/getSize';

const cancle = require('../../img/cancle.png');
const ok = require('../../img/ok.png');
const record = require('../../img/record.png');
const pauseRecord = require('../../img/pauseRecord.png');

export default class AddVoice extends Component {
    static navigationOptions = ({ navigation, screenProps }) => ({
        header:null
    });

    constructor(props){
        super(props);
        this.state = {
            currentTime:'00:00',
            recording:false,
            stoppedRecording:false,
            finished:false,
            audioPath:AudioUtils.DocumentDirectoryPath + '/test.aac',
            canRecord:true
        };
    };

    componentWillMount(){

    };

    componentDidMount(){
        AudioRecorder.prepareRecordingAtPath(this.state.audioPath,{
            SampleRate:22050,
            Channels:1,
            AudioQuality:"Low",
            AudioEncoding:"aac",
            AudioEncodingBitRate:32000
        });
        AudioRecorder.onProgress = (data) => {
            this.setState({
                currentTime:this.formatTime(Math.floor(data.currentTime))
            });
        };
        AudioRecorder.onFinished = (data) => {
            console.log(data.audioFileURL)
            global.videoList.push(data.audioFileURL);
            InteractionManager.runAfterInteractions(() => {this.props.navigation.goBack();})
        };
    };
    /*时间转换*/
    formatTime(seconds) {
        return[
            parseInt(seconds/60%60),
            parseInt(seconds%60)
        ].join(":").replace(/\b(\d)\b/g,"0$1");
    };
    /*开始录音*/
    onRecord(){
        AudioRecorder.startRecording();
        this.setState({
            canRecord:false
        });

    };
    /*暂停录音*/
    pauseRecord(){
        AudioRecorder.pauseRecording();
        this.setState({
            canRecord:true
        });
    };
    /*完成录音*/
    async doneRecord(){
        if(!this.state.canRecord){
            this.pauseRecord();
        }
        if(this.state.currentTime != '00:00'){
            AudioRecorder.stopRecording();
        }
    }

    render(){
        return(
            <View style={styles.background}>
                <Text style={{fontSize:24,color:'#333',fontWeight:'bold',marginTop:(140/zoomH)}}>录音时间</Text>
                <Text style={{fontSize:48,color:'#00be3c',marginTop:(30/zoomH)}}>{this.state.currentTime}</Text>
                <View style={styles.videoSetting}>
                    <TouchableOpacity activeOpacity={0.8} onPress={() => InteractionManager.runAfterInteractions(() => {this.props.navigation.goBack();})}>
                        <Image source={cancle} style={{width:(50/zoomH),height:(50/zoomH)}} resizeMode="contain" />
                    </TouchableOpacity>
                    {this.state.canRecord && <TouchableOpacity activeOpacity={0.8} onPress={() => this.onRecord()}>
                        <Image source={record} style={{width:(120/zoomH),height:(120/zoomH)}} />
                    </TouchableOpacity>}
                    {!this.state.canRecord && <TouchableOpacity activeOpacity={0.8} onPress={() => this.pauseRecord()}>
                        <Image source={pauseRecord} style={{width:(120/zoomH),height:(120/zoomH)}} />
                    </TouchableOpacity>}
                    <TouchableOpacity activeOpacity={0.8} onPress={() => this.doneRecord()}>
                        <Image source={ok} style={{width:(50/zoomH),height:(50/zoomH)}} resizeMode="contain"/>
                    </TouchableOpacity>
                </View>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    background:{
        flex:1,
        backgroundColor:'#fff',
        display:'flex',
        alignItems:'center'
    },
    videoSetting:{
        width:(300/zoomW),
        marginTop:(250/zoomH),
        display:'flex',
        flexDirection:'row',
        justifyContent:'space-between',
        alignItems:'center'
    }
});